Magento 2 Shipping Methods – Hide If Customer Not Logged In

magento2shippingshipping-methods

I want to hide shipping method when user is not logged in in checkout page.

After selecting the city shipping methods to be shown in checkout page.

So, Is there any solution for this I have try this code :

if (!customer.isLoggedIn()) {
       self.visible(false);
}

But its not working as i want. when I put this code in my shipping.js file near about line no 123-124 inside initialize function it hides whole form includes registration form and shipping method, So I need to hide only the shipping method

Thanks

Best Answer

You can also hide shipping method with shipping.html

Override shipping.html in your module

vendor/magento/module-checkout/view/frontend/web/template/shipping.html

Now Add <!-- ko if: (isCustomerLoggedIn) --> before shipping method <li> tag and end <!-- /ko --> after </li> to check customer is loggedIn or not.

Sample:

<!-- ko if: (isCustomerLoggedIn) -->
<li id="opc-shipping_method"
class="checkout-shipping-method"
data-bind="fadeVisible: visible(), blockLoader: isLoading"
role="presentation">

<!-- Shipping Methode Html -->

</li>
<!-- /ko -->`
Related Topic