PriceCollection example

static::assertInstanceOf(DALEntity::class$blog);
        static::assertEquals($ids->get('blog-2')$blog->get('id'));
        static::assertEquals(1, $blog->get('position'));
        static::assertEquals(2.2, $blog->get('rating'));
        static::assertEquals('blog-2', $blog->get('title'));
        static::assertEquals('Test <123>', $blog->get('content'));
        static::assertTrue($blog->get('display'));
        static::assertEquals(['foo' => 'Bar']$blog->get('payload'));
        static::assertEquals('test@test.com', $blog->get('email'));
        static::assertInstanceOf(\DateTimeImmutable::class$blog->get('myDate'));
        static::assertEquals(new PriceCollection([new Price(Defaults::CURRENCY, 10, 10, false)])$blog->get('price'));

        static::assertInstanceOf(ArrayEntity::class$blog);
        static::assertEquals($ids->get('blog-2')$blog->getId());
        static::assertInstanceOf(ProductEntity::class$blog->get('topSellerCascade'));
        static::assertInstanceOf(ProductEntity::class$blog->get('topSellerSetNull'));
        static::assertInstanceOf(ProductEntity::class$blog->get('linkProductCascade'));
        static::assertInstanceOf(ProductEntity::class$blog->get('linkProductSetNull'));
        static::assertCount(2, $blog->get('products'));
        static::assertCount(2, $blog->get('comments'));
        static::assertCount(2, $blog->get('linksSetNull'));
    }

    

    }

    private function calculateDelivery(CartDataCollection $data, Cart $cart, Delivery $delivery, SalesChannelContext $context): void
    {
        $costs = null;
        $manualShippingCost = $cart->getExtension(DeliveryProcessor::MANUAL_SHIPPING_COSTS);
        $manualShippingCost = $manualShippingCost instanceof CalculatedPrice ? $manualShippingCost : null;
        if ($delivery->getShippingCosts()->getUnitPrice() > 0 || $manualShippingCost) {
            $costs = $this->calculateShippingCosts(
                $delivery->getShippingMethod(),
                new PriceCollection([
                    new Price(
                        Defaults::CURRENCY,
                        $delivery->getShippingCosts()->getTotalPrice(),
                        $delivery->getShippingCosts()->getTotalPrice(),
                        false
                    ),
                ]),
                $delivery->getPositions()->getLineItems(),
                $context,
                $manualShippingCost
            );

            
return new PercentageCalculation(
            -10,
            new CalculatedPrice(
                -10.4,
                -10.4,
                new CalculatedTaxCollection([
                    new CalculatedTax(-1.80, 21, -10.4),
                ]),
                new TaxRuleCollection([new TaxRule(21)])
            ),
            new PriceCollection([$price])
        );
    }

    private static function getFiftyPercentageCalculation(): PercentageCalculation
    {
        $calculator = self::createQuantityPriceCalculator();

        $priceDefinition = new QuantityPriceDefinition(100.00, new TaxRuleCollection([new TaxRule(20, 100)]), 5);

        $price = $calculator->calculate($priceDefinition, Generator::createSalesChannelContext());
        static::assertSame(500.00, $price->getTotalPrice());
        

    public function testChange(string $currencyKey, string $taxState, float $unit, float $tax): void
    {
        $ids = new IdsCollection([
            'default' => Defaults::CURRENCY,
            'usd' => Uuid::randomHex(),
        ]);

        $price = $this->rampUpPriceFacade($ids$currencyKey$taxState);

        $update = new PriceCollection([
            new Price(Defaults::CURRENCY, 2, 5, false),
            new Price($ids->get('usd'), 1, 4, false),
        ]);

        $price->change($update);

        static::assertEquals($unit$price->getUnit());
        static::assertEquals($tax$price->getTaxes()->getAmount());
    }

    public function testChangeWithPriceFacade(): void
    {
return $price;
    }

    /** * Gets the price collection of all cart items in this package. * * @throws PriceNotFoundException */
    public function getAffectedPrices(): PriceCollection
    {
        $affectedPrices = new PriceCollection();

        foreach ($this->cartItems as $lineItem) {
            if ($lineItem->getPrice() === null) {
                throw new PriceNotFoundException($lineItem);
            }

            $affectedPrices->add($lineItem->getPrice());
        }

        return $affectedPrices;
    }

    
public function testGetPricesCollectionOfMultipleItems(): void
    {
        $lineItems = new LineItemCollection([
            (new LineItem('A', 'test'))
                ->setPrice(new CalculatedPrice(200, 200, new CalculatedTaxCollection()new TaxRuleCollection())),

            (new LineItem('B', 'test'))
                ->setPrice(new CalculatedPrice(300, 300, new CalculatedTaxCollection()new TaxRuleCollection())),
        ]);

        static::assertEquals(
            new PriceCollection([
                new CalculatedPrice(200, 200, new CalculatedTaxCollection()new TaxRuleCollection()),
                new CalculatedPrice(300, 300, new CalculatedTaxCollection()new TaxRuleCollection()),
            ]),
            $lineItems->getPrices()
        );
    }

    public function testRemoveWithNoneExistingIdentifier(): void
    {
        $collection = new LineItemCollection([
            (new LineItem('A', 'test', null, 3))->assign(['uniqueIdentifier' => 'A']),
            (

#[Package('checkout')] class AbsoluteItem extends LineItem
{
    public function __construct(
        float $price,
        ?string $id = null
    ) {
        parent::__construct($id ?? Uuid::randomHex(), LineItem::DISCOUNT_LINE_ITEM);

        $this->priceDefinition = new CurrencyPriceDefinition(new PriceCollection([
            new Price(Defaults::CURRENCY, $price$price, false),
        ]));
        $this->removable = true;
    }
}
static::assertInstanceOf(ContainerFacade::class$container);

        $split = $product->take(1);
        static::assertInstanceOf(ItemFacade::class$split);
        $container->add($split);

        $split = $product->take(1);
        static::assertInstanceOf(ItemFacade::class$split);
        $container->add($split);
        $container->discount('my-discount', 'percentage', -10, 'Fanzy discount');

        $surcharge = new PriceCollection([new Price(Defaults::CURRENCY, 2, 2, false)]);
        $container->surcharge('my-surcharge', 'absolute', $surcharge, 'unit test');

        $service->items()->add($container);
        $service->calculate();

        static::assertTrue($service->has('my-container'));
        $container = $service->get('my-container');

        static::assertInstanceOf(ItemFacade::class$container);
        static::assertInstanceOf(PriceFacade::class$container->getPrice());
        static::assertEquals(182, $container->getPrice()->getTotal());
    }


        yield 'Test absolute price definition' => [
            new AbsolutePriceDefinition(20, $rule),
        ];

        yield 'Test percentage price definition' => [
            new PercentagePriceDefinition(-20, $rule),
        ];

        yield 'Test currency price definition' => [
            new CurrencyPriceDefinition(new PriceCollection([
                new Price(Defaults::CURRENCY, 100, 200, false),
                new Price(Uuid::randomHex(), 200, 300, true),
            ])$rule),
        ];

        $customFieldsRule = new LineItemCustomFieldRule(
            LineItemCustomFieldRule::OPERATOR_EQ,
            ['name' => 'foobar', 'type' => CustomFieldTypes::BOOL]
        );
        $customFieldsRule->assign([
            'selectedField' => 'foo',
            
$price = $product->get('calculatedPrice');

        static::assertInstanceOf(CalculatedPrice::class$price);

        static::assertEquals($expected$price->getTotalPrice());
    }

    public static function taxStateWillBeUsedProvider(): \Generator
    {
        $product = (new PartialEntity())->assign([
            'taxId' => Uuid::randomHex(),
            'price' => new PriceCollection([
                new Price(Defaults::CURRENCY, 10, 20, false),
            ]),
        ]);

        yield 'Gross price will be used for gross state' => [$product, CartPrice::TAX_STATE_GROSS, 20];

        yield 'Net price will be used for net price state' => [$product, CartPrice::TAX_STATE_NET, 10];

        yield 'Net price will be used for tax free state' => [$product, CartPrice::TAX_STATE_FREE, 10];
    }

    


    public function testCalculateWithOneMatchingPrice(): void
    {
        $validRuleId = Uuid::randomHex();
        $shippingMethod = new ShippingMethodEntity();
        $shippingMethod->setDeliveryTime($this->deliveryTimeEntity);
        $shippingMethod->setId(Uuid::randomHex());
        $shippingMethod->setTaxType(ShippingMethodEntity::TAX_TYPE_AUTO);
        $price = new ShippingMethodPriceEntity();
        $price->setUniqueIdentifier(Uuid::randomHex());
        $price->setCurrencyPrice(new PriceCollection([
            new Price(
                Defaults::CURRENCY,
                12,
                12,
                false
            ),
        ]));
        $price->setCalculationRuleId($validRuleId);

        $shippingMethod->setPrices(new ShippingMethodPriceCollection([$price]));

        
6,
                new CalculatedTaxCollection([
                    new CalculatedTax(-0.48, 19, -3),
                    new CalculatedTax(-0.20, 7, -3),
                ]),
                new TaxRuleCollection([
                    new TaxRule(19, 50),
                    new TaxRule(7, 50),
                ]),
                1
            ),
            new PriceCollection([$price1$price2])
        );
    }

    private static function getOneHundredPercentageDiscountCase(): AbsoluteCalculation
    {
        $calculator = self::createQuantityPriceCalculator();

        $priceDefinition = new QuantityPriceDefinition(29.00, new TaxRuleCollection([new TaxRule(17, 100)]), 10);

        $price = $calculator->calculate($priceDefinition, Generator::createSalesChannelContext());

        

class PriceFacadeTest extends TestCase
{
    public function testLineItemsGetUpdatePriceDefinition(): void
    {
        $item = new LineItem('test', 'test', 'temp');

        $original = new CalculatedPrice(1, 1, new CalculatedTaxCollection()new TaxRuleCollection());
        $price = new PriceFacade($item$original$this->createMock(ScriptPriceStubs::class)$this->createMock(SalesChannelContext::class));

        $price->change(new PriceCollection([
            new Price(Defaults::CURRENCY, 2, 2, false),
        ]));

        static::assertInstanceOf(QuantityPriceDefinition::class$item->getPriceDefinition());
        static::assertEquals(2, $item->getPriceDefinition()->getPrice());
        static::assertNull($item->getPrice());
    }

    public function testChangesAreAppliedDirectlyForEntities(): void
    {
        $item = new Entity();
        
$original = new CartPrice(
            100,
            200,
            300,
            new CalculatedTaxCollection(),
            new TaxRuleCollection(),
            CartPrice::TAX_STATE_GROSS,
            99.99
        );

        $stubs = $this->createMock(ScriptPriceStubs::class);
        $price = new PriceCollection([]);

        $stubs->method('build')->willReturn($price);

        $facade = new CartPriceFacade($original$stubs);

        static::assertEquals(100, $facade->getNet());
        static::assertEquals(200, $facade->getTotal());
        static::assertEquals(200, $facade->getRounded());
        static::assertEquals(300, $facade->getPosition());
        static::assertEquals(99.99, $facade->getRaw());

        
#[Package('buyers-experience')] class DiscountPackageCollection extends Collection
{
    /** * Gets a list of all prices within all * existing packages of this collection. * * @throws PriceNotFoundException */
    public function getAffectedPrices(): PriceCollection
    {
        $affectedPrices = new PriceCollection();

        /** @var DiscountPackage $package */
        foreach ($this->elements as $package) {
            foreach ($package->getAffectedPrices() as $price) {
                $affectedPrices->add($price);
            }
        }

        return $affectedPrices;
    }

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