I'm trying to hide prices and "add to shopping cart" for guests.
To do that I need to check whether the customer is logged in or not. There are no predefined methods in the Catalog Widget block so I need to extend the class via preference.
I've successfully extended the block Magento\Catalog\Block\Product\ListProduct
but Magento\CatalogWidget\Block\Product\ProductsList
is trickier.
When I try it, I get main.CRITICAL: Invalid template file: 'product/widget/content/grid.phtml' in module: 'Vendor_Namespace' block's name: 'product\productslist_0'
in system.log.
Any solutions to this?
#Edit
Edit
This is the contents of the di.xml file:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\CatalogWidget\Block\Product\ProductsList" type="Vendor\Namespace\Block\Product\ProductsList" />
</config>
This is the contents of the ProductsList.php file:
<?php
namespace Vendor\Namespace\Block\Product;
class ProductsList extends \Magento\CatalogWidget\Block\Product\ProductsList
{
protected $_httpContext;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Framework\App\Http\Context $httpContext,
\Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
\Magento\CatalogWidget\Model\Rule $rule,
\Magento\Widget\Helper\Conditions $conditionsHelper,
array $data = []
) {
$this->_httpContext = $httpContext;
parent::__construct(
$context,
$productCollectionFactory,
$catalogProductVisibility,
$httpContext,
$sqlBuilder,
$rule,
$conditionsHelper,
$data
);
}
protected function _construct()
{
parent::_construct();
// Tested, but does not work
// $this->setTemplate('Vendor_Namespace::product/widget/content/grid.phtml');
}
public function isCustomerLoggedIn()
{
return $this->_httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
}
}