0

I want to add a cacheable block on the cart page. The cacheable block should be cached using the block_html cache type.

3 Answers 3

0

To add a cacheable block on the cart page in Magento 2 using the block_html cache type,

you need to define a custom block in your module that extends \Magento\Framework\View\Element\Template,

override the _construct method to include cache_lifetime and cache_tags with \Magento\Framework\App\Cache\Type\Block::CACHE_TAG,

and implement getCacheKeyInfo for unique cache key generation. Then, add your block to the checkout_cart_index.xml layout with the cacheable="true" attribute and specify the template file.

0

Try the below code I have added a sample cart Summary section.

// app/code/YourVendor/YourModule/Block/CartSummary.php

namespace YourVendor\YourModule\Block;

use Magento\Framework\View\Element\Template;

class CartSummary extends Template
{
    protected function _construct()
    {
        parent::_construct();
        $this->addData([
            'cache_lifetime' => 3600, // Cache lifetime in seconds
            'cache_tags' => [\Magento\Checkout\Block\Cart::CACHE_TAG],
        ]);
    }
}

Create or modify the layout XML file associated with the cart page to use your custom block class.

<!-- app/code/YourVendor/YourModule/view/frontend/layout/checkout_cart_index.xml -->

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="YourVendor\YourModule\Block\CartSummary" name="checkout.cart.summary" template="YourVendor_YourModule::cart/summary.phtml" cacheable="true"/>
        </referenceContainer>
    </body>
</page>

And then run below command.

php bin/magento cache:flush

Let me know if any queries.

Thanks.

0

I achieved this without creating a custom block. Here is the layout code:

Layout file: checkout_cart_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <container name="custom.container" htmlTag="div" htmlClass="custom-container" after="-">
                <block class="Magento\Framework\View\Element\Template" name="cacheable_block" template="[Namespace]_[ModuleName]::cachedblock.phtml" cacheable="true">
                    <arguments>
                        <!-- Set your desired cache lifetime in seconds -->
                        <argument name="cache_lifetime" xsi:type="number">3600</argument>
                        <argument name="cache_tags" xsi:type="array">
                            <item name="cache_tag_name" xsi:type="string">block_html</item>
                        </argument>
                    </arguments>
                </block>
            </container>
        </referenceContainer>
    </body>
</page>

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.