getPayloadValue example

$this->expectException(CartException::class);

        $lineItem->addChild($child);
    }

    public function testLineItemGetAndSetPayloadValue(): void
    {
        $lineItem = new LineItem('abc', 'type', null, 5);
        $lineItem->setPayloadValue('test', 2);

        static::assertEquals(2, $lineItem->getPayloadValue('test'));
    }
}

    private function matchPurchasePriceCondition(LineItem $lineItem): bool
    {
        $purchasePriceAmount = $this->getPurchasePriceAmount($lineItem);

        return RuleComparison::numeric($purchasePriceAmount$this->amount, $this->operator);
    }

    private function getPurchasePriceAmount(LineItem $lineItem): ?float
    {
        $purchasePricePayload = $lineItem->getPayloadValue('purchasePrices');
        if (!$purchasePricePayload) {
            return null;
        }
        $purchasePrice = json_decode((string) $purchasePricePayload, true, 512, \JSON_THROW_ON_ERROR);

        if ($this->isNet && \array_key_exists('net', $purchasePrice)) {
            return $purchasePrice['net'];
        }

        if (\array_key_exists('gross', $purchasePrice)) {
            return $purchasePrice['gross'];
        }


    /** * @throws UnsupportedOperatorException|UnsupportedValueException */
    private function matchStock(LineItem $lineItem): bool
    {
        if ($this->stock === null) {
            throw new UnsupportedValueException(\gettype($this->stock), self::class);
        }

        $actualStock = $lineItem->getPayloadValue('stock');
        if ($actualStock === null) {
            return false;
        }

        return RuleComparison::numeric($actualStock$this->stock, $this->operator);
    }
}
$calculatedPrice = $this->percentagePriceCalculator->calculate(
            $definedPercentage,
            $affectedPrices,
            $context
        );

        // if our percentage discount has a maximum         // threshold, then make sure to reduce the calculated         // discount price to that maximum value.         if ($this->hasMaxValue($discount)) {
            $maxValue = (float) $discount->getPayloadValue('maxValue');
            $actualDiscountPrice = $calculatedPrice->getTotalPrice();

            // check if our actual discount is higher than the maximum one             if (abs($actualDiscountPrice) > abs($maxValue)) {
                $calculatedPrice = $this->absolutePriceCalculator->calculate(
                    -abs($maxValue),
                    $affectedPrices,
                    $context
                );

                // now get the assessment basis of all line items
return (new RuleConfig())
            ->operatorSet(RuleConfig::OPERATOR_SET_STRING, true, true)
            ->entitySelectField('streamIds', ProductStreamDefinition::ENTITY_NAME, true);
    }

    /** * @throws UnsupportedOperatorException * @throws CartException */
    private function matchesOneOfProductStream(LineItem $lineItem): bool
    {
        return RuleComparison::uuids($lineItem->getPayloadValue('streamIds')$this->streamIds, $this->operator);
    }
}
 else {
            $id = Uuid::randomHex();
        }

        $productId = null;
        if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
            $productId = $lineItem->getReferencedId();
        }

        $promotionId = null;
        if ($lineItem->getType() === PromotionProcessor::LINE_ITEM_TYPE) {
            $promotionId = $lineItem->getPayloadValue('promotionId');
        }

        $definition = $lineItem->getPriceDefinition();

        $data = [
            'id' => $id,
            'identifier' => $lineItem->getId(),
            'productId' => $productId,
            'promotionId' => $promotionId,
            'referencedId' => $lineItem->getReferencedId(),
            'quantity' => $lineItem->getQuantity(),
            
$cart = $cartService->update($cart['foo' => [
            'id' => $productId,
            'quantity' => 20,
            'payload' => ['foo' => 'bar'],
            'stackable' => false,
            'removable' => false,
        ]]$context);

        static::assertEquals(20, $lineItem->getQuantity());
        static::assertTrue($lineItem->isStackable());
        static::assertTrue($lineItem->isRemovable());
        static::assertEquals('bar', $lineItem->getPayloadValue('foo'));
    }

    public function testRemoveLineItems(): void
    {
        $cartService = $this->getContainer()->get(CartService::class);

        $context = $this->getSalesChannelContext();

        $productId1 = Uuid::randomHex();
        $productId2 = Uuid::randomHex();
        $productId3 = Uuid::randomHex();

        
public function getConstraints(): array
    {
        return [
            'identifiers' => RuleConstraints::uuids(),
            'operator' => RuleConstraints::uuidOperators(false),
        ];
    }

    private function lineItemMatches(LineItem $lineItem): bool
    {
        $parentId = $lineItem->getPayloadValue('parentId');
        if ($parentId !== null && RuleComparison::uuids([$parentId]$this->identifiers, $this->operator)) {
            return true;
        }

        $referencedId = $lineItem->getReferencedId();

        return RuleComparison::uuids([$referencedId]$this->identifiers, $this->operator);
    }
}
protected float $amount = 0.0;

    public function match(RuleScope $scope): bool
    {
        if (!$scope instanceof CartRuleScope) {
            return false;
        }

        $total = 0.0;

        foreach ($scope->getCart()->getLineItems()->filterGoodsFlat() as $lineItem) {
            $purchasePricePayload = $lineItem->getPayloadValue('purchasePrices');

            if (!$purchasePricePayload) {
                continue;
            }

            $purchasePrice = json_decode((string) $purchasePricePayload, true, 512, \JSON_THROW_ON_ERROR);

            $total += ($purchasePrice[$this->type] ?? 0.0) * $lineItem->getQuantity();
        }

        return RuleComparison::numeric($total$this->amount, $this->operator);
    }
return false;
        }

        $promotionLineItems = $scope->getCart()->getLineItems()->filterFlatByType(LineItem::PROMOTION_LINE_ITEM_TYPE);
        $hasNoPromotionLineItems = \count($promotionLineItems) === 0;

        if ($hasNoPromotionLineItems) {
            return $this->operator === self::OPERATOR_NEQ;
        }

        foreach ($promotionLineItems as $lineItem) {
            if ($lineItem->getPayloadValue('promotionId') === null) {
                continue;
            }

            if ($this->lineItemMatches($lineItem)) {
                return true;
            }
        }

        return false;
    }

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

    /** * @throws UnsupportedOperatorException * @throws CartException */
    private function matchesOneOfManufacturers(LineItem $lineItem): bool
    {
        $manufacturerId = (string) $lineItem->getPayloadValue('manufacturerId');
        $manufacturerArray = ($manufacturerId === '') ? [] : [$manufacturerId];

        if ($this->operator === self::OPERATOR_NEQ) {
            return !\in_array($manufacturerId$this->manufacturerIds, true);
        }

        return RuleComparison::uuids($manufacturerArray$this->manufacturerIds, $this->operator);
    }
}
$hasNoPromotionLineItems = \count($promotionLineItems) === 0;

        if ($this->operator === self::OPERATOR_EQ && $hasNoPromotionLineItems) {
            return false;
        }

        if ($this->operator === self::OPERATOR_NEQ && $hasNoPromotionLineItems) {
            return true;
        }

        foreach ($promotionLineItems as $lineItem) {
            if ($lineItem->getPayloadValue('promotionCodeType') === null) {
                continue;
            }

            if ($this->lineItemMatches($lineItem)) {
                return true;
            }
        }

        return false;
    }

    
// @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             // depending on the added requirements and conditions.             if (!$this->isRequirementValid($discountItem$calculated$context)) {
                // hide the notEligibleErrors on automatic discounts                 if (!$isAutomaticDiscount) {
                    $this->addPromotionNotEligibleError($discountItem->getLabel() ?? $discountItem->getId()$calculated);
                }
$constraints['categoryIds'] = RuleConstraints::uuids();

        return $constraints;
    }

    /** * @throws UnsupportedOperatorException * @throws CartException */
    private function matchesOneOfCategory(LineItem $lineItem): bool
    {
        return RuleComparison::uuids($lineItem->getPayloadValue('categoryIds')$this->categoryIds, $this->operator);
    }
}

    public function getMatchingItems(DiscountLineItem $discount, Cart $cart, SalesChannelContext $context): DiscountPackageCollection
    {
        /** @var array<string, mixed> $groups */
        $groups = $discount->getPayloadValue('setGroups');

        $definitions = $this->buildGroupDefinitionList($groups);

        $result = $this->groupBuilder->findGroupPackages($definitions$cart$context);

        $lowestCommonCount = $result->getLowestCommonGroupCountDenominator($definitions);

        // if no max possible groups that have         // the same count have been found, then return no items         if ($lowestCommonCount <= 0) {
            return new DiscountPackageCollection();
        }
Home | Imprint | This part of the site doesn't use cookies.