0

I am using an extension auto-selecting related products from the same category. However, I want to exclude the current product from that list which is not really working.

protected $_categoryFactory;
protected $_registry;
$collection = $this->getRelatedProductsCollection($result);

I am getting the current product and ID like that:

$product = $this->_registry->registry('current_product');
$productId = $product->getId();

And then I tried to exclude it like that:

$collection ->removeItemByKey($productId);
$collection ->getSelect()->orderRand();

But I still can see the current product somewhere in the list. What do I need to change to exclude the current product?

4
  • You need to filter category product collection with not filter Sep 29, 2021 at 18:29
  • Could you please explain further, I don't understand what you mean @BirenPatel Sep 30, 2021 at 10:28
  • try $collection->addAttributeToFilter('entity_id', ['neq' => $productId]);
    – jiheison
    Oct 2, 2021 at 1:53
  • @jiheison Thank you it works! If you want create an answer and I will mark it as solved. Oct 6, 2021 at 8:30

1 Answer 1

1

You can set your collection to omit the current product using addAttributeToFilter.

E.g. by adding:

$collection->addAttributeToFilter('entity_id', ['neq' => $productId]);

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.