I want to show you how we can add an extra amount to all shipping method price. My task was added an extra amount to shipping methods for someday. My customer wants to create a new module over the Amasty delivery date to add an amount for some special dates like mother day or valentine etc.
At this tutorial, I show you how we can reload the shipping methods and totals also, how we can add an extra amount to all or special shipping method, for first reloading shipping method on checkout.
reload the shipping method on checkout
We need to do it by javascript. When date input changed, I should send a request to save date and time, after that the shipping method should be reloaded.
<script>
require([
'jquery',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/cart/totals-processor/default',
'Magento_Checkout/js/model/shipping-rate-registry',
], function ($, quote,
totalsDefaultProvider, rateReg) {
var amdatepicker;
amdatepicker = $("input[name='amdeliverydate_date']");
amdatepicker.change(function (e) {
var address = quote.shippingAddress();
$.ajax({
showLoader: true,
url: '<?=$block->getUrl('chdate/js/index')?>',
data: {
ajax: 1,
time: amtimepicker.find("option:selected").text(),
date: amdatepicker.val(),
form_key: '<?=$block->formKey->getFormKey()?>'
},
type: "POST",
dataType: 'json'
}).success(function (data) {
rateReg.set(address.getKey(), null);
rateReg.set(address.getCacheKey(), null);
quote.shippingAddress(address);
totalsDefaultProvider.estimateTotals(quote.shippingAddress());
});
});
});
</script>
okay check the code.
this part reloaded the shipping method.
rateReg.set(address.getKey(), null);
rateReg.set(address.getCacheKey(), null);
quote.shippingAddress(address);
And this pice of code refresh the totals.
totalsDefaultProvider.estimateTotals(quote.shippingAddress());
Change Shipping amount on checkout
At part two of my task, I should change the shipping amount and add an extra amount. for this, we need to create a plugin.
Note: I assume you know what is the plugin or how the require js work.
We should create a di.xml at etc directory and add below code.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\Quote\Address\Rate">
<plugin name="add_extra_amount_to_method_rate" type="ChalakSoft\AmastyDeliveryRule\Plugin\Quote\Address\Rate" disabled="false" sortOrder="3"/>
</type>
</config>
Plugin class should be something like this.
namespace ChalakSoft\AmastyDeliveryRule\Plugin\Quote\Address;
class Rate
{
private $checkoutSession;
private $helper;
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\ChalakSoft\AmastyDeliveryRule\Helper\Data $helper
){
$this->helper=$helper;
$this->checkoutSession=$checkoutSession;
}
/**
* @param \Magento\Quote\Model\Quote\Address\AbstractResult $rate
* @return \Magento\Quote\Model\Quote\Address\Rate
*/
public function afterImportShippingRate($subject, $result, $rate)
{
if ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
$result->setPrice(
$result->getPrice()+$this->getExtraPrice()
);
}
return $result;
}
private function getExtraPrice(){
$amDate=$this->checkoutSession->getDeliveryDate();
$amTime=$this->checkoutSession->getDeliveryTime();
$extra=$this->helper->getExtraAmount($amDate,$amTime);
return $extra;
}
}
If you need more description, let me know, but I know you are brilliant Magento developer and can find it.
thanks for your article update shipping rate at checkout
it help me
you are so welcome
Thank you for your article. Is it possible to run this script when the postcode changes, without the amasty dates addon?
“Yes, of course. You just need to add an observer to the address object by using a code like
quote.shippingAddress.subscribe(this.updateAddress) .”