setPayload example

$cart->addLineItems(new LineItemCollection([$item1$item2$item3]));

        $rules = new AndRule(
            [
                $this->getProductsRule([$item1->getReferencedId()$item2->getReferencedId()]),
            ]
        );

        $ruleEntity = new RuleEntity();
        $ruleEntity->setId(Uuid::randomHex());
        $ruleEntity->setPayload($rules);

        $group = $this->buildGroup(
            self::KEY_PACKAGER_COUNT,
            5,
            self::KEY_SORTER_PRICE_DESC,
            new RuleCollection([$ruleEntity])
        );

        $result = $this->unitTestBuilder->findGroupPackages([$group]$cart$this->context);
        $groupCount = $result->getGroupResult($group);

        
private static function updateLineItem(LineItem $lineItem, OrderLineItemEntity $entity, string $id): void
    {
        $lineItem->setId($entity->getIdentifier())
            ->setLabel($entity->getLabel())
            ->setGood($entity->getGood())
            ->setRemovable($entity->getRemovable())
            ->setStackable($entity->getStackable())
            ->setStates($entity->getStates())
            ->addExtension(OrderConverter::ORIGINAL_ID, new IdStruct($id));

        if ($entity->getPayload() !== null) {
            $lineItem->setPayload($entity->getPayload());
        }

        if ($entity->getPrice() !== null) {
            $lineItem->setPrice($entity->getPrice());
        }

        if ($entity->getPriceDefinition() !== null) {
            $lineItem->setPriceDefinition($entity->getPriceDefinition());
        }

        if ($entity->getDownloads() !== null) {
            

        $currencyFactor = random_int(0, mt_getrandmax()) / mt_getrandmax();

        $discount = new PromotionDiscountEntity();
        $discount->setId('P123');
        $discount->setType(PromotionDiscountEntity::TYPE_PERCENTAGE);
        $discount->setValue(0);
        $discount->setScope(PromotionDiscountEntity::SCOPE_CART);

        $rule = new RuleEntity();
        $rule->setId('R1');
        $rule->setPayload((new LineItemUnitPriceRule())->assign(['amount' => 10, 'operator' => '=']));

        $ruleCollection = new RuleCollection([$rule]);

        $group = new PromotionSetGroupEntity();
        $group->setId(Uuid::randomBytes());
        $group->setPackagerKey('COUNT');
        $group->setValue(2);
        $group->setSorterKey('PRICE_ASC');
        $group->setSetGroupRules($ruleCollection);

        $this->promotion->setSetgroups(new PromotionSetGroupCollection([$group]));

        
$lock = $this->createMock(LockInterface::class);
        $lock->method('acquire')->willReturn(true);
        $lockFactory->expects(static::once())->method('createLock')->willReturn($lock);

        $versionCommit = new VersionCommitEntity();
        $versionCommitData = new VersionCommitDataEntity();
        $versionCommitData->setAction('upsert');
        $versionCommitData->setId(Uuid::randomHex());
        $versionCommitData->setEntityName('product');
        $versionCommitData->setEntityId([Uuid::randomHex()]);
        $versionCommitData->setPayload(['Id' => Uuid::randomHex()]);
        $versionCommit->setData(new VersionCommitDataCollection([$versionCommitData]));
        $versionCommit->setId(Uuid::randomHex());

        $entityReaderMock->expects(static::once())->method('read')->willReturn(new VersionCommitCollection([$versionCommit]));

        $writeContextMock = $this->createMock(WriteContext::class);

        $this->versionManager->merge(
            Uuid::randomHex(),
            $writeContextMock
        );
    }
static::assertArrayHasKey('score', $encoded['extensions']['search']);
        static::assertArrayHasKey('exposedFk', $encoded['extensions']['foreignKeys']);
        static::assertArrayNotHasKey('notExposedFk', $encoded['extensions']['foreignKeys']);
    }

    public function testPayloadProtection(): void
    {
        $cart = new Cart('test');

        $item = new LineItem('test', LineItem::PRODUCT_LINE_ITEM_TYPE, 'test');

        $item->setPayload(['foo' => 'bar', 'bar' => 'foo']['foo' => false, 'bar' => true]);

        $cart->add($item);

        $serializer = new Serializer([new StructNormalizer()][new JsonEncoder()]);

        $encoded = (new StructEncoder($this->createMock(DefinitionRegistryChain::class)$serializer))
            ->encode($cartnew ResponseFields(null));

        static::assertIsArray($encoded);
        static::assertArrayHasKey('lineItems', $encoded);
        static::assertArrayHasKey(0, $encoded['lineItems']);
        
/** * @return RuleCollection|RuleEntity */
    public function getInput(): EntityCollection|Entity
    {
        $ruleCollection = new RuleCollection();

        $rule = new RuleEntity();
        $rule->setId('f343a3c119cf42a7841aa0ac5094908c');
        $rule->setName('Test rule');
        $rule->setDescription('Test description');
        $rule->setPayload(new AndRule([new TrueRule()new FalseRule()]));
        $rule->internalSetEntityData('rule', new FieldVisibility([]));
        $ruleCollection->add($rule);

        return $ruleCollection;
    }

    /** * @return array<string, mixed> */
    protected function getJsonApiFixtures(string $baseUrl): array
    {
        

    public function testPreconditionRulePersonaRules(): void
    {
        $fakePersonaRule = new AndRule();

        $personaRuleEntity = new RuleEntity();
        $personaRuleEntity->setId('R1');
        $personaRuleEntity->setPayload($fakePersonaRule);

        $promotion = new PromotionEntity();
        $promotion->setCustomerRestriction(false);

        $promotion->setPersonaRules(new RuleCollection([$personaRuleEntity]));

        $expected = new AndRule(
            [
                new OrRule(
                    [$fakePersonaRule]
                ),
            ]
$flow = new StorableFlow('bar', Context::createDefaultContext());
        $flow->setFlowState(new FlowState());
        $flow->setData(OrderAware::ORDER, $order);

        $scopeBuilder->method('build')->willReturn(
            new FlowRuleScope($ordernew Cart('test')$this->createMock(SalesChannelContext::class))
        );

        $rule = new OrderTagRule(Rule::OPERATOR_EQ, [$tagId]);
        $ruleEntity = new RuleEntity();
        $ruleEntity->setId($ruleId);
        $ruleEntity->setPayload($rule);
        $ruleEntity->setAreas([RuleAreas::FLOW_AREA]);
        $ruleLoader->method('load')->willReturn(new RuleCollection([$ruleEntity]));

        $flowExecutor = new FlowExecutor($eventDispatcher$appFlowActionProvider$ruleLoader$scopeBuilder[]);
        $flowExecutor->executeIf($ifSequence$flow);

        static::assertEquals($trueCaseSequence$flow->getFlowState()->currentSequence);
    }
}
self::coverCart('foo', null, true),
            null,
        ];
    }

    /** * @param array<string, mixed> $payload */
    private static function payloadItem(string $id, array $payload): LineItem
    {
        $item = new LineItem($id, 'foo');
        $item->setPayload($payload);

        $childItem = new LineItem($id . '-child', 'foo');
        $childItem->setPayload($payload);

        $item->addChild($childItem);

        return $item;
    }

    private static function coverItem(string $id, ?string $thumbnailString, bool $skipCover = false): LineItem
    {
        
// always make sure we have a valid code entry.         // this helps us to identify the item by code later on.         // we use the one from the argument, because that one tells us why this         // promotion is added...it might not just be the promotion code, but         // one of the thousand individual codes for it...thus we have an         // external algorithm that makes our lookup why this promotion is added.         $promotionItem->setReferencedId($code);

        // add custom content to our payload.         // we need this as meta data information.         $promotionItem->setPayload(
            $this->buildPayload(
                $code,
                $discount,
                $promotion,
                $currencyId,
                $currencyFactor
            )
        );

        // add our lazy-validation rules.         // this is required within the recalculation process.

        $lineItemA = new OrderLineItemEntity();
        $lineItemA->setId(Uuid::randomHex());

        $lineItemB = new OrderLineItemEntity();
        $lineItemB->setId(Uuid::randomHex());

        $collection = new OrderLineItemCollection([$lineItemA$lineItemB]);

        static::assertEquals([]$collection->getPayloadsProperty('foobar'));

        $lineItemA->setPayload(['foobar' => 'foo']);

        static::assertEquals([$lineItemA->getId() => 'foo']$collection->getPayloadsProperty('foobar'));

        $lineItemB->setPayload(['foobar' => 'bar']);

        static::assertEquals([$lineItemA->getId() => 'foo', $lineItemB->getId() => 'bar']$collection->getPayloadsProperty('foobar'));
    }
}
/** * `getPayload()` returns the payload of this line-item. * * @return ArrayFacade Returns the payload as `ArrayFacade`. */
    public function getPayload(): ArrayFacade
    {
        return new ArrayFacade(
            $this->item->getPayload(),
            function Darray $payload): void {
                $this->item->setPayload($payload);
            }
        );
    }

    /** * `getChildren()` returns the child line-items of this line-item. * * @return ItemsFacade Returns the children as a `ItemsFacade`, that may be empty if no children exist. */
    public function getChildren(): ItemsFacade
    {
        
/** * @internal * * @covers \Shopware\Core\Checkout\Cart\Facade\ItemFacade */
class ItemFacadeTest extends TestCase
{
    public function testPublicApiAvailable(): void
    {
        $item = new LineItem('foo', 'type', 'reference', 2);
        $item->setLabel('label');
        $item->setPayload([
            'foo' => 'bar',
            'nested' => ['foo' => 'nested'],
        ]);

        $price = new CalculatedPrice(10, 10, new CalculatedTaxCollection()new TaxRuleCollection());
        $item->setPrice($price);

        $stubs = $this->createMock(ScriptPriceStubs::class);
        $helper = $this->createMock(CartFacadeHelper::class);
        $context = $this->createMock(SalesChannelContext::class);

        
$discount->setValue(50);
        $discount->setConsiderAdvancedRules(true);
        $discount->setScope(PromotionDiscountEntity::SCOPE_CART);

        $amount = 100;
        $operator = '=';

        $discountFilter = $this->getFakeRule($amount$operator);

        $discountRuleEntity = new RuleEntity();
        $discountRuleEntity->setId('foo');
        $discountRuleEntity->setPayload($discountFilter);

        $ruleCollection = new RuleCollection();
        $ruleCollection->add($discountRuleEntity);
        $discount->setDiscountRules($ruleCollection);

        $expectedRule = new OrRule();
        $expectedRule->addRule($discountFilter);

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

        static::assertInstanceOf(AbsolutePriceDefinition::class$item->getPriceDefinition());
        
/** * @param array<mixed> $data */
    public function update(LineItem $lineItem, array $data, SalesChannelContext $context): void
    {
        if (!$context->hasPermission(ProductCartProcessor::ALLOW_PRODUCT_PRICE_OVERWRITES)) {
            throw CartException::insufficientPermission();
        }

        if (isset($data['payload'])) {
            $lineItem->setPayload($data['payload'] ?? []);
        }

        if (isset($data['stackable'])) {
            $lineItem->setStackable($data['stackable']);
        }

        if (isset($data['removable'])) {
            $lineItem->setRemovable($data['removable']);
        }

        if (isset($data['label'])) {
            
Home | Imprint | This part of the site doesn't use cookies.