7

I have a controller where I apply some discount on the checkout page. Now, I can see the price change just after I refresh the page.

I search for a solution, to refresh the price using a Js file.

define([
        'jquery',
        'Magento_Checkout/js/action/get-totals'
        ], function ($, getTotalsAction) {
        "use strict";

          $.ajax({
                method: "post",
                url: demoUrl, //your ajax Url
                data: {/*here post data*/},
                dataType: "html",
                success: function (resp) {
                   //below two lines refershing cart summary
                    var deferred = $.Deferred();
                    getTotalsAction([], deferred); //this function already created in Magento_Checkout/js/action/get-totals.js file
                }

            });

});

But if I insert this script in my controller, nothing happens!

Does anyone have any idea how can an update the price without refreshing the page?

I know by default Magento 2 has a function to do that. When I enter a valid coupon code the price it's updated without refreshing the page!

  • Hi. Did you achieve this? – spiil Feb 28 '18 at 13:54
  • Hi, yes, but i achieved this using jQuery – Alin Lupoiu Feb 28 '18 at 14:02
  • could you please share me your solution? – spiil Feb 28 '18 at 14:25
  • Could you please share your solution with us? – Condor Apr 11 '18 at 7:26
  • Share your ajax call file (entire js file) so i can help you. – Lucky May 7 '18 at 11:52
0

The order total summary should be reloaded after applying the custom coupon. We should add the event handle function for the custom form, for example: onclick, onchange. We can follow the logic of coupon form.

The coupon form:

enter image description here

We can find the click event handle function: vendor/magento/module-sales-rule/view/frontend/web/js/view/payment/discount.js

     apply: function() {
            if (this.validate()) {
                isLoading(true);
                setCouponCodeAction(couponCode(), isApplied, isLoading);
            }
        },

There is a note: we should use mage/storage, -> lib/web/mage/storage.js to perform asynchronous POST, GET, PUT, DELETE request to server. For example: vendor/magento/module-sales-rule/view/frontend/web/js/action/set-coupon-code.js

0

the checkoutData is stored in the localStorage of the browser and is refreshed on specific actions.

you should read the devdocs article about FPC holepunching here: http://devdocs.magento.com/guides/v2.0/config-guide/cache/cache-priv-priv.html

to refresh the checkout data object you have to define a section upgrade for your ajax call like here: https://github.com/magento/magento2/blob/develop/app/code/Magento/Checkout/etc/frontend/sections.xml#L33

note that this update (like described in the devdocs) will only trigger on POST or PUT requests.

magento should then fire a sections update request for the checkout data section right after your ajax call completed successfully

0

Use sections.xml in following location vendor/module_name/etc/frontend/sections.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="route/controller/actionname">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
</config>  
0

If u have Controller then u must have ajax function inside custom js . so in response of that ajax call use below two lines.

 var deferred = $.Deferred();
 getTotalsAction([], deferred);

And for this you must create object i.e getTotolsAction as shown in your question code inside your custom js.

Note : Don't use JS code inside controller, won't work.

0

I use this approach:

require(['Magento_Checkout/js/action/get-totals'], function(a) { a([]); } );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.