0

I am using below code in my custom phtml

$object =  \Magento\Framework\App\ObjectManager::getInstance();
$cart = $object->get('Magento\Checkout\Model\Session')->getQuote()->getAllVisibleItems();
$cartCount = count( $cart );
if($cartCount > 0){
    echo $cartCount;
    } else{
        echo "0" ;
    }
    ?>   

It return correct count only cart page rest all page it return 0 only

  • this may be because your header block caching is true – Mohammad Mujassam May 24 '17 at 6:00
  • HOw i can fix this – Magento 2 May 24 '17 at 6:04
  • In which block are u using this..? – Mohammad Mujassam May 24 '17 at 6:16
  • In logo.phtml i added this code – Magento 2 May 24 '17 at 6:18
1

you need to override the logo block in default.xml, if you go to core module Magento/Theme/view/frontend/layout/default.xml there you have container called header-wrapper inside that logo block is there, override this block in you default.xml file.

Logo block should look like below

            <block class="Magento\Theme\Block\Html\Header\Logo" name="logo" cacheable="false">
                <arguments>
                    <argument name="logo_img_width" xsi:type="number">189</argument>
                    <argument name="logo_img_height" xsi:type="number">64</argument>
                </arguments>
            </block>

In you default.xml (module/theme) you need to put this code.

<referenceContainer name="header.container"> <container name="header-wrapper" label="Page Header" as="header-wrapper" htmlTag="div" htmlClass="header content"> <block class="Magento\Theme\Block\Html\Header\Logo" name="logo" cacheable="false"> <arguments> <argument name="logo_img_width" xsi:type="number">189</argument> <argument name="logo_img_height" xsi:type="number">64</argument> </arguments> </block> </container> </referenceContainer>

| improve this answer | |
  • I call this way <referenceBlock name="logo"> <arguments> <argument name="logo_file" xsi:type="string">images/logo-big.png</argument> <argument name="logo_img_width" xsi:type="number">100</argument> <argument name="logo_img_height" xsi:type="number">100</argument> </arguments> </referenceBlock> – Magento 2 May 24 '17 at 6:42
  • How i will add here – Magento 2 May 24 '17 at 6:42
  • see my updated code – Mohammad Mujassam May 24 '17 at 6:48

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.