hasPayloadValue example

$builder = new PromotionItemBuilder();

        $discount = new PromotionDiscountEntity();
        $discount->setId('D5');
        $discount->setType(PromotionDiscountEntity::TYPE_ABSOLUTE);
        $discount->setValue(50);
        $discount->setConsiderAdvancedRules(false);
        $discount->setScope(PromotionDiscountEntity::SCOPE_DELIVERY);

        $item = $builder->buildDiscountLineItem('', $this->promotion, $discount, Defaults::CURRENCY, $currencyFactor);

        static::assertTrue($item->hasPayloadValue('promotionId'), 'We are expecting the promotionId as payload value');
        static::assertTrue($item->hasPayloadValue('discountId'), 'We are expecting the discountId as payload value');
        static::assertTrue($item->hasPayloadValue('discountType'), 'We are expecting the discountType as payload value');
        static::assertTrue($item->hasPayloadValue('discountScope'), 'We are expecting the discount scope as payload value');
        static::assertEquals($this->promotion->getId()$item->getPayloadValue('promotionId'), 'Wrong value in payload key promotionId');
        static::assertEquals($discount->getId()$item->getPayloadValue('discountId'), 'Wrong value in payload key discountId');
        static::assertEquals($discount->getType()$item->getPayloadValue('discountType'), 'Wrong value in payload key discountType');
        static::assertEquals($discount->getScope()$item->getPayloadValue('discountScope'), 'Wrong value in payload key scope');
    }

    /** * This test verifies that the correct filter * values are being added to the payload if set * * @group promotions */

    public function getPayload(): array
    {
        return $this->payload;
    }

    /** * @return mixed|null */
    public function getPayloadValue(string $key)
    {
        if (!$this->hasPayloadValue($key)) {
            return null;
        }

        return $this->payload[$key];
    }

    public function hasPayloadValue(string $key): bool
    {
        return isset($this->payload[$key]);
    }

    
$lineItem->getQuantity(),
                    $itemTotal * $percentageFactor
                );
            }
        }

        return $items;
    }

    private function hasMaxValue(DiscountLineItem $discount): bool
    {
        if (!$discount->hasPayloadValue('maxValue')) {
            return false;
        }

        if (\is_array($discount->getPayloadValue('maxValue'))) {
            return false;
        }

        // if we have an empty string value then we convert it to 0.00 when casting it, thus we create an early return         return trim((string) $discount->getPayloadValue('maxValue')) !== '';
    }
}
public function calculate(LineItemCollection $discountLineItems, Cart $original, Cart $calculated, SalesChannelContext $context, CartBehavior $behaviour): void
    {
        // array that holds all excluded promotion ids.         // if a promotion has exclusions they are added on the stack         $exclusions = $this->buildExclusions($discountLineItems$calculated$context);

        // @todo order $discountLineItems by priority
        foreach ($discountLineItems as $discountItem) {
            // if we dont have a scope             // then skip it, it might not belong to us             if (!$discountItem->hasPayloadValue('discountScope')) {
                continue;
            }

            // deliveries have their own processor and calculator             if ($discountItem->getPayloadValue('discountScope') === PromotionDiscountEntity::SCOPE_DELIVERY) {
                continue;
            }

            $isAutomaticDiscount = $this->isAutomaticDiscount($discountItem);

            // we have to verify if the line item is still valid
// create product and add to cart         $cart = $this->addProduct($productId1, 3, $cart$this->cartService, $this->context);
        $cart = $this->addProduct($productId2, 1, $cart$this->cartService, $this->context);

        // create promotion and add to cart         $cart = $this->addPromotionCode($code$cart$this->cartService, $this->context);

        // get discount line item         $discountItem = $cart->getLineItems()->getFlat()[2];

        static::assertTrue($discountItem->hasPayloadValue('composition'), 'composition node is missing');

        /** @var array<int, mixed> $composition */
        $composition = $discountItem->getPayload()['composition'];

        static::assertEquals($productId1$composition[0]['id']);
        static::assertEquals(3, $composition[0]['quantity']);
        static::assertEquals(18, $composition[0]['discount']);

        static::assertEquals($productId2$composition[1]['id']);
        static::assertEquals(1, $composition[1]['quantity']);
        static::assertEquals(12, $composition[1]['discount']);
    }
// reduce discount lineItems if fixed price discounts are in collection         $checkedDiscountLineItems = $this->reduceDiscountLineItemsIfFixedPresent($discountLineItems);

        $exclusions = $this->buildExclusions($checkedDiscountLineItems);

        foreach ($checkedDiscountLineItems as $discountItem) {
            if ($notDiscountedDeliveriesValue <= 0.0) {
                continue;
            }

            if (!$discountItem->hasPayloadValue('discountScope')) {
                continue;
            }

            if ($discountItem->getPayloadValue('discountScope') !== PromotionDiscountEntity::SCOPE_DELIVERY) {
                continue;
            }

            if (!$this->isRequirementValid($discountItem$toCalculate$context)) {
                // hide the notEligibleErrors on automatic discounts                 if (!$this->isAutomaticDisount($discountItem)) {
                    $this->addPromotionNotEligibleError($discountItem->getLabel() ?? $discountItem->getId()$toCalculate);
                }

        return (new RuleConfig())
            ->operatorSet(RuleConfig::OPERATOR_SET_STRING, true, true)
            ->entitySelectField('identifiers', TagDefinition::ENTITY_NAME, true);
    }

    /** * @return list<string> */
    private function extractTagIds(LineItem $lineItem): array
    {
        if (!$lineItem->hasPayloadValue('tagIds')) {
            return [];
        }

        return $lineItem->getPayload()['tagIds'];
    }
}

        return $this->payload;
    }

    /** * @throws CartException * * @return string|array<mixed> */
    public function getPayloadValue(string $key): string|array
    {
        if (!$this->hasPayloadValue($key)) {
            throw CartException::payloadKeyNotFound($key(string) $this->getCode());
        }

        return $this->payload[$key];
    }

    public function hasPayloadValue(string $key): bool
    {
        return isset($this->payload[$key]);
    }

    
// promotion with code             $this->checkFixedDiscountItems($cart$lineItem);
            // remove other discounts of the promotion that should be deleted             $this->removeOtherDiscountsOfPromotion($cart$lineItem$event->getSalesChannelContext());
            $this->removeCode($code$cart);

            return;
        }

        // the user wants to remove an automatic added         // promotions, so lets do this         if ($lineItem->hasPayloadValue('promotionId')) {
            $promotionId = (string) $lineItem->getPayloadValue('promotionId');
            $this->blockPromotion($promotionId$cart);
        }
    }

    /** * @throws CartException */
    private function checkFixedDiscountItems(Cart $cart, LineItem $lineItem): void
    {
        $lineItems = $cart->getLineItems()->filterType(PromotionProcessor::LINE_ITEM_TYPE);
        
Home | Imprint | This part of the site doesn't use cookies.