【问题标题】:shopware 6 cart line items duplicate resultsshopware 6 购物车行项目重复结果
【发布时间】:2022-07-05 19:03:23
【问题描述】:

给出下面的代码示例

private function test(Cart $cart): LineItemCollection
{
    return $cart->getLineItems()->filter(function (LineItem $item) {
        // Only consider products, not custom line items or promotional line items
        if ($item->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
            return false;
        }
        $exampleInLabel = $item->getLabel();

并且一件商品在购物车中,exampleInLabel 是标签的两倍。

这与任何其他功能相同,例如getPayloadValue()getQuantity() 等。

如何克服?

【问题讨论】:

  • 这个方法是在CartProcessor 中调用的吗?或者它叫什么名字?请记住,处理器在计算过程中可能会运行多次,因此有问题的代码可能会被简单地执行两次,因为商店软件会重新运行购物车计算。
  • @j_elfering 是的,它在CartProcessor 中,我知道商店软件会多次遍历购物车以根据规则“丰富”数据。但是,我的问题仍然存在...如果我需要获取项目的数量以创建更多规则,例如我会得到 33 而不是 3...等等...任何解决方案?

标签: php shopware shopware6


【解决方案1】:

尝试在订阅者中执行此操作,可能在 beforeLineItemAddedEvent 中

【讨论】:

    【解决方案2】:

    如果处理您的代码的唯一方法是放入多次执行的 CartProcessor,您可以设置一个运行时类属性,该属性设置为 false,然后在第一次处理时设置为 true。在添加数量之前,请检查此属性是否为 false。

    private bool $hasBeenProcessedAlready = false;
    
    private function test(Cart $cart): LineItemCollection
    {
        return $cart->getLineItems()->filter(function (LineItem $item) {
            // Only consider products, not custom line items or promotional line items
            if (
                $item->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE
                || $hasBeenProcessedAlready
            ) {
                return false;
            }
            $exampleInLabel = $item->getLabel();
            $this->hasBeenProcessedAlready = true;
    

    可能不是最漂亮的方法,但应该可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 2023-01-29
      • 1970-01-01
      • 2022-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      相关资源
      最近更新 更多