1

Please check Below code. I have try to get customer country code on footer.phtml. I have used GeoIP plugin to get. And added this code to block. Anyone let me know that How can I get this code out of a cache.

Issue: currently all visitor get the same county due to load this block from the cache.

namespace Rokanthemes\Themeoption\Block;
    class Themeoption extends \Magento\Framework\View\Element\Template 
    {
        public $geoIpService;

        public function __construct(
            \MagePal\GeoIp\Service\GeoIpService $geoIpService,
            array $data = []
        ) {
            $this->geoIpService = $geoIpService;
            parent::__construct($context, $data);

        }
        public function ipCountry()
        {
            return $this->geoIpService->getCountry();
        }
    }
0

3 Answers 3

2

Try this:

protected function _construct()
{
    $this->addData(
        [
            'cache_lifetime' => false,
            'cache_tags' => ['Rokanthemes_Themeoption', \Magento\Store\Model\Group::CACHE_TAG],
        ]
    );
}

OR

Add below function in your block file

public function getCacheLifetime()
{
    return false;
}

OR

Set cacheable attribute false in your module layout XML file. For your block

<block class="Rokanthemes\Themeoption\Block\Themeoption" name="themeoption" cacheable="false" />
1
  • Not work. I have tried all three methods. Commented Dec 14, 2018 at 12:19
1

You can do this by setting cacheable attribute false in layout XML files.

<block class="Block\Class" name="blockname" cacheable="false" />

You can also check this link How do disable caching of custom block on product view page?

0

You can prevent a block or phtml from cache use cacheable="false" in your layout xml file.

<block cacheable="false" class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml" />

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