如何从 hybris 中删除 CartEntries?

SG *_*dge 1 sap-commerce-cloud

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

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

Hyb*_*elp 5

您必须使用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);
Run Code Online (Sandbox Code Playgroud)

您可以使用 groovy 脚本实现相同的形式。请参阅在 SAP Hybris 中使用 Groovy 脚本创建 Cronjob

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";
Run Code Online (Sandbox Code Playgroud)