Skip to content

Commit a94c53b

Browse files
Update the click and collect method instance name
- update the click and collect method instance name to remove the underscore - ensures backwards-compatibility is maintained for orders already created using the older method instance name - resolves a magento core bug introduced in Magento v2.1 with regards to handling shipping methods that contain an underscore in the instance name (magento/magento2#13902) Signed-off-by: Matthew Muscat <[email protected]>
1 parent 7ad418f commit a94c53b

File tree

7 files changed

+73
-18
lines changed

7 files changed

+73
-18
lines changed

Helper/Carrier/ClickAndCollect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class ClickAndCollect extends \Shippit\Shipping\Helper\Data
2222
{
23-
const XML_PATH_SETTINGS = 'carriers/shippit_cc/';
23+
const XML_PATH_SETTINGS = 'carriers/shippitcc/';
2424

2525
/**
2626
* Return store config value for key

Helper/Data.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
class Data extends \Magento\Framework\App\Helper\AbstractHelper
2323
{
2424
const CARRIER_CODE = 'shippit';
25-
const CARRIER_CODE_CC = 'shippit_cc';
25+
const CARRIER_CODE_CC = 'shippitcc';
26+
const CARRIER_CODE_CC_LEGACY = 'shippit_cc';
2627
const XML_PATH_SETTINGS = 'shippit/general/';
2728

2829
protected $_scopeConfig;

Helper/Sync/Order.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public function getShippingMethodMapping()
7373
// Helper Methods
7474
public function getShippitShippingMethod($shippingMethod)
7575
{
76-
if (strpos($shippingMethod, self::CARRIER_CODE_CC) !== FALSE) {
76+
if (strpos($shippingMethod, self::CARRIER_CODE_CC) !== FALSE ||
77+
strpos($shippingMethod, self::CARRIER_CODE_CC_LEGACY) !== FALSE
78+
) {
7779
return 'click_and_collect';
7880
}
7981

Model/Config/Source/Shipping/Methods.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Shippit\Shipping\Model\Config\Source\Shipping;
44

5+
use Shippit\Shipping\Helper\Data as ShippitHelper;
56
use Magento\Store\Model\ScopeInterface;
67

78
class Methods implements \Magento\Framework\Option\ArrayInterface
@@ -52,8 +53,13 @@ public function toOptionArray($showPlaceholder = false, $excludeShippit = false)
5253

5354
// if the carrier is shippit, exclude
5455
// it from the returned results
55-
if ($excludeShippit && ($carrierCode == 'shippit' || $carrierCode == 'shippit_cc')
56-
) {
56+
if ($excludeShippit &&
57+
(
58+
$carrierCode == ShippitHelper::CARRIER_CODE ||
59+
$carrierCode == ShippitHelper::CARRIER_CODE_CC ||
60+
$carrierCode == ShippitHelper::CARRIER_CODE_CC_LEGACY
61+
)
62+
) {
5763
continue;
5864
}
5965

Setup/UpgradeData.php

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,39 @@
1616

1717
namespace Shippit\Shipping\Setup;
1818

19+
use Magento\Framework\App\Config\ScopeConfigInterface;
20+
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
1921
use Magento\Framework\Setup\ModuleContextInterface;
2022
use Magento\Framework\Setup\ModuleDataSetupInterface;
2123
use Magento\Framework\Setup\UpgradeDataInterface;
24+
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
2225

2326
class UpgradeData implements UpgradeDataInterface
2427
{
2528
/**
26-
* @var \Magento\Framework\App\Config\ConfigResource\ConfigInterface
29+
* @var ConfigInterface
2730
*/
2831
protected $config;
2932

3033
/**
31-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
34+
* @var ScopeConfigInterface
3235
*/
33-
protected $scopeConfig;
36+
protected $configScope;
37+
38+
/**
39+
* @var CollectionFactory
40+
*/
41+
protected $configCollectionFactory;
3442

3543
public function __construct(
36-
\Magento\Framework\App\Config\ConfigResource\ConfigInterface $config,
37-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
44+
ConfigInterface $config,
45+
ScopeConfigInterface $configScope,
46+
CollectionFactory $configCollectionFactory
3847
)
3948
{
4049
$this->config = $config;
41-
$this->scopeConfig = $scopeConfig;
50+
$this->configScope = $configScope;
51+
$this->configCollectionFactory = $configCollectionFactory;
4252
}
4353

4454
/**
@@ -54,15 +64,17 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
5464
$installer->startSetup();
5565

5666
if (version_compare($context->getVersion(), '1.1.19') < 0) {
57-
//code to upgrade to 1.1.19
5867
$this->upgrade_1119($installer);
5968
}
6069

6170
if (version_compare($context->getVersion(), '1.2.6') < 0) {
62-
//code to upgrade to 1.2.6
6371
$this->upgrade_126($installer);
6472
}
6573

74+
if (version_compare($context->getVersion(), '1.4.10') < 0) {
75+
$this->upgrade_1410($installer);
76+
}
77+
6678
$installer->endSetup();
6779
}
6880

@@ -80,7 +92,7 @@ public function upgrade_1119($installer)
8092
];
8193

8294
foreach ($configOptions as $configOptionOldKey => $configOptionNewKey) {
83-
$configOptionValue = $this->scopeConfig->getValue($configOptionOldKey);
95+
$configOptionValue = $this->configScope->getValue($configOptionOldKey);
8496

8597
if (!empty($configOptionValue)) {
8698
$this->config->saveConfig(
@@ -108,7 +120,7 @@ public function upgrade_126($installer)
108120
];
109121

110122
foreach ($configOptions as $configOptionOldKey => $configOptionNewKey) {
111-
$configOptionValue = $this->scopeConfig->getValue($configOptionOldKey);
123+
$configOptionValue = $this->configScope->getValue($configOptionOldKey);
112124

113125
if (!empty($configOptionValue)) {
114126
$this->config->saveConfig(
@@ -120,4 +132,38 @@ public function upgrade_126($installer)
120132
}
121133
}
122134
}
135+
136+
/**
137+
* Update config data to v1.4.10
138+
*/
139+
public function upgrade_1410($installer)
140+
{
141+
/**
142+
* Migrate settings data to v1.4.10
143+
* (new shipping method carrier path for click and collect)
144+
*/
145+
$configOptions = $this->configCollectionFactory
146+
->create()
147+
->addFieldToFilter('path', array('like' => 'carriers/shippit_cc/%'))
148+
->load();
149+
150+
if ($configOptions->getSize() <= 0) {
151+
return;
152+
}
153+
154+
foreach ($configOptions->getItems() as $configOption) {
155+
$newConfigPath = str_replace(
156+
'shippit_cc',
157+
'shippitcc',
158+
$configOption->getPath()
159+
);
160+
161+
$this->config->saveConfig(
162+
$newConfigPath,
163+
$configOption->getValue(),
164+
$configOption->getScope(),
165+
$configOption->getScopeId()
166+
);
167+
}
168+
}
123169
}

etc/adminhtml/system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
</field>
296296
</group>
297297

298-
<group id="shippit_cc" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
298+
<group id="shippitcc" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
299299
<label>Shippit - Click and Collect</label>
300300

301301
<field id="active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">

etc/config.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@
6969
<specificerrmsg>This shipping method is not available. To use this shipping method, please contact us.</specificerrmsg>
7070
<sort_order>10</sort_order>
7171
</shippit>
72-
<shippit_cc>
72+
<shippitcc>
7373
<model>Shippit\Shipping\Model\Carrier\ClickAndCollect</model>
7474
<active>0</active>
7575
<title>Click and Collect</title>
7676
<method/>
7777
<sallowspecific/>
7878
<specificcountry/>
7979
<sort_order>20</sort_order>
80-
</shippit_cc>
80+
</shippitcc>
8181
</carriers>
8282
</default>
8383
</config>

0 commit comments

Comments
 (0)