Question

Hello all I am trying to get selected shipping address from dropdown(without changing the dropdown option like getting selected shipping address when page loads) or default shipping address on checkout page. On implementing my functionality in

app\design\frontend\Company\porto_base_child\Magento_Checkout\web\js\view\shipping-information-mixin.js

define(['Magento_Checkout/js/model/quote', 'jquery',
'mage/translate',], function (quote,$) {
    'use strict';

    var mixin = {

        isPickup: function() {
            var shippingMethod = quote.shippingMethod();
            if (shippingMethod === null || shippingMethod === undefined) {
                return false;
            }
            if (shippingMethod.carrier_title === null || shippingMethod.carrier_title === undefined) {
                return false;
            }
            return (shippingMethod.carrier_title === "Toronto Customer pick-up");
        }
    };

    return function (target) {
        /**Code below checks if the default shipping address or the slected shipping address has pobox in dropdwon shipping address select on Checkout page*/
        console.log(quote);
        console.log(quote.shippingAddress());
        var convertstreet = quote.shippingAddress().street.toString();
        var pattern = /((((p[\s\.]?)\s?[o\s][\.]?)\s?)|(post\s?office\s?))((box|bin|b\.?)?\s?((num|number|no|#)\.?)?\s?\d+)/igm.test(convertstreet);

        if(pattern== false)
        {
            document.getElementById('opc-shipping_method').style.display='block';
            document.getElementById('poboxmessage').style.display='none';
        }
        else{
         document.getElementById('opc-shipping_method').style.display='none';
         document.getElementById('poboxmessage').style.display='block';
         document.getElementById('poboxmessage').innerHTML=$.mage.__(document.getElementById('poboxmessage').innerHTML);
        }
         return target.extend(mixin);
    };
});

But I am getting an error saying Uncaught TypeError: Cannot read property 'street' of null on (var convertstreet = quote.shippingAddress().street.toString();).

I am not sure is this the best way to get the street address from quote but I am still trying to figure out any better solution to get the street address from either default shipping address or selected shipping address from dropdown(without changing the dropdown option like getting selected shipping address when page loads). It would be really helpful if I can get some advice or suggestion so that I can understand the concept how quote works? thanks

Was it helpful?

Solution

Late answer but might help someone else, just add the check above your code like if(quote.shippingAddress() !== null) and put your code inside it. Hopefully, It'll avoid the error you're getting.

OTHER TIPS

I have a suggestion for you, we can get street:

quote.shippingAddress().street[0]

It's an array.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top