Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Gene\SampleProductsCartRule\Model\Rule\Action;
- /**
- * This plugs in to \Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory
- * and implements the Magento\SalesRule\Model\Rule\Action\Discount\DiscountInterface
- */
- class SampleItems extends \Magento\SalesRule\Model\Rule\Action\Discount\AbstractDiscount
- {
- public function __construct(
- \Magento\SalesRule\Model\Validator $validator,
- \Magento\SalesRule\Model\Rule\Action\Discount\DataFactory $discountDataFactory,
- \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
- ) {
- parent::__construct($validator, $discountDataFactory, $priceCurrency);
- }
- public function getSampleSkus($rule)
- {
- return explode(",", $rule->getProductIds());
- }
- /**
- * The below is run for each line item in the cart once
- * it meets the salesrule criteria as set in the admin.
- *
- * The problem is that it is not run when a change is made in the cart
- * and the salesrule is removed from the quote, so there is no
- * clean place to reset the custom price.
- */
- public function calculate($rule, $item, $qty)
- {
- $discountData = $this->discountFactory->create();
- if (!in_array($item->getSku(), $this->getSampleSkus($rule))) {
- return $discountData;
- }
- $fixedPrice = $rule->getDiscountAmount();
- $item->setCustomPrice($fixedPrice);
- return $discountData;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement