0

We would like to check in the checkout cart if the shipping cost is 0,00 and then replace it by text: "free". Therefore I copied the file /Magento_Tax/web/template/checkout/cart/totals/shipping.html to our theme and tried the following below.

Sadly that does not seem to work and it does not display any value, so it seems that it does not load the value as numbers I guess.

How can we check the shipping cost using the knockout code and then display text?

<!-- ko if: getValue() < 1.00 -->
<td class="amount">
    <span class="shipping-free"><!-- ko i18n: 'Free' --><!-- /ko --></span>
</td>
<!-- /ko -->
<!-- ko if: getValue() > 1.00 -->
<td class="amount">
    <span class="price" data-bind="text: getValue()"></span>
</td>
<!-- /ko -->
5
  • 1
    Try console logging in the getValue function to understand what format it's returning – Jay Ghosh May 19 at 15:41
  • 1
    Or you could maybe try doing <!-- ko if: parseFloat(getValue()) < 1 --> – Jay Ghosh May 19 at 15:43
  • @JayGhosh Thanks! That's what I also tried, but sadly that does not seem to work. – JGeer May 19 at 18:43
  • 1
    @JGeer let me know if given solution works for you. Thanks! – Rahul Barot May 20 at 11:42
  • @RahulBarot Thanks! Just replied to your answer. – JGeer May 20 at 11:43
0

first of all you are working in wrong file

Magento_Tax/web/template/checkout/cart/totals/shipping.html

if you wanna change shipping rate title or text then add your code in below file in your theme

for cart

/app/design/frontend/Your/Theme/Magento_Checkout/web/template/cart/shipping-rates.html

for checkout

/app/design/frontend/Your/Theme/Magento_Checkout/web/template/shipping.html

3
  • Thanks! But that is for the shipping calculation, I need it inside the cart summary. – JGeer May 20 at 11:44
  • then override /vendor/magento/module-checkout/view/frontend/web/template/summary/shipping.html this file and add condition based on getValue() (getValue is shipping amount) – Gohil Rajesh May 20 at 12:01
  • Thanks, but that's the problem here. I want to check if the value is $ 0,00 and then replace it by text. That code will be placed also in that template file for the checkout. – JGeer May 20 at 12:26
0

Found the solution:

    <!-- ko if: totals()['shipping_amount'] < 1.00 -->
    <td class="amount">
        <span class="shipping-free"><!-- ko i18n: 'Free' --><!-- /ko --></span>
    </td>
    <!-- /ko -->
    <!-- ko if: totals()['shipping_amount'] > 1.00 -->
    <td class="amount">
        <span class="price" data-bind="text: getValue()"></span>
    </td>
    <!-- /ko -->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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