getAffectedPrices example


    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;
    }

    /** * Gets a list of all line item entries * that existing within all packages. */
    

    public function calculate(DiscountLineItem $discount, DiscountPackageCollection $packages, SalesChannelContext $context): DiscountCalculatorResult
    {
        /** @var AbsolutePriceDefinition $definition */
        $definition = $discount->getPriceDefinition();

        if (!$definition instanceof AbsolutePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $affectedPrices = $packages->getAffectedPrices();

        $totalOriginalSum = $affectedPrices->sum()->getTotalPrice();
        $discountValue = -min(abs($definition->getPrice())$totalOriginalSum);

        $price = $this->priceCalculator->calculate(
            $discountValue,
            $affectedPrices,
            $context
        );

        $composition = $this->getCompositionItems(
            
public function calculate(DiscountLineItem $discount, DiscountPackageCollection $packages, SalesChannelContext $context): DiscountCalculatorResult
    {
        /** @var AbsolutePriceDefinition|null $priceDefinition */
        $priceDefinition = $discount->getPriceDefinition();

        if (!$priceDefinition instanceof AbsolutePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $fixedTotalPrice = abs($priceDefinition->getPrice());

        $affectedPrices = $packages->getAffectedPrices();

        $discountDiff = $this->getTotalDiscountDiffSum($fixedTotalPrice$packages$affectedPrices);

        // now calculate the correct price         // from our collected total discount price         $discountPrice = $this->absolutePriceCalculator->calculate(
            -abs($discountDiff),
            $affectedPrices,
            $context
        );

        
/** * This test verifies that we have an empty and valid * list for new objects. * * @group promotions */
    public function testAffectedPricesOnNewObject(): void
    {
        $package = new DiscountPackage(new LineItemQuantityCollection());

        static::assertEquals(0, $package->getAffectedPrices()->count());
    }

    /** * This test verifies that our affected price function * does correctly collect the price collections from our cart items. * * @group promotions */
    public function testAffectedPricesWithCartItems(): void
    {
        $cartItems = new LineItemFlatCollection();

        
// add a reference, so we know what items are discounted                     $composition[] = new DiscountCompositionItem($lineItem->getId()$quantity$discountDiffPrice);
                }
            }
        }

        // now calculate the correct price         // from our collected total discount price         $discountPrice = $this->absolutePriceCalculator->calculate(
            -abs($totalDiscountSum),
            $packages->getAffectedPrices(),
            $context
        );

        return new DiscountCalculatorResult($discountPrice$composition);
    }
}
// now aggregate any composition items         // which might be duplicated due to separate packages         $aggregatedCompositionItems = $this->discountCompositionBuilder->aggregateCompositionItems($result->getCompositionItems());
        $result = new DiscountCalculatorResult($result->getPrice()$aggregatedCompositionItems);

        // get the cart total price => discount may never be higher than this value         $maxDiscountValue = $this->getMaxDiscountValue($calculatedCart$context);

        // if our price is larger than the max discount value,         // then use the max discount value as negative discount         if (abs($result->getPrice()->getTotalPrice()) > abs($maxDiscountValue)) {
            $result = $this->limitDiscountResult($maxDiscountValue$packages->getAffectedPrices()$result$context);
        }

        return $result;
    }

    /** * Calculates a max discount value based on current cart and customer group. * If customer is in net customer group, get the cart's net value, * otherwise use the gross value as maximum value. */
    private function getMaxDiscountValue(Cart $cart, SalesChannelContext $context): float
    {
$product1 = $this->createProductItem(29, 19);
        $product2 = $this->createProductItem(14, 19);

        $package1 = new DiscountPackage(new LineItemQuantityCollection());
        $package1->setCartItems(new LineItemFlatCollection([$product1]));

        $package2 = new DiscountPackage(new LineItemQuantityCollection());
        $package2->setCartItems(new LineItemFlatCollection([$product2]));

        $collection = new DiscountPackageCollection([$package1$package2]);

        static::assertEquals(2, $collection->getAffectedPrices()->count());
    }

    /** * This test verifies that our object collects the * line items from all existing packages. * * @group promotions */
    public function testAllLineItemsFromAllPackages(): void
    {
        $package1 = new DiscountPackage(new LineItemQuantityCollection(
            [
if (!$definition instanceof PercentagePriceDefinition) {
            throw new InvalidPriceDefinitionException($discount->getLabel()$discount->getCode());
        }

        $definedPercentage = -abs($definition->getPercentage());

        // now simply calculate the price object         // with that sum for the corresponding line items.         // we dont need to check on the actual item count in there,         // because our calculation does always go for the original cart items         // without considering any previously applied discounts.         $affectedPrices = $packages->getAffectedPrices();

        $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)) {
            
Home | Imprint | This part of the site doesn't use cookies.