I need to restrict Shipping Method options based on custom collection data in Magento 2.


I have added a condition to the below file:

<!-- ko if: ($data != "Free Shipping") -->
<!-- ko-->

But I want to add another condition:

if($customerlevel >= $targetlevel){
  # free shipping
  # no free shipping

How can I get custom collection data in knockout HTML template to put condition like above?


<form id="co-shipping-method-form" data-bind="blockLoader: isLoading, visible: isVisible()">
    <p class="field note" data-bind="visible: (shippingRates().length <= 0)">
        <!-- ko text: $t('Sorry, no quotes are available for this order at this time')--><!-- /ko -->
    <fieldset class="fieldset rate" data-bind="visible: (shippingRates().length > 0)">
        <dl class="items methods" data-bind="foreach: shippingRateGroups">
        <!-- ko if: ($data != "Free Shipping") -->
            <dt class="item-title"><span data-bind="text: $data"></span></dt>
            <dd class="item-options" data-bind="foreach: { data:$parent.getRatesForGroup($data), as: 'method' }">
                <div data-bind="css: {'field choice item': available, 'message error': !available} ">
                    <!-- ko ifnot: (available) -->
                    <div data-bind="text: error_message"></div>
                    <!-- /ko -->
                    <!-- ko if: (available) -->
                    <input type="radio"
                                click: $parents[1].selectShippingMethod,
                                checked: $parents[1].selectedShippingMethod,
                                attr: {
                                        value: carrier_code + '_' + method_code,
                                        id: 's_method_' + carrier_code + '_' + method_code
                    <label class="label" data-bind="attr: {for: 's_method_' + carrier_code + '_' + method_code}">
                        <!-- ko text: $data.method_title --><!-- /ko -->
                        <each args="element.getRegion('price')" render="" />
                    <!-- /ko -->
        <!-- /ko -->
  • based on customer collection data Magento 2. Can u explain in details?
    – Abdul
    Commented Dec 16, 2017 at 9:47
  • i was just edited question Commented Dec 16, 2017 at 10:19
  • $customerlevel & $targetlevel both customer attributes or fields?
    – Abdul
    Commented Dec 16, 2017 at 10:23
  • both are custom collection data Commented Dec 16, 2017 at 10:45
  • Maybe you will find some inspiration here: magento.stackexchange.com/questions/198444/… Commented Dec 17, 2017 at 13:12

1 Answer 1


The first thing: The template which you paste here is only part from Cart - not from chcekout.

Checkout template for shipping methods you will find here


In Checkout you have access to Quote object and to customer-data saved in localStorage.

So I thnik that you should extend a Quote about your custom attributes or add them to customer object in localStorage.

vendor/magento/module-checkout/view/frontend/web/js/model/quote.js vendor/magento/module-customer/view/frontend/web/js/customer-data.js

When you will have yours attributes in Knockout.JS, you have 2 possiblities:

  1. Better and more complicated to implement. Change the ShippingRates collections, which contains array with all shippings methods. Depends on your attribute add or remove Free Shipping from this collection.

  2. In template hide your method when your condition is true. You can use carrier_code to detect a freeshipping and implement Knockout 'if' with your custom attributes or use data-bind="visible: your_condition" on any div element

Your Answer

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

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