cancel
Showing results for 
Search instead for 
Did you mean: 

Call block function in controller

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Call block function in controller

Hello everyone,

 

I want to set product attribute value in my controller and want to call block function in my controller.

I am unable to do this as facing 500 internal error in magento 2.3.1

 

Can anyone help me in this.

Thanks

Magento Developer
Ankita Biswas
1 REPLY 1

Re: Call block function in controller

Call block in controller

public function __construct(
 ...... \Vendor\Module\Block\Blockname $blockname, 
.....)
{
 $this->blockname = $blockname; 
}
public function execute() 
{
$value=$this->blockname->blockmethod(); 

Use saveAttribute() method to save custom product attribute sample attribute

$mypro=1001; 
$productRepository=$objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface'); 
$product =$productRepository->getById($mypro);
$product->setBestPrice(0);
$product->getResource()->saveAttribute($product, 'best_price');