public function buildDiscountLineItem(PromotionEntity $promotion, PromotionDiscountEntity $discount, int $currencyPrecision): LineItem
    {
        //get the rules collection of discount
        /** @var RuleCollection|null $discountRuleCollection */
        $discountRuleCollection = $discount->getDiscountRules();

        // this is our target Filter that may be null if discount has no filters
        $targetFilter = null;

        // we do only need to build a target rule if user has allowed it
        // and the rule collection is not empty
        if ($discountRuleCollection instanceof RuleCollection && $discount->isConsiderAdvancedRules() && $discountRuleCollection->count() > 0) {
            $targetFilter = new OrRule();

            /** @var RuleEntity $discountRule */
            foreach ($discountRuleCollection as $discountRule) {
                /** @var Rule|string|null $rule */
                $rule = $discountRule->getPayload();

                if ($rule instanceof Rule) {
                    $targetFilter->addRule($rule);
                }
            }
        }

        // our promotion values are always negative values.
        // either type percentage or absolute needs to be negative to get
        // automatically subtracted within the calculation process
        $promotionValue = -$discount->getValue();

        switch ($discount->getType()) {
            case PromotionDiscountEntity::TYPE_ABSOLUTE:
                $promotionDefinition = new AbsolutePriceDefinition($promotionValue, $currencyPrecision, $targetFilter);
                break;

            case PromotionDiscountEntity::TYPE_PERCENTAGE:
                $promotionDefinition = new PercentagePriceDefinition($promotionValue, $currencyPrecision, $targetFilter);
                break;

            default:
                $promotionDefinition = null;
        }

        if ($promotionDefinition === null) {
            throw new \Exception('No Promotion Discount Type set');
        }

        // build our discount line item
        // and make sure it has everything as dynamic content.
        // this is necessary for the recalculation process.
        $promotionItem = new LineItem($discount->getId(), $this->lineItemType, 1);
        $promotionItem->setLabel($promotion->getName());
        $promotionItem->setDescription($promotion->getName());
        $promotionItem->setGood(false);
        $promotionItem->setRemovable(true);
        $promotionItem->setPriceDefinition($promotionDefinition);

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

        // add our lazy-validation rules.
        // this is required within the recalculation process.
        // if the requirements are not met, the calculation process
        // will remove our discount line item.
        $promotionItem->setRequirement($promotion->getPreconditionRule());

        return $promotionItem;
    }
public function buildDiscountLineItem(PromotionEntity $promotion, PromotionDiscountEntity $discount, int $currencyPrecision): LineItem
    {
        //get the rules collection of discount
        /** @var RuleCollection|null $discountRuleCollection */
        $discountRuleCollection = $discount->getDiscountRules();

        // this is our target Filter that may be null if discount has no filters
        $targetFilter = null;

        // we do only need to build a target rule if user has allowed it
        // and the rule collection is not empty
        if ($discountRuleCollection instanceof RuleCollection && $discount->isConsiderAdvancedRules() && $discountRuleCollection->count() > 0) {
            $targetFilter = new OrRule();

            /** @var RuleEntity $discountRule */
            foreach ($discountRuleCollection as $discountRule) {
                /** @var Rule|string|null $rule */
                $rule = $discountRule->getPayload();

                if ($rule instanceof Rule) {
                    $targetFilter->addRule($rule);
                }
            }
        }

        // our promotion values are always negative values.
        // either type percentage or absolute needs to be negative to get
        // automatically subtracted within the calculation process
        $promotionValue = -$discount->getValue();

        switch ($discount->getType()) {
            case PromotionDiscountEntity::TYPE_ABSOLUTE:
                $promotionDefinition = new AbsolutePriceDefinition($promotionValue, $currencyPrecision, $targetFilter);
                break;

            case PromotionDiscountEntity::TYPE_PERCENTAGE:
                $promotionDefinition = new PercentagePriceDefinition($promotionValue, $currencyPrecision, $targetFilter);
                break;

            default:
                $promotionDefinition = null;
        }

        if ($promotionDefinition === null) {
            throw new \Exception('No Promotion Discount Type set');
        }

        // build our discount line item
        // and make sure it has everything as dynamic content.
        // this is necessary for the recalculation process.
        $promotionItem = new LineItem($discount->getId(), $this->lineItemType, 1);
        $promotionItem->setLabel($promotion->getName());
        $promotionItem->setDescription($promotion->getName());
        $promotionItem->setGood(false);
        $promotionItem->setRemovable(true);
        $promotionItem->setPriceDefinition($promotionDefinition);

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

        // add our lazy-validation rules.
        // this is required within the recalculation process.
        // if the requirements are not met, the calculation process
        // will remove our discount line item.
        $promotionItem->setRequirement($promotion->getPreconditionRule());

        return $promotionItem;
    }
public function buildDiscountLineItem ( PromotionEntity $ promotion , PromotionDiscountEntity $ discount , int $ currencyPrecision ) : LineItem { //get the rules collection of discount /** @var RuleCollection|null $discountRuleCollection */ $ discountRuleCollection = $ discount -> getDiscountRules ( ) ; // this is our target Filter that may be null if discount has no filters $ targetFilter = null ; // we do only need to build a target rule if user has allowed it // and the rule collection is not empty if ( $ discountRuleCollection instanceof RuleCollection && $ discount -> isConsiderAdvancedRules ( ) && $ discountRuleCollection -> count ( ) > 0 ) { $ targetFilter = new OrRule ( ) ; /** @var RuleEntity $discountRule */ foreach ( $ discountRuleCollection as $ discountRule ) { /** @var Rule|string|null $rule */ $ rule = $ discountRule -> getPayload ( ) ; if ( $ rule instanceof Rule ) { $ targetFilter -> addRule ( $ rule ) ; } } } // our promotion values are always negative values. // either type percentage or absolute needs to be negative to get // automatically subtracted within the calculation process $ promotionValue = - $ discount -> getValue ( ) ; switch ( $ discount -> getType ( ) ) { case PromotionDiscountEntity :: TYPE_ABSOLUTE : $ promotionDefinition = new AbsolutePriceDefinition ( $ promotionValue , $ currencyPrecision , $ targetFilter ) ; break ; case PromotionDiscountEntity :: TYPE_PERCENTAGE : $ promotionDefinition = new PercentagePriceDefinition ( $ promotionValue , $ currencyPrecision , $ targetFilter ) ; break ; default : $ promotionDefinition = null ; } if ( $ promotionDefinition === null ) { throw new \ Exception ( 'No Promotion Discount Type set' ) ; } // build our discount line item // and make sure it has everything as dynamic content. // this is necessary for the recalculation process. $ promotionItem = new LineItem ( $ discount -> getId ( ) , $ this -> lineItemType , 1 ) ; $ promotionItem -> setLabel ( $ promotion -> getName ( ) ) ; $ promotionItem -> setDescription ( $ promotion -> getName ( ) ) ; $ promotionItem -> setGood ( false ) ; $ promotionItem -> setRemovable ( true ) ; $ promotionItem -> setPriceDefinition ( $ promotionDefinition ) ; // add custom content to our payload. // we need this as meta data information. $ promotionItem -> setPayload ( $ this -> buildPayload ( $ promotion ) ) ; // add our lazy-validation rules. // this is required within the recalculation process. // if the requirements are not met, the calculation process // will remove our discount line item. $ promotionItem -> setRequirement ( $ promotion -> getPreconditionRule ( ) ) ; return $ promotionItem ; }
Builds a new Line Item for the provided discount and its promotion.
It will automatically reference all provided "product" item Ids within the payload.

@throws \Shopware\Core\Checkout\Cart\Exception\InvalidPayloadException
@throws \Shopware\Core\Checkout\Cart\Exception\InvalidQuantityException
Builds a new Line Item for the provided discount and its promotion . It will automatically reference all provided product item Ids within the payload .