Found a solution - I'm going to share here in case someone needs it.
You need to overwrite Magento\Reports\Block\Product\Widget\Viewed
block and make sure to change this in catalog_product_view.xml
.
In the overwritten block add method:
/**
* Method returns array of recently viewed products without parent product
*
* @param array $recentlyViewedProducts
*
* @return array
*/
public function removeCurrentProductFromRecentlyViewed(array $recentlyViewedProducts)
{
$parentProductId = $this->catalogSession->getlastViewedProductId();
/** @var Product $product */
foreach ($recentlyViewedProducts as $product) {
if ((count($recentlyViewedProducts) == 1) && ($product->getId() == $parentProductId)) {
return [];
} elseif ($product->getId() == $parentProductId) {
unset($recentlyViewedProducts[$product->getId()]);
}
}
return $recentlyViewedProducts;
}
This get's current product ID and removes it from the array.
In viewed_grid.phtml
you need to add the following section in the if ($exist = ($block->getRecentlyViewedProducts() && $block->getRecentlyViewedProducts()->getSize())) ...
code:
$items = $block->getRecentlyViewedProducts();
$itemsWithoutParentProduct = $block->removeCurrentProductFromRecentlyViewed($items->getItems());
$items->removeAllItems();
$exist = false;
if (!count($itemsWithoutParentProduct) == 0) {
foreach ($itemsWithoutParentProduct as $item) {
$items->addItem($item);
}
$exist = true;
}
And it's done! Good luck everybody.
Unfortunately the accepted answer didn't work for me and I wasn't able to use widget instance layout updates in my use case
If for whatever reason you need to add a 'Recently Viewed' products block specifically via layout xml, here's my solution (tested in version 2.2.2):
I decided to use the same class Magento\Catalog\Block\Widget\RecentlyViewed
which the 'Recently Viewed' Product widget uses in my block declaration.
<!-- Basic recently viewed products block -->
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-">
<arguments>
<argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
<argument name="page_size" xsi:type="number">4</argument>
</arguments>
</block>
This will render a simple grid with the last 4 products view by the user.
You can configure the block further, just as you would the widget instance:
<!-- Configured recently viewed products block -->
<block class="Magento\Catalog\Block\Widget\RecentlyViewed" name="recently_viewed" template="Magento_Catalog::product/widget/viewed/grid.phtml" after="-">
<arguments>
<argument name="uiComponent" xsi:type="string">widget_recently_viewed</argument>
<argument name="page_size" xsi:type="number">4</argument>
<!-- 'Product attributes to show' configuration -->
<argument name="show_attributes" xsi:type="string">name,image,price,learn_more</argument>
<!-- 'Buttons to show' configuration -->
<argument name="show_buttons" xsi:type="string">add_to_cart,add_to_compare,add_to_wishlist</argument>
</arguments>
</block>
One thing to note is that the argument values for show_attributes
and show_buttons
need to be declared as comma delimited strings (no spaces).
Declaring them as arrays or including spaces next to your comma's won't work due to the way the UI Component parse the content before Knockout renders it.
And since I said Knockout... it should play nicely with FPC, should.
Finally this solution doesn't use the 'action' instruction which appears to have been deprecated
Best Answer
There is an Admin setting that will show the Recently Viewed Products for Not Logged in Customers.
Go to Admin => Stores > Settings > Configuration > Catalog > Recently Viewed/Compared Products select Yes to ( Synchronize widget products with backend storage )
Clear cache and check.