From c0414e1568940cb39bd79c23b84e451e7f068a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kobierzy=C5=84ski?= Date: Mon, 15 Nov 2021 15:04:16 +0100 Subject: [PATCH] [IHB-937] gift code for whole cart fix --- Model/Checkout.php | 4 ++-- Model/Dibs/Items.php | 57 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Model/Checkout.php b/Model/Checkout.php index b06a23bf..bb97c41b 100644 --- a/Model/Checkout.php +++ b/Model/Checkout.php @@ -129,8 +129,8 @@ public function initCheckout($reloadIfCurrencyChanged = true) $billingAddress->save(); $shippingAddress->save(); - $this->totalsCollector->collectAddressTotals($quote, $shippingAddress); - $this->totalsCollector->collectQuoteTotals($quote); +// $this->totalsCollector->collectAddressTotals($quote, $shippingAddress); +// $this->totalsCollector->collectQuoteTotals($quote); $quote->collectTotals(); $this->quoteRepository->save($quote); diff --git a/Model/Dibs/Items.php b/Model/Dibs/Items.php index ed62509d..d8ea321a 100644 --- a/Model/Dibs/Items.php +++ b/Model/Dibs/Items.php @@ -38,11 +38,16 @@ class Items protected $_itemsArray = []; protected $addCustomOptionsToItemName = null; + /** + * @var \Magento\SalesRule\Api\RuleRepositoryInterface + */ + private $ruleRepository; public function __construct( \Dibs\EasyCheckout\Helper\Data $helper, \Magento\Catalog\Helper\Product\Configuration $productConfig, - \Magento\Tax\Model\Calculation $calculationTool + \Magento\Tax\Model\Calculation $calculationTool, + \Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository ) { $this->_helper = $helper; $this->_productConfig = $productConfig; @@ -50,6 +55,7 @@ public function __construct( // resets all values $this->init(); + $this->ruleRepository = $ruleRepository; } public function init($store = null) @@ -478,6 +484,48 @@ public function addDiscounts($couponCode) return $this; } + /** + * Check if discount was applied for whole cart + * + * @param Quote $quote + */ + private function addDiscountByCartRule(Quote $quote) : void + { + $ruleIds = $quote->getAppliedRuleIds(); + if (!$ruleIds) { + return; + } + + foreach (explode(',', $ruleIds) as $ruleId) { + try { + $rule = $this->ruleRepository->getById($ruleId); + } catch (\Exception $e) { + continue; + } + + if ($rule->getSimpleAction() != 'cart_fixed') { + continue; + } + + $discount = $quote->getSubtotal() - $quote->getSubtotalWithDiscount(); + $discount = $this->addZeroes($discount); + $orderItem = new OrderItem(); + $reference = $rule->getName(); + $orderItem + ->setReference($reference) + ->setName($quote->getCouponCode() ? (string)__('Discount (%1)', $quote->getCouponCode()) : (string)__('Discount')) + ->setUnit("st") + ->setQuantity(1) + ->setTaxRate($this->addZeroes(0)) // the tax rate i.e 25% (2500) + ->setTaxAmount(0) // total tax amount + ->setUnitPrice(-$discount) // excl. tax price per item + ->setNetTotalAmount(-$discount) // excl. tax + ->setGrossTotalAmount(-$discount); // incl. tax + + $this->_cart[$reference] = $orderItem; + } + } + public function validateTotals($grandTotal) { //calculate Dibs total @@ -591,7 +639,12 @@ public function generateOrderItemsFromQuote(Quote $quote) $this->addShipping($shippingAddress); } - $this->addDiscounts($quote->getCouponCode()); + // If there is no discount per items, but code does exist + // it means, that discount was applied on whole cart + // @TODO: Refactor this to external model + count($this->_discounts) + ? $this->addDiscounts($quote->getCouponCode()) + : $this->addDiscountByCartRule($quote); try { $this->validateTotals($quote->getGrandTotal());