2

I've got a problem with my Magento 2.1.4 store. I added recently viewed products block to the product page, but it keeps showing product I''m currently viewing as the 1st recently viewed product (example below).

Please help, I can't figure out why does it happen.

Code that adds the recently viewed product block

<referenceContainer name="content.aside">
    <block class="Magento\Reports\Block\Product\Widget\Viewed" name="related.recently.viewed"
           before="catalog.product.related" after="-"
           template="Magento_Reports::widget/viewed/content/viewed_grid.phtml">
        <argument method="setLimit">
            <argument name="limit" xsi:type="number">4</argument>
        </argument>
    </block>
</referenceContainer>

Example: Recently viewed product

  • How about your issue? – Khoa TruongDinh Apr 2 '17 at 2:03
  • I went with the idea of comparing product ID's in viewed_grid.phtml - thanks @KhoaTruongDinh – Mateusz Serotiuk Apr 3 '17 at 6:43
  • @KhoaTruongDinh slight problem that came up in testing - when there are no recently viewed products, $block->getProduct() works just fine, but when there are, this method returns null. Any ideas? – Mateusz Serotiuk Apr 3 '17 at 7:29
  • You are being on product page or not? We can get the product when being in product page. – Khoa TruongDinh Apr 3 '17 at 7:37
  • In catalog_product_view.xml I added a block with recently viewed products (template viewed_grid.phtml). Now, when I'm trying to get product in this template using the abovementioned method, there are 2 cases: if there are no recently viewed products I can get the product on which page I am, but if there are recently viewed products, then $block-getProduct() returns null – Mateusz Serotiuk Apr 3 '17 at 7:49
2

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.

  • Yeah! Finally, you found the solution. – Khoa TruongDinh Apr 10 '17 at 9:38
1

1) I think there is an easy way to exclude the current product in Recently Viewed template by checking the current product in this template. For example, copy vendor/magento/module-reports/view/frontend/templates/widget/viewed/content/viewed_grid.phtml to your custom theme. We can get the current product

$currentId = $block->getProduct()->getId()

And then, we can use if condition to check. We will not print the recently viewed product if the id is equal to the current product id.

2) Seem that we can use the Plugin with setRecentlyViewedProducts : vendor/magento/module-reports/Block/Product/Viewed.php::_toHtml()

3) Take a look: vendor/magento/module-reports/Model/Product/Index/AbstractIndex.php::getExcludeProductIds(). This method is used in vendor/magento/module-reports/Block/Product/AbstractProduct.php:: getItemsCollection(). We can try to add the current product id to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.