0

I am unable to get summary count with php code when cache is enable.

I used below code.

<?php
$helper = $this->helper('\Magento\Checkout\Helper\Cart');
echo $helper->getSummaryCount();
?>
1
  • instead of $this->helper('\Magento\Checkout\Helper\Cart'); you should try it $this->helper(Magento\Checkout\Helper\Cart::class); make sure clearing cache after making this change.
    – Aman Alam
    Dec 30, 2019 at 12:53

1 Answer 1

0

You can't with cache enabled, all files aren't re-interpreted.

You should get it with javascript. You can try to add the following code in your phtml file (the following example is to show you the way) :

<script type="text/javascript">
    require([
        'Magento_Customer/js/customer-data'
    ], function (customerData) {
        'use strict';

        customerData.get('cart').subscribe(function (cartInfo) {
            console.log(cartInfo['summary_count']);
        }, this);
    });
</script>

This code will print nothing where there isn't product in cart, and cart count when there is products.
It will be triggered every times that the cart is updated.

2

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.