I am developing a module i which i override the Magento\Catalog\Block\Product\ProductList\Toolbar.php by preferece and list.phtml by catalog_category_view.xml layout.

Correct product listing data is come when i run the cache clean command. When I write cacheable="false" then product listing show correct data but i don't want to use cacheable="false" because it wil error on marketplace.

I don't understand why need cache clean on every refresh if i don't use cacheable false. what is alternative of cacheable="false".

I override the Toolbar.php by preference

<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\Module\Block\Catalog\Product\ProductList\Toolbar" />

public function setCollection($collection)
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $Resource = $objectManager->get('\Magento\Framework\App\ResourceConnection');
    $helper = $objectManager->get('\Vendor\Module\Helper\Data');

    $this->_collection = $collection;

    $limit = (int)$this->getLimit();
    if ($limit) {

    if($moduleEnable = $helper->getConfigModuleEnabled()){

        $coordinates = $helper->getlatlogSession();
        $customerCoordinates = explode(',', $coordinates);
        $customerLat = $customerCoordinates[0];
        $customerLang = $customerCoordinates[1];

        if ($this->getCurrentOrder()) {
            switch ($this->getCurrentOrder()) {
                case 'distance':
                    $data = [];
                    foreach ($collection as $key => $value) {
                        $sku = $value->getData('sku');
                        $sourceCoordinates = $helper->getSourceLatLang($sku);
                        if (count($sourceCoordinates) > 1) {
                            $sourceCoordinates = $this->getNearestfromArray($sourceCoordinates, $customerLat, $customerLang, $helper);
                        foreach($sourceCoordinates as $item){
                            $sourceLat = $item['latitude'];
                            $sourceLang = $item['longitude'];
                            $distance = $helper->calculateDistance($customerLat, $customerLang, $sourceLat, $sourceLang);
                            $data[$value->getData('entity_id')] = [$distance, $value->getData('price')];
                    $ids = implode(',', array_keys($data));
                    $logger->info('ids ' .$ids);
                    $this->_collection->getSelect()->order(array(new \Zend_Db_Expr("FIELD(e.entity_id, " . $ids . ")")));
                    // $helper->flushCache();
                    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());

        return $this;
       $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());

I craeted a custom sortby option Distance and $ids variable give me sorted product id's that is working fine,but also i created a popup for pincode through default.xml

  <link src="Vendor_Module::js/location.js"/>
    <referenceContainer name="page.wrapper">
        <block class="Magento\Framework\View\Element\Template" name="custom-block-name" template="Vendor_Module::popup.phtml" ifconfig="nearbyproducts/general/enable" />


enter image description here

When I enter the pincode page is reload by JS but request is not come in the setCollection method because maybe products are coming from cache. When I added cacheable false in default.xml it's working fine.

1 Answer 1


Best alternative of cacheable="false" is to use private content.

You can use private content, visit this link -> CLICK HERE

This is the Magento way to use private content.

And for best example you can go through this blog CLICK HERE

  • I applied the sorting on product collection in Toolbar.php and collection is sort when i clean cache. can you please suggest me how can i use section for this? Sep 22, 2022 at 9:21
  • Can you please add your code in question for more clarification? Sep 22, 2022 at 11:33
  • I have added code for more clarification, can you please provide me solution? Sep 23, 2022 at 5:25

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.