1

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

2 Answers 2

0

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.

1

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

quote.shippingAddress().street[0]

It's an array.

2
  • I tried above solution but its still says null.
    – J. Doe
    Nov 20, 2019 at 13:18
  • I have don't have those addresses set as shipping or billing its coming from magento backend and I have same shipping address and billing address which is not present in the list. The select shipping address droplist has around 200 addresses except for the default shipping address. I hope it makes sense. So I just want the selected address from dropdown on page loads for the first time. I am still trying to figure out if I am missing something.
    – J. Doe
    Nov 20, 2019 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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