getBehavior example

/** * The `calculate()` method recalculates the whole cart. * Use this to get the correct prices after you made changes to the cart. * Note that after calling the `calculate()` all collections (e.g. items(), products()) get new references, * so if you still hold references to things inside the cart, these are outdated after calling `calculate()`. * * The `calculate()` method will be called automatically after your cart script executed. */
    public function calculate(): void
    {
        $behavior = $this->cart->getBehavior();
        if (!$behavior) {
            throw CartException::missingCartBehavior();
        }

        $this->cart = $behavior->disableHooks(fn () => $this->helper->calculate($this->cart, $behavior$this->context));
    }

    /** * The `price()` method returns the current price of the cart. * Note that this price may be outdated, if you changed something inside the cart in your script. * Use the `calculate()` method to recalculate the cart and update the price. * * @return CartPriceFacade The calculated price of the cart. */
use Shopware\Core\Checkout\Cart\CartValidatorInterface;
use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

#[Package('checkout')] class ProductLineItemValidator implements CartValidatorInterface
{
    public function validate(Cart $cart, ErrorCollection $errors, SalesChannelContext $context): void
    {
        $behavior = $cart->getBehavior();
        if ($behavior !== null && $behavior->hasPermission(ProductCartProcessor::SKIP_PRODUCT_STOCK_VALIDATION)) {
            return;
        }

        $productLineItems = array_filter($cart->getLineItems()->getFlat()static fn (LineItem $lineItem) => $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE);

        $quantities = [];
        $refs = [];
        foreach ($productLineItems as $lineItem) {
            $productId = $lineItem->getReferencedId();
            if ($productId === null) {
                
$delivery->getPositions()->getLineItems(),
                $context,
                $manualShippingCost
            );

            $delivery->setShippingCosts($costs);

            return;
        }

        if (
            $this->hasDeliveryPriceRecalculationSkipWithZeroUnitPrice($cart->getBehavior()$delivery->getShippingCosts()->getUnitPrice())
            || $this->hasDeliveryWithOnlyShippingFreeItems($delivery)
        ) {
            $costs = $this->calculateShippingCosts(
                $delivery->getShippingMethod(),
                new PriceCollection([new Price(Defaults::CURRENCY, 0, 0, false)]),
                $delivery->getPositions()->getLineItems(),
                $context
            );
            $delivery->setShippingCosts($costs);

            return;
        }
$this->eventDispatcher->dispatch(new CartLoadedEvent($cart$context));

        return $cart;
    }

    /** * @throws InvalidUuidException */
    public function save(Cart $cart, SalesChannelContext $context): void
    {
        if ($cart->getBehavior()?->isRecalculation()) {
            return;
        }

        $shouldPersist = $this->shouldPersist($cart);

        $event = new CartVerifyPersistEvent($context$cart$shouldPersist);
        $this->eventDispatcher->dispatch($event);

        if (!$event->shouldBePersisted()) {
            $this->delete($cart->getToken()$context);

            
Home | Imprint | This part of the site doesn't use cookies.