From 659329798acdacce832d625e07f19ec3d2d15e91 Mon Sep 17 00:00:00 2001 From: Shanavas M Date: Mon, 21 Aug 2017 10:33:40 +0300 Subject: [PATCH 1/5] Fix column comments --- Setup/InstallSchema.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index 3567f4a..365bfbb 100644 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -65,14 +65,14 @@ protected function _setupTable(SchemaSetupInterface $setup, ModuleContextInterfa Table::TYPE_DECIMAL, '10,4', [], - 'From amount' + 'Fee' ) ->addColumn( 'is_pct', Table::TYPE_BOOLEAN, null, [], - 'From amount' + 'Is fee percetage?' ); $setup->getConnection()->createTable($table); From ad9358a67361051c335b87c3599713ae7fe21bb2 Mon Sep 17 00:00:00 2001 From: Shanavas M Date: Mon, 21 Aug 2017 12:04:23 +0300 Subject: [PATCH 2/5] Support region based fee customization --- Api/CashondeliveryInterface.php | 3 ++- Api/CashondeliveryTableInterface.php | 3 ++- Model/Cashondelivery.php | 5 +++-- Model/CashondeliveryTable.php | 9 +++++---- Model/ResourceModel/CashondeliveryTable.php | 12 ++++++++---- Model/Total/Quote/Cashondelivery.php | 3 ++- Model/Total/Quote/CashondeliveryTax.php | 4 +++- Setup/UpgradeSchema.php | 18 ++++++++++++++++++ etc/module.xml | 2 +- 9 files changed, 44 insertions(+), 15 deletions(-) diff --git a/Api/CashondeliveryInterface.php b/Api/CashondeliveryInterface.php index 9e475d4..1e8d94b 100644 --- a/Api/CashondeliveryInterface.php +++ b/Api/CashondeliveryInterface.php @@ -31,9 +31,10 @@ interface CashondeliveryInterface * Get base amount * @param array $totals * @param string $country + * @param string $region * @return double */ - public function getBaseAmount(array $totals, $country); + public function getBaseAmount(array $totals, $country, $region); /** * Get base tax amount diff --git a/Api/CashondeliveryTableInterface.php b/Api/CashondeliveryTableInterface.php index 4982e6c..f220af8 100644 --- a/Api/CashondeliveryTableInterface.php +++ b/Api/CashondeliveryTableInterface.php @@ -32,9 +32,10 @@ interface CashondeliveryTableInterface * * @param double $amount * @param string $country + * @param string $region * @return double */ - public function getFee($amount, $country); + public function getFee($amount, $country, $region); /** * Get table as array diff --git a/Model/Cashondelivery.php b/Model/Cashondelivery.php index cf1a608..428c68c 100644 --- a/Model/Cashondelivery.php +++ b/Model/Cashondelivery.php @@ -106,12 +106,13 @@ public function getCalcBase(array $totals) * Get base amount * @param array $totals * @param string $country + * @param string $region * @return double */ - public function getBaseAmount(array $totals, $country) + public function getBaseAmount(array $totals, $country, $region) { $calcBase = $this->getCalcBase($totals); - return $this->cashondeliveryTableInterface->getFee($calcBase, $country); + return $this->cashondeliveryTableInterface->getFee($calcBase, $country, $region); } /** diff --git a/Model/CashondeliveryTable.php b/Model/CashondeliveryTable.php index c1d3729..ac0e1a7 100644 --- a/Model/CashondeliveryTable.php +++ b/Model/CashondeliveryTable.php @@ -34,7 +34,7 @@ class CashondeliveryTable extends AbstractModel implements CashondeliveryTableIn protected $filesystem; protected $file; - protected $_columns = ['country', 'from_amount', 'fee', 'website']; + protected $_columns = ['country', 'region', 'from_amount', 'fee', 'website']; public function __construct( \Magento\Framework\Model\Context $context, @@ -62,11 +62,12 @@ protected function _construct() * * @param double $amount * @param string $country + * @param string $region * @return double */ - public function getFee($amount, $country) + public function getFee($amount, $country, $region) { - return $this->_getResource()->getFee($amount, $country); + return $this->_getResource()->getFee($amount, $country, $region); } /** @@ -126,7 +127,7 @@ public function saveFromFile($fileName) $stream = $tmpDirectory->openFile($path); $headers = $stream->readCsv(); - if ($headers === false || count($headers) < 3) { + if ($headers === false || count($headers) < 4) { $stream->close(); throw new LocalizedException(__('Invalid columns count.')); } diff --git a/Model/ResourceModel/CashondeliveryTable.php b/Model/ResourceModel/CashondeliveryTable.php index d273afe..da0c534 100644 --- a/Model/ResourceModel/CashondeliveryTable.php +++ b/Model/ResourceModel/CashondeliveryTable.php @@ -46,9 +46,10 @@ protected function _construct() * * @param double $amount * @param string $country + * @param string $region * @return double */ - public function getFee($amount, $country) + public function getFee($amount, $country, $region) { $table = $this->getMainTable(); @@ -62,16 +63,19 @@ public function getFee($amount, $country) .'country = '.$connection->quote($country).' OR ' .'country = '.$connection->quote('*') .') AND (' - .'from_amount < '.doubleval($amount) . ' AND ' - .'(' + .'region = '.$connection->quote($region).' OR ' + .'region = '.$connection->quote('*') + .') AND (' + .'from_amount < '.doubleval($amount).' AND (' .'website = '.$connection->quote($currentWebsite).' OR ' .'website = '.$connection->quote('*') - .')' + .')' .')' ) ->order('from_amount desc') ->order(new \Zend_Db_Expr("website = '*'")) ->order(new \Zend_Db_Expr("country = '*'")) + ->order(new \Zend_Db_Expr("region = '*'")) ->limit(1); $row = $connection->fetchRow($qry); diff --git a/Model/Total/Quote/Cashondelivery.php b/Model/Total/Quote/Cashondelivery.php index b196e7e..4bc6213 100644 --- a/Model/Total/Quote/Cashondelivery.php +++ b/Model/Total/Quote/Cashondelivery.php @@ -57,8 +57,9 @@ public function collect( } $country = $quote->getShippingAddress()->getCountryModel()->getData('iso2_code'); + $region = $quote->getShippingAddress()->getRegion(); - $baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country); + $baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country, $region); $amount = $this->priceCurrencyInterface->convert($baseAmount); if ($this->_canApplyTotal($quote)) { diff --git a/Model/Total/Quote/CashondeliveryTax.php b/Model/Total/Quote/CashondeliveryTax.php index 89f5110..16a3e31 100644 --- a/Model/Total/Quote/CashondeliveryTax.php +++ b/Model/Total/Quote/CashondeliveryTax.php @@ -55,7 +55,9 @@ public function collect( } $country = $quote->getShippingAddress()->getCountryModel()->getData('iso2_code'); - $baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country); + $region = $quote->getShippingAddress()->getRegion(); + + $baseAmount = $this->cashOnDeliveryInterface->getBaseAmount($total->getAllBaseTotalAmounts(), $country, $region); $baseTaxAmount = $this->cashOnDeliveryInterface->getBaseTaxAmount($baseAmount); $taxAmount = $this->priceCurrencyInterface->convert($baseTaxAmount); diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 6a93257..9c91bc4 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -22,6 +22,20 @@ protected function upgradeTo110(SchemaSetupInterface $setup) ]); } + protected function addRegionColumn(SchemaSetupInterface $setup) + { + $tableName = $setup->getTable('msp_cashondelivery_table'); + $setup->getConnection()->addColumn($tableName, 'region', [ + 'type' => Table::TYPE_TEXT, + 'nullable' => false, + 'comment' => 'Region ID', + ]); + + $setup->getConnection()->update($tableName, [ + 'region' => '*', + ]); + } + /** * Upgrades DB schema for a module * @@ -37,6 +51,10 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con $this->upgradeTo110($setup); } + if (version_compare($context->getVersion(), '1.1.9') < 0) { + $this->addRegionColumn($setup); + } + $setup->endSetup(); } } diff --git a/etc/module.xml b/etc/module.xml index e04684b..d30a63e 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -21,7 +21,7 @@ --> - + From 072c0abd1030c478e3370bd517dc062e0f0f883b Mon Sep 17 00:00:00 2001 From: Shanavas M Date: Mon, 21 Aug 2017 12:32:05 +0300 Subject: [PATCH 3/5] Hide label if extra fee is zero --- Model/CashondeliveryCart.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Model/CashondeliveryCart.php b/Model/CashondeliveryCart.php index 27e5f58..20d5466 100644 --- a/Model/CashondeliveryCart.php +++ b/Model/CashondeliveryCart.php @@ -52,7 +52,7 @@ protected function getQuote() $this->quote = $this->checkoutSession->getQuote(); $this->quote->collectTotals(); } - + return $this->quote; } @@ -83,6 +83,11 @@ public function getFeeLabel() $amount = $this->getAmount(); $taxAmount = $this->getTaxAmount(); + // Need not display label if extra fee is zero + if ($amount == 0 && $taxAmount == 0) { + return ''; + } + return __('You will be charged by an extra fee of %1 (+%2 taxes)', [ $this->priceCurrencyInterface->format($amount), $this->priceCurrencyInterface->format($taxAmount), From 327771ae5e28dfa3f2498606f53633c5a46a0a3f Mon Sep 17 00:00:00 2001 From: Shanavas M Date: Mon, 21 Aug 2017 12:37:54 +0300 Subject: [PATCH 4/5] Update Readme --- Model/CashondeliveryTable.php | 2 +- README.md | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Model/CashondeliveryTable.php b/Model/CashondeliveryTable.php index ac0e1a7..dcb7018 100644 --- a/Model/CashondeliveryTable.php +++ b/Model/CashondeliveryTable.php @@ -127,7 +127,7 @@ public function saveFromFile($fileName) $stream = $tmpDirectory->openFile($path); $headers = $stream->readCsv(); - if ($headers === false || count($headers) < 4) { + if ($headers === false || count($headers) < 5) { $stream->close(); throw new LocalizedException(__('Invalid columns count.')); } diff --git a/README.md b/README.md index 008ec4c..3b3516f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Cash On Delivery for Magento 2 -This module is a Cash On Delivery implementation for Magento2 allowing you to define an additional fee based on destination country and total amount. +This module is a Cash On Delivery implementation for Magento2 allowing you to define an additional fee based on destination country, region and total amount. @@ -9,10 +9,10 @@ This module is a Cash On Delivery implementation for Magento2 allowing you to de * Multiple currencies allowed * Multi store allowed * Percent or static fee supported -* Fee per country / amount +* Fee per country / region / amount * Default fee fallback -* Multi website support -* Specific regions limitation +* Multi website support +* Exclude specific regions ## Installing in your Magento @@ -36,9 +36,10 @@ This module is a Cash On Delivery implementation for Magento2 allowing you to de ### CSV syntax -MSP Cash On Delivery CSV file syntax is really simple. You have 3 columns: **country**, **from_amount**, **fee** +MSP Cash On Delivery CSV file syntax is really simple. You have 5 columns: **country**, **region**, **from_amount**, **fee**, **website** * **country**: ISO 2 letters country code. Use * as wildcard to indicate all countries +* **region**: Region name. Use * as wildcard to indicate all regions * **from_amount**: Indicates the minimum amount to apply the additional fee * **fee**: The fee to apply (in base currency). Adding **%** after the fee indicates a percent value * **website**: Magento website code (e.g.: *base*). Use * as wildcard to indicate all websites From 901bdcbdf6666c2d95f8164572e8093198394af1 Mon Sep 17 00:00:00 2001 From: Riccardo Tempesta Date: Tue, 22 Aug 2017 16:33:03 +0200 Subject: [PATCH 5/5] Upgrade to version 1.2.0 --- Setup/UpgradeData.php | 21 +++++++++++++++++-- Setup/UpgradeSchema.php | 14 +++++-------- etc/module.xml | 2 +- view/frontend/layout/checkout_cart_index.xml | 2 +- view/frontend/layout/checkout_index_index.xml | 2 +- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php index 08594c7..c53d55a 100644 --- a/Setup/UpgradeData.php +++ b/Setup/UpgradeData.php @@ -45,7 +45,7 @@ public function __construct( * @param ModuleDataSetupInterface $setup * @return void */ - protected function upgradeTo100(ModuleDataSetupInterface $setup) + protected function upgradeTo010000(ModuleDataSetupInterface $setup) { $attributes = [ 'msp_cod_amount' => 'Cash On Delivery Amount', @@ -61,6 +61,19 @@ protected function upgradeTo100(ModuleDataSetupInterface $setup) } } + /** + * Upgrade to version 1.2.0 + * @param ModuleDataSetupInterface $setup + * @return void + */ + protected function upgradeTo010200(ModuleDataSetupInterface $setup) + { + $tableName = $setup->getTable('msp_cashondelivery_table'); + $setup->getConnection()->update($tableName, [ + 'region' => '*', + ]); + } + /** * Upgrades data for a module @@ -74,7 +87,11 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $setup->startSetup(); if (version_compare($context->getVersion(), '1.0.0') < 0) { - $this->upgradeTo100($setup); + $this->upgradeTo010000($setup); + } + + if (version_compare($context->getVersion(), '1.2.0') < 0) { + $this->upgradeTo010200($setup); } $setup->endSetup(); diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 9c91bc4..a422ea4 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -8,7 +8,7 @@ class UpgradeSchema implements UpgradeSchemaInterface { - protected function upgradeTo110(SchemaSetupInterface $setup) + protected function upgradeTo010100(SchemaSetupInterface $setup) { $tableName = $setup->getTable('msp_cashondelivery_table'); $setup->getConnection()->addColumn($tableName, 'website', [ @@ -22,7 +22,7 @@ protected function upgradeTo110(SchemaSetupInterface $setup) ]); } - protected function addRegionColumn(SchemaSetupInterface $setup) + protected function upgradeTo010200(SchemaSetupInterface $setup) { $tableName = $setup->getTable('msp_cashondelivery_table'); $setup->getConnection()->addColumn($tableName, 'region', [ @@ -30,10 +30,6 @@ protected function addRegionColumn(SchemaSetupInterface $setup) 'nullable' => false, 'comment' => 'Region ID', ]); - - $setup->getConnection()->update($tableName, [ - 'region' => '*', - ]); } /** @@ -48,11 +44,11 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con $setup->startSetup(); if (version_compare($context->getVersion(), '1.1.0') < 0) { - $this->upgradeTo110($setup); + $this->upgradeTo010100($setup); } - if (version_compare($context->getVersion(), '1.1.9') < 0) { - $this->addRegionColumn($setup); + if (version_compare($context->getVersion(), '1.2.0') < 0) { + $this->upgradeTo010200($setup); } $setup->endSetup(); diff --git a/etc/module.xml b/etc/module.xml index d30a63e..908b17c 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -21,7 +21,7 @@ --> - + diff --git a/view/frontend/layout/checkout_cart_index.xml b/view/frontend/layout/checkout_cart_index.xml index 2610bd9..60c2767 100644 --- a/view/frontend/layout/checkout_cart_index.xml +++ b/view/frontend/layout/checkout_cart_index.xml @@ -19,7 +19,7 @@ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ --> - + diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index d430747..e1afd46 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -19,7 +19,7 @@ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) */ --> - +