cancel
Showing results for 
Search instead for 
Did you mean: 

not able to remove cacheable false blocks

SOLVED

not able to remove cacheable false blocks

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

1 ACCEPTED SOLUTION

Accepted Solutions

Re: not able to remove cacheable false blocks

@mikesam202abc5 ,

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 Smiley Happy 

View solution in original post

3 REPLIES 3

Re: not able to remove cacheable false blocks

@mikesam202abc5 ,

 

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

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.

Re: not able to remove cacheable false blocks

@mikesam202abc5 ,

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 Smiley Happy