1

enter image description here

Hello

I used this event to exclude products with the same variation_group_id value from my product collection. it is showing the correct items, but the wrong way.

Any idea how to correct the following:

  • show correct items count (256 is the wrong items count for this filtered collection)
  • show correct pagination (it shows 1-9 pages for 256. 30 items/page) but we only have 22 in our case.

public function execute(
            \Magento\Framework\Event\Observer $observer
        ) {
            $collection  = $observer->getEvent()
                ->getCollection();
            $variations = [];
            foreach ($collection as $_product) {
                $variationGroupId = $_product->getVariationGroupId();
                if (!empty($variationGroupId)) {
                    if (in_array($variationGroupId, $variations)) {
                        $collection->removeItemByKey($_product->getId());
                    }
                    $variations[] =  $variationGroupId;
                }
            }
            return $collection;
        }

5
  • Can you share the $collection->getSelect() curious how the query looks like and why it is returning duplicates... Also it looks like there is more to the story, custom logic that interferes... Commented Apr 6, 2023 at 7:25
  • it does not return duplicates sku. I have products that share the same variation_group_id attribute value and I want to show only 1 of these products
    – user33629
    Commented Apr 6, 2023 at 16:11
  • It's really hard to answer your question. This page is getting product data from Elastic search or not? It's very important. The correct way is to add the filter for Elastic request. Commented Apr 12, 2023 at 5:03
  • these are category pages, I don't think so, but I'll check and update my answer.
    – user33629
    Commented Apr 12, 2023 at 13:38
  • Any update on this please? @Sergey Vlasov I am facing the same issue Commented Jun 10, 2023 at 6:53

1 Answer 1

0

you should customize the collection in the same way that it was made - SQL. Because in this case, you're removing the item of the array only.

I suppose variation_group_id it's a product attribute And in this case the possible solution might be next:

All items will be grouped by some attribute that must be unique for all items in the collection

$collection  = $observer->getEvent()
                ->getCollection();
$collection->groupByAttribute('variation_group_id');

Note The attribute must be in the float table (listed in the catalog) If it doesn't you need to join this attribute using this

$collection->joinAttribute('variation_group_id', "catalog_product/variation_group_id", 'entity_id');

Example, \Magento\CatalogWidget\Model\Rule\Condition\Product::addToCollection()

Another important thing I think you should use a plugin instead event

as an example, you can consider this one \Magento\CatalogInventory\Model\AddStockStatusToCollection::beforeLoad();

I hope this information helps you.

3
  • Thanks, Sergey, I have added $collection->groupByAttribute('variation_group_id'); to my code. Now, the page shows 1 product/page for a total of 9 items (9 pages). our catalog without this custom filter shows 30 items/page. also, the groupby must ignore products where variation_group_id is not assigned (null)
    – user33629
    Commented Apr 13, 2023 at 14:42
  • @user33629 Technically you need to group by NOT NULL and it might work like that GROUP BY IFNULL('variation_group_id', 'entity_id') $collection->getSelect()->group("IFNULL('e.variation_group_id', 'e.entity_id')"); BUT it can work if you're using a flat structure Commented Apr 14, 2023 at 17:32
  • Hi Sergey, Thanks for the update. The issue is still there with your suggested changes applied.
    – user33629
    Commented Apr 17, 2023 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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