You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6购物车商品删除失败求助:如何有效移除不符合规则的商品

问题:Shopware中无法删除购物车属性冲突商品

我需要阻止带/不带dropshipping属性的商品混入同一购物车,已经实现了提示逻辑,但没法移除触发冲突的商品。

现有监听AfterLineItemAddedEvent的代码:

public function onLineItemAdded(AfterLineItemAddedEvent $event): void
{
    $lineItems = $event->getLineItems();
    $shipperExists = false;
    foreach ($lineItems as $lineItem) {
        $this->processCustomFields($lineItem);
        if (isset($lineItem->getPayload()["customFields"]["dropper_"])) {
            $shipperExists = true;
        }
    }
    if (!$shipperExists && isset($this->customFields["dropper_"])) {
        $this->flashBag->add(
            "info",
            "Invalid entry, You are trying to include a product without dropshipping option to a product with a dropshipping option."
        );
    }
    if ($shipperExists && !isset($this->customFields["dropper_"]) && isset($this->customFields)) {
        $this->flashBag->add(
            "info",
            "Invalid entry, You are trying to include a product with dropshipping option to a product without a dropshipping option."
        );
    }
}

之后尝试用CartService删商品但没成功,代码片段:

use Shopware\Core\Checkout\Cart\CartService;

$cart = $cartService->getCart($contextToken);
$cart = $cartService->delete($cart, $lineItemId, $context);

求解决办法。


问题分析与解决办法

1. 事件阶段选择错误

AfterLineItemAddedEvent触发时,购物车的变更流程已接近完成,单独调用CartService->delete会拿到独立的购物车实例,修改后无法同步到当前流程中。应该直接操作事件传递的购物车实例。

2. 修正后的删除逻辑

直接从事件中获取购物车,定位冲突的新增商品后移除,代码调整如下:

public function onLineItemAdded(AfterLineItemAddedEvent $event): void
{
    $cart = $event->getCart();
    $allLineItems = $cart->getLineItems();
    $shipperExists = false;
    $conflictingItemId = null;
    $addedItemHasDropship = false;

    // 检查购物车现有商品是否包含dropshipping属性
    foreach ($allLineItems as $item) {
        if (isset($item->getPayload()["customFields"]["dropper_"])) {
            $shipperExists = true;
            break;
        }
    }

    // 遍历刚添加的商品,判断属性是否冲突
    foreach ($event->getLineItems() as $addedItem) {
        $this->processCustomFields($addedItem);
        $addedItemHasDropship = isset($addedItem->getPayload()["customFields"]["dropper_"]);

        // 冲突判定:现有商品有dropship但新增无,或现有无但新增有
        if (($shipperExists && !$addedItemHasDropship) || (!$shipperExists && $addedItemHasDropship)) {
            $conflictingItemId = $addedItem->getId();
            break;
        }
    }

    if ($conflictingItemId) {
        // 直接从事件的购物车实例中移除冲突商品
        $cart->remove($conflictingItemId);
        // 添加对应提示信息
        $message = $addedItemHasDropship 
            ? "Invalid entry, You are trying to include a product with dropshipping option to a product without a dropshipping option."
            : "Invalid entry, You are trying to include a product without dropshipping option to a product with a dropshipping option.";
        $this->flashBag->add("info", $message);
    }
}

3. 额外注意事项

  • 避免使用$this->customFields,直接从新增商品的Payload中获取属性,防止上下文错乱。
  • 修改事件中的购物车实例后,无需手动调用CartService->save,Shopware会自动处理持久化。
  • 确保服务已正确注入FlashBagInterface,否则提示信息无法正常显示。

内容的提问来源于stack exchange,提问作者olawole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 20:24:52