Skip to content

Commit

Permalink
Update custom price on rate refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Oct 23, 2018
1 parent 3426d9c commit f44c844
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
48 changes: 48 additions & 0 deletions Plugin/Model/Quote/AddressPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | support@magepal.com
*/

namespace MagePal\CustomShippingRate\Plugin\Model\Quote;

class AddressPlugin
{

/**
* @param \Magento\Quote\Api\Data\AddressInterface $subject
* @param callable $proceed
* @return mixed
*/
public function aroundCollectShippingRates(\Magento\Quote\Api\Data\AddressInterface $subject, callable $proceed)
{

$price = null;
$description = null;

//get custom shipping rate set by admin
foreach ($subject->getAllShippingRates() as $rate) {
if ($rate->getCode() == $subject->getShippingMethod()) {
$price = $rate->getPrice();
$description = $rate->getMethodTitle();
break;
}
}

$return = $proceed();

if ($price !== null) {
//reset custom shipping rate
foreach ($subject->getAllShippingRates() as $rate) {
if ($rate->getCode() == $subject->getShippingMethod()) {
$rate->setPrice($price);
$rate->setMethodTitle($description);
break;
}
}
}

return $return;
}
}
10 changes: 5 additions & 5 deletions Plugin/Quote/Address/Total/ShippingPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class ShippingPlugin
/**
* @var \MagePal\CustomShippingRate\Helper\Data
*/
protected $_customShippingRateHelper;
protected $customShippingRateHelper;

/**
* @param \MagePal\CustomShippingRate\Helper\Data $customShippingRateHelper
*/
public function __construct(
\MagePal\CustomShippingRate\Helper\Data $customShippingRateHelper
) {
$this->_customShippingRateHelper = $customShippingRateHelper;
$this->customShippingRateHelper = $customShippingRateHelper;
}

/**
Expand All @@ -50,7 +50,7 @@ public function aroundCollect(
$address = $shipping->getAddress();
$method = $address->getShippingMethod();

if (!$this->_customShippingRateHelper->isEnabled()
if (!$this->customShippingRateHelper->isEnabled()
|| $address->getAddressType() != Address::ADDRESS_TYPE_SHIPPING
|| strpos($method, Carrier::CODE) === false
) {
Expand Down Expand Up @@ -99,13 +99,13 @@ protected function updateCustomRate($address, $customShippingOption)
*/
private function getCustomShippingJsonToArray($json, $address)
{
$isJson = $this->_customShippingRateHelper->isJson($json);
$isJson = $this->customShippingRateHelper->isJson($json);

//reload exist shipping cost if custom shipping method
if ($json && !$isJson) {
$jsonToArray = [
'code' => $json,
'type' => $this->_customShippingRateHelper->getShippingCodeFromMethod($json),
'type' => $this->customShippingRateHelper->getShippingCodeFromMethod($json),
'rate' => $address->getShippingAmount()
];

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"magento 2 free shipping extension"
],
"type": "magento2-module",
"version": "1.5.0",
"version": "1.5.1",
"license": [
"proprietary"
],
Expand Down
3 changes: 3 additions & 0 deletions etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<type name="Magento\Quote\Model\Quote\Address\Total\Shipping">
<plugin name="MagePal_CustomShippingRate::ShippingPlugin" type="MagePal\CustomShippingRate\Plugin\Quote\Address\Total\ShippingPlugin" />
</type>
<type name="Magento\Quote\Api\Data\AddressInterface">
<plugin sortOrder="1" name="magePalCustomShippingRateAddress" type="MagePal\CustomShippingRate\Plugin\Model\Quote\AddressPlugin"/>
</type>
</config>

0 comments on commit f44c844

Please sign in to comment.