one magento developer

my experiences about magento

Magento 2 update shipping rate at checkout

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.

    ], function ($, quote,
                 totalsDefaultProvider, rateReg) {
        var amdatepicker;

        amdatepicker = $("input[name='amdeliverydate_date']");

            amdatepicker.change(function (e) {
                var address = quote.shippingAddress();

                    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);


okay check the code.

this part reloaded the shipping method.

   rateReg.set(address.getKey(), null);
                    rateReg.set(address.getCacheKey(), null);

And this pice of code refresh the totals.


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="" 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"/>


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


     * @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) {

        return $result;

    private function getExtraPrice(){
        return $extra;

If you need more description, let me know, but I know you are brilliant Magento developer and can find it.

Leave a Reply

Your email address will not be published. Required fields are marked *.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>