- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have some cacheable false blocks
if I remove cacheable="false" from catalog_product_view.xml and catalog_category_view.xml from my custom theme than i get error
getFinalPrice() on null on Category & Product Page
I am getting final price from the product into this block
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
you have 2 issues
1. do not use ObjectManager
2. do not use session get on catalog pages
to get the current product you can use the following code.
function getProductFinalPrice() { $product = $this->_registry->registry('current_product'); $finalPrice = number_format($product->getFinalPrice(), 2, '.', ''); return $finalPrice; }
Note :
to use $this->_registry, you need to add this class \Magento\Framework\Registry in your constructor and assign to $this->_registry as shown here
kindly Accept as a Solution and give Kudos if this works for you
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: not able to remove cacheable false blocks
if you are having issues with removing cacheable false then you must have some custom block implementation in which the product is loaded by object?
kindly provide the code in which you are having issues
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: not able to remove cacheable false blocks
i have following code into my Block
function getProductFinalPrice() { $productId = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Model\Session')->getData('last_viewed_product_id'); $product = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Model\Product')->load($productId); $finalPrice = number_format($product->getFinalPrice(), 2, '.', ''); return $finalPrice; }
further i checked that the $productId is NULL when i remove cacheable="false" from xml in 2nd page hit
1st page hit works fine after cache flush.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
you have 2 issues
1. do not use ObjectManager
2. do not use session get on catalog pages
to get the current product you can use the following code.
function getProductFinalPrice() { $product = $this->_registry->registry('current_product'); $finalPrice = number_format($product->getFinalPrice(), 2, '.', ''); return $finalPrice; }
Note :
to use $this->_registry, you need to add this class \Magento\Framework\Registry in your constructor and assign to $this->_registry as shown here
kindly Accept as a Solution and give Kudos if this works for you