cancel
Showing results for 
Search instead for 
Did you mean: 

Call a function from shipping-service.js

Call a function from shipping-service.js

Hi,

 

I just found in Magento\Checkout\view\frontend\web\js\model\**bleep** change on cart page.

 

If I can, I would like to call a function in a template, such as Magento\Checkout\view\frontend\templates\cart\item\default.phtml.

function Test(){
   console.log("called");
}

Is it possible to call a function outside viewmodel?

 

I tried:

 getShippingRates: function () {
Test(); // <----------------------------added code
return shippingRates;
}

and did not work.

 

3 REPLIES 3

Re: Call a function from shipping-service.js

Hi @tvgarden,

 

You should be able to add your custom JS or to override a default component but you shouldn't be able (I guess) to change the model in that way.

Maybe you can override the original JS component? https://devdocs.magento.com/guides/v2.1/javascript-dev-guide/javascript/custom_js.html

Re: Call a function from shipping-service.js

Thank you.

 

I only want to run my own function only when postcode on cart is entered.

 

This didn't work:

 

$('input[name="postcde"]').change(function(){

console.log('called');

});

Re: Call a function from shipping-service.js

hi @Damian Culotta,

Yes I could do that way, however even though doing it it will not fix the problem the situation. My function is making an ajax call and reloading form. The problem is after this ajax call and reloading form, if a client manually reload the page, it will lose postcode data in local storage.

 

It works when I make an ajax call in a template file. 

My question is I want to call my function in a template when a client insert a postcode on cart page.

 

 $.ajax({
   type: "POST",
   url: "updatePost?postcode="+postcode,
   data: data + "&update_cart_action="+cartaction,
   success: function (data) {
             if(data.cartcontent && Number(totalsData.subtotal)>0){
             $('.cart-container form#form-validate').replaceWith(data.cartcontent);
            }
       }
});