What is the best way to override _initProduct()
function
in:
vendor\magento\module-checkout\Controller\Cart\Add.php
I have this in my di.xml
:
<type name="Magento\Checkout\Controller\Cart\Add">
<plugin name="x_cart_add" type="X\Y\Plugin\Checkout\Controller\Cart\Add" sortOrder="1"/>
</type>
Method:
/**
* Initialize product instance from request data
*
* @return \Magento\Catalog\Model\Product|false
*/
protected function _initProduct()
{
$productId = (int)$this->getRequest()->getParam('product');
if ($productId) {
$storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
try {
return $this->productRepository->getById($productId, false, $storeId);
} catch (NoSuchEntityException $e) {
return false;
}
}
return false;
}