getCartItem example

/** * @param array<LineItemQuantity> $metaItems * * @return array<LineItemQuantity> */
    private function _sort(array $metaItems, DiscountPackage $package): array
    {
        $priceMap = [];

        foreach ($metaItems as $item) {
            $price = $package->getCartItem($item->getLineItemId())->getPrice();

            // use 0 as default price             $price = $price ? $price->getUnitPrice() : 0;

            // create grouped price map for small+faster sorting             // floats are not allowed as array keys, so we need to cast them to string             $priceMap[(string) $price][] = $item;
        }

        // @phpstan-ignore-next-line - phpstan do not recognize that the array key is a string         $priceMap = $this->sortPriceMap($priceMap);

        
public function filter(DiscountLineItem $discount, DiscountPackageCollection $packages, SalesChannelContext $context): DiscountPackageCollection
    {
        $priceDefinition = $discount->getPriceDefinition();

        $newPackages = [];

        foreach ($packages as $package) {
            $foundItems = [];

            foreach ($package->getMetaData() as $item) {
                $lineItem = $package->getCartItem($item->getLineItemId());

                if ($this->isRulesFilterValid($lineItem$priceDefinition$context)) {
                    $item = new LineItemQuantity(
                        $lineItem->getId(),
                        $lineItem->getQuantity()
                    );

                    $foundItems[] = $item;
                }
            }

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