Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?php
  2. namespace Gene\SampleProductsCartRule\Model\Rule\Action;
  3.  
  4. /**
  5. * This plugs in to \Magento\SalesRule\Model\Rule\Action\Discount\CalculatorFactory
  6. * and implements the Magento\SalesRule\Model\Rule\Action\Discount\DiscountInterface
  7. */
  8. class SampleItems extends \Magento\SalesRule\Model\Rule\Action\Discount\AbstractDiscount
  9. {
  10. public function __construct(
  11. \Magento\SalesRule\Model\Validator $validator,
  12. \Magento\SalesRule\Model\Rule\Action\Discount\DataFactory $discountDataFactory,
  13. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  14. ) {
  15. parent::__construct($validator, $discountDataFactory, $priceCurrency);
  16. }
  17.  
  18. public function getSampleSkus($rule)
  19. {
  20. return explode(",", $rule->getProductIds());
  21. }
  22.  
  23. /**
  24. * The below is run for each line item in the cart once
  25. * it meets the salesrule criteria as set in the admin.
  26. *
  27. * The problem is that it is not run when a change is made in the cart
  28. * and the salesrule is removed from the quote, so there is no
  29. * clean place to reset the custom price.
  30. */
  31. public function calculate($rule, $item, $qty)
  32. {
  33. $discountData = $this->discountFactory->create();
  34.  
  35. if (!in_array($item->getSku(), $this->getSampleSkus($rule))) {
  36. return $discountData;
  37. }
  38.  
  39. $fixedPrice = $rule->getDiscountAmount();
  40. $item->setCustomPrice($fixedPrice);
  41.  
  42. return $discountData;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement