最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - How to calculate cart in plugin? - Stack Overflow

programmeradmin1浏览0评论

I am working on a Shopware 6 plugin to add some options while adding a product to the cart. User can choose some options of the product and when clicking "Add to cart", the ids of the selected options are sent alongside with a new LineItem to the backend. There, I add all the options as children to the product LineItem in the CartDataCollector. The children are added to the cart, here I have no problem.

However, Shopware does not include the prices of those child LineItems in the cart total price.

I tried this CartProcessor and it does not work:

class AdditionalPriceCartProcessor implements CartProcessorInterface
{
    private QuantityPriceCalculator $calculator;

    public function __construct(
        QuantityPriceCalculator $calculator
    ) {
        $this->calculator = $calculator;
    }

    public function process(
        CartDataCollection $data,
        Cart $original,
        Cart $toCalculate,
        SalesChannelContext $context,
        CartBehavior $behavior
    ): void {
        $prices = new PriceCollection();

        foreach ($toCalculate->getLineItems() as $lineItem) {
            if ($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
                $prices->add($lineItem->getPrice());
                continue;
            }

            if (!$lineItem->hasPayloadValue('HMnetAdditionalPricesAreSet')) {
                $prices->add($lineItem->getPrice());
                continue;
            }

            $tempOriginalCart = new Cart('temp-original', $original->getToken());
            $tempCalculateCart = new Cart('temp-calculate', $original->getToken());

            $tempCalculateCart->setLineItems(
                $lineItem->getChildren()
            );

            $this->processChildren($data, $tempOriginalCart, $tempCalculateCart, $context, $behavior, $prices);

            $lineItem->setChildren(
                $tempCalculateCart->getLineItems()
            );
        }

        $newCartPrice = $this->sum($original->getPrice(), $prices);
        $toCalculate->setPrice($newCartPrice);
    }

    private function processChildren(
        CartDataCollection $data,
        Cart $original,
        Cart $toCalculate,
        SalesChannelContext $context,
        CartBehavior $behavior,
        PriceCollection $prices
    ): void {
        foreach ($toCalculate->getLineItems() as $lineItem) {
            // ensure we calculate only the items added via this plugin
            ...

            $priceDefinition = $lineItem->getPriceDefinition();
            $calculated = $this->calculator->calculate($priceDefinition, $context);

            $lineItem->setPrice($calculated);
            $prices->add($calculated);
        }
    }

    private function sum(CartPrice $original, PriceCollection $prices): CartPrice
    {
        $total = $original->getTotalPrice();

        foreach ($prices as $price) {
            $total += $price->getTotalPrice();
        }

        return new CartPrice(
            $total,
            $total,
            $total,
            $original->getCalculatedTaxes(),
            $original->getTaxRules(),
            $original->getTaxStatus()
        );
    }
}
发布评论

评论列表(0)

  1. 暂无评论