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/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),
diff --git a/Model/CashondeliveryTable.php b/Model/CashondeliveryTable.php
index c1d3729..dcb7018 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) < 5) {
$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/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
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);
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 6a93257..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,6 +22,16 @@ protected function upgradeTo110(SchemaSetupInterface $setup)
]);
}
+ protected function upgradeTo010200(SchemaSetupInterface $setup)
+ {
+ $tableName = $setup->getTable('msp_cashondelivery_table');
+ $setup->getConnection()->addColumn($tableName, 'region', [
+ 'type' => Table::TYPE_TEXT,
+ 'nullable' => false,
+ 'comment' => 'Region ID',
+ ]);
+ }
+
/**
* Upgrades DB schema for a module
*
@@ -34,7 +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.2.0') < 0) {
+ $this->upgradeTo010200($setup);
}
$setup->endSetup();
diff --git a/etc/module.xml b/etc/module.xml
index e04684b..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)
*/
-->
-
+