TransWikia.com

Magento 2 - How to get quote shipping method in knockout js?

Magento Asked by Fbr on November 25, 2020

In Magento 2 I try to get shipping method in this js file : vendor/magento/module-checkout/view/frontend/web/template/billing-address.html

I try this :

quote.shippingMethod()['method_code'];

And I have this error :

Uncaught TypeError: Cannot read property 'method_code' of null

But when I console.log(quote); I can see shippingMethod method code exist:
enter image description here

Do you have any idea how can I get shipping method attributes here ?

Thank you

EDIT

My goal is to change data-bind in this file : /vendor/magento/module-checkout/view/frontend/web/template/billing-address.html

<fieldset class="fieldset" data-bind="visible: !isAddressDetailsVisible()">

In order that isAddressDetailsVisible() method return false if current shipping method is "colissimo pickup".

One Answer

Try a simple way:

quote.shippingMethod().method_code

Where quote is 'Magento_Checkout/js/model/quote'

Avoiding js error, try the following way:

if (quote.shippingMethod() != undefined) {
    quote.shippingMethod().method_code
}

[Update]

Add the following code in your js class inside initialize method. Ex. view/frontend/web/js/view/billing-address.js initialize

quote.shippingMethod.subscribe(function (method) {
    if (method.method_code == 'your_method_code') {
        this.isAddressDetailsVisible(true);
    }
}, this);

Answered by Sohel Rana on November 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP