0

I have a custom shipping estimation block on the checkout cart page to calculate the shipping rate. It's working well until there are many products (5 or more) on the cart. I add the product one by one to the cart. Somehow the shipping estimation block is gone if the products counter on the cart reach 5 (sometimes 6). If I remove the new added product, the shipping estimation block is back. I found no error about it. I do it for different products and the same issue happened. It make me puzzle. Why this is happened? How to trace the origin problem?

Magento_Checkout/templates/cart/shipping.phtml:

<div id="block-summary" data-bind="scope:'block-summary'" class="content" data-role="content" aria-labelledby="block-shipping-heading">
<!-- ko template: getTemplate() --><!-- /ko -->
<script type="text/x-magento-init">
    {
        "#block-summary": {
            "Magento_Ui/js/core/app": <?php /* @escapeNotVerified */ echo $block->getJsLayout();?>
        }
    }
</script>
<script>
    window.checkoutConfig = <?php /* @escapeNotVerified */ echo \Zend_Json::encode($block->getCheckoutConfig()); ?>;
    window.customerData = window.checkoutConfig.customerData;
    window.isCustomerLoggedIn = window.checkoutConfig.isCustomerLoggedIn;
    require([
        'mage/url',
        'Magento_Ui/js/block-loader'
    ], function(url, blockLoader) {
        blockLoader("<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('images/loader-1.gif'); ?>");
        return url.setBaseUrl('<?php /* @escapeNotVerified */ echo $block->getBaseUrl();?>');
    })
</script>

Anybody know how to trace it? The shipping estimation block is not disabled or hidden. I got $block->getJsLayout() give same result no matter the shipping estimation block is displayed or not. Somehow the template shipping-estimation.html is not rendered (This form with id='shipping-zip-form' ... not rendered). it should render like this:

...
<!-- START_MSPDEV[bfdd122979ad7f283290bc9e5e1fa7b7]: checkout.cart.shipping -->
...
<div id="block-summary" data-bind="scope:'block-summary'" class="content" data-role="content" aria-labelledby="block-shipping-heading">
    <!-- ko template: getTemplate() -->
    <!-- ko foreach: {data: elems, as: 'element'} -->
    <!-- ko if: hasTemplate() -->
    <!-- ko template: getTemplate() -->
    <form method="post" id="shipping-zip-form" name="shipping-zip-form" data-bind="afterRender: loadForm">  <---FROM shipping-estimation.html
        <div class="fon-ship-method">
            <h4>SELECT DELIVERY METHOD FOR ORDER</h4>
            ...
        </div>
    </form>
    <!-- /ko -->
    <!-- /ko -->

    <!-- ko if: hasTemplate() -->
    <!-- ko template: getTemplate() -->
    <form id="co-shipping-method-form" data-bind="blockLoader: isLoading, visible: isVisible()" class="">  <---FROM shipping-rates.html
        <p class="field note" data-bind="visible: (shippingRates().length <= 0)">
            <!-- ko text: $t('Sorry, no quotes are available for this order at this time')-->Sorry, no quotes are available for this order at this time<!-- /ko -->
        </p>
        <fieldset class="fieldset rate" data-bind="visible: (shippingRates().length > 0)" style="display: none;">
            <dl class="items methods" data-bind="foreach: shippingRateGroups"></dl>
        </fieldset>
    </form>
    <!-- /ko -->
    <!-- /ko -->
    <!-- /ko -->
    <!-- /ko -->
    ...
</div>
...

I traced until the core file in Magento_Ui/js/lib/core/element/element.js:

...
getTemplate: function () {
    if(this.template.indexOf("Magento_Checkout/cart/") > -1){
        debugger;
    }
    return this.template;
},
...
hasTemplate: function () {
    if(this.template.indexOf("Magento_Checkout/cart/") > -1){
        debugger;
    }
    return !!this.template;
},
...

But template "Magento_Checkout/cart/shipping-estimation" is gone for there are many products on the cart.

0

Ok. I just realize this is my mistake. This problem nothing to do with the shipping estimation. Mistakenly I put my JavaScript that contain some ajax and events binding in Magento_Checkout/templates/cart/item/default.phtml. So if there are more products in the cart, the script would be duplicated more. Then it'd crash without notice and affect another script like shipping estimation. In my case it'd crash if there are 5 products in the cart. I moved the script to somewhere else like on Magento_Checkout/templates/cart/form.phtml. It fixed my problem.

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.