如何从 hybris 中删除 CartEntries?

How to delete CartEntries from hybris?

我需要创建一个 CronJob 来删除特定类型的购物车条目。我已经找到了所有需要PK的条目,但我仍然无法删除它们。

我在网上发现我不能为此使用 FlexibleSearchQuery。我也没有在 CartEntryService 中找到任何方法。删除逻辑在哪里?

您必须使用 modelService 从数据库中删除任何模型。在您的情况下,您需要做的就是将 cartEntryModel 的列表传递给 getModelService().removeAll(list)

查看 DefaultCartService updateQuantities 方法,基本上这个方法是用 0 数量为用户试图删除的购物车条目调用的来自购物车。

    final Collection<CartEntryModel> toRemove = new LinkedList<CartEntryModel>();
    final Collection<CartEntryModel> toSave = new LinkedList<CartEntryModel>();
    for (final Map.Entry<CartEntryModel, Long> e : getEntryQuantityMap(cart, quantities).entrySet())
    {
        final CartEntryModel cartEntry = e.getKey();
        final Long quantity = e.getValue();
        if (quantity == null || quantity.longValue() < 1)
        {
            toRemove.add(cartEntry);
        }
        else
        {
            cartEntry.setQuantity(quantity);
            toSave.add(cartEntry);
        }
    }
    getModelService().removeAll(toRemove);

您可以使用 groovy 脚本实现相同的表单。参考 create a Cronjob using Groovy script in SAP Hybris

Groovy 脚本示例:

import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.model.ModelService;
import de.hybris.platform.core.model.user.CartEntryModel;
import de.hybris.platform.servicelayer.search.SearchResult;
import org.apache.commons.collections.CollectionUtils;



def removeCartEntries()

 {
  final SearchResult<CartEntryModel> searchResults = flexibleSearchService.search("query to get the list of cartentries")
  if (searchResults != null && CollectionUtils.isNotEmpty(searchResults.getResult())) {
      modelService.removeAll(searchResults.getResult());
   }
 }
removeCartEntries();

println "done";