From c8aa29dd5ebd41c08c695e6745713e16068de3ee Mon Sep 17 00:00:00 2001 From: Riccardo Tempesta Date: Thu, 17 Aug 2017 20:23:51 +0200 Subject: [PATCH] FIX issue #29 --- Model/Total/Quote/AbstractTotal.php | 21 ++++++++++++++++++++- Model/Total/Quote/Cashondelivery.php | 4 ++++ Model/Total/Quote/CashondeliveryTax.php | 3 +++ etc/module.xml | 2 +- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Model/Total/Quote/AbstractTotal.php b/Model/Total/Quote/AbstractTotal.php index dfb7eb0..e4983d6 100644 --- a/Model/Total/Quote/AbstractTotal.php +++ b/Model/Total/Quote/AbstractTotal.php @@ -20,12 +20,22 @@ namespace MSP\CashOnDelivery\Model\Total\Quote; +use Magento\Quote\Api\PaymentMethodManagementInterface; use Magento\Quote\Model\Quote\Address\Total\AbstractTotal as MageAbstractTotal; -use Magento\Quote\Model\Quote\Address\Total; use Magento\Quote\Model\Quote; abstract class AbstractTotal extends MageAbstractTotal { + /** + * @var PaymentMethodManagementInterface + */ + private $paymentMethodManagement; + + public function __construct(PaymentMethodManagementInterface $paymentMethodManagement) + { + $this->paymentMethodManagement = $paymentMethodManagement; + } + /** * Return true if can apply totals * @param Quote $quote @@ -33,6 +43,15 @@ abstract class AbstractTotal extends MageAbstractTotal */ protected function _canApplyTotal(Quote $quote) { + // FIX bug issue #29 + if (!$quote->getId()) { + return false; + } + $paymentMethodsList = $this->paymentMethodManagement->getList($quote->getId()); + if ((count($paymentMethodsList) == 1) && ($paymentMethodsList[0]->getCode() == 'msp_cashondelivery')) { + return true; + } + return ($quote->getPayment()->getMethod() == 'msp_cashondelivery'); } } diff --git a/Model/Total/Quote/Cashondelivery.php b/Model/Total/Quote/Cashondelivery.php index 5641795..b196e7e 100644 --- a/Model/Total/Quote/Cashondelivery.php +++ b/Model/Total/Quote/Cashondelivery.php @@ -21,6 +21,7 @@ namespace MSP\CashOnDelivery\Model\Total\Quote; use Magento\Framework\Pricing\PriceCurrencyInterface; +use Magento\Quote\Api\PaymentMethodManagementInterface; use Magento\Quote\Model\Quote\Address\Total; use Magento\Quote\Api\Data\ShippingAssignmentInterface; use Magento\Quote\Model\Quote; @@ -33,9 +34,12 @@ class Cashondelivery extends AbstractTotal protected $priceCurrencyInterface; public function __construct( + PaymentMethodManagementInterface $paymentMethodManagement, PriceCurrencyInterface $priceCurrencyInterface, CashondeliveryInterface $cashOnDeliveryInterface ) { + parent::__construct($paymentMethodManagement); + $this->cashOnDeliveryInterface = $cashOnDeliveryInterface; $this->priceCurrencyInterface = $priceCurrencyInterface; $this->setCode('msp_cashondelivery'); diff --git a/Model/Total/Quote/CashondeliveryTax.php b/Model/Total/Quote/CashondeliveryTax.php index b3cd053..89f5110 100644 --- a/Model/Total/Quote/CashondeliveryTax.php +++ b/Model/Total/Quote/CashondeliveryTax.php @@ -21,6 +21,7 @@ namespace MSP\CashOnDelivery\Model\Total\Quote; use Magento\Framework\Pricing\PriceCurrencyInterface; +use Magento\Quote\Api\PaymentMethodManagementInterface; use Magento\Quote\Model\Quote\Address\Total; use Magento\Quote\Api\Data\ShippingAssignmentInterface; use Magento\Quote\Model\Quote; @@ -33,9 +34,11 @@ class CashondeliveryTax extends AbstractTotal protected $priceCurrencyInterface; public function __construct( + PaymentMethodManagementInterface $paymentMethodManagement, PriceCurrencyInterface $priceCurrencyInterface, CashondeliveryInterface $cashOnDeliveryInterface ) { + parent::__construct($paymentMethodManagement); $this->cashOnDeliveryInterface = $cashOnDeliveryInterface; $this->priceCurrencyInterface = $priceCurrencyInterface; } diff --git a/etc/module.xml b/etc/module.xml index c9c83d5..e04684b 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -21,7 +21,7 @@ --> - +