Question

In Magento 2.2 on the product view page is it possible to sort the simple products of a grouped product based on price (or any other attribute)?

At the moment they seem to be sorting randomly.

I know that you can change sort order when editing grouped product in admin panel, but this is not realistic when confronted with hundreds of products.

Was it helpful?

Solution

Override public function getAssociatedProducts($product) of this file:

vendor/magento/module-grouped-product/Model/Product/Type/Grouped.php

in this function you have to remove setPositionOrder() method on collection and have to set your required order.

public function getAssociatedProducts($product)
{
    if (!$product->hasData($this->_keyAssociatedProducts)) {
        $associatedProducts = [];

        $this->setSaleableStatus($product);

        $collection = $this->getAssociatedProductCollection(
            $product
        )->addAttributeToSelect(
            ['name', 'price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id']
        )->addFilterByRequiredOptions()->addStoreFilter(
            $this->getStoreFilter($product)
        )->addAttributeToFilter(
            'status',
            ['in' => $this->getStatusFilters($product)]
        );

        /** this is new order of collection by name **/
        $collection->setOrder('price','ASC');

        foreach ($collection as $item) {
            $associatedProducts[] = $item;
        }

        $product->setData($this->_keyAssociatedProducts, $associatedProducts);
    }
    return $product->getData($this->_keyAssociatedProducts);
}

Using module:

1) Create app/code/DSP/GroupedSimpleSort/registration.php file:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'DSP_GroupedSimpleSort',
    __DIR__
);

2) Create app/code/DSP/GroupedSimpleSort/etc/module.xml file:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="DSP_GroupedSimpleSort" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

3) Create app/code/DSP/GroupedSimpleSort/etc/frontend/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\GroupedProduct\Model\Product\Type\Grouped" type="DSP\GroupedSimpleSort\Model\Product\Type\Grouped" />
</config>

4) Create app/code/DSP/GroupedSimpleSort/Model/Product/Type/Grouped.php file:

<?php
namespace DSP\GroupedSimpleSort\Model\Product\Type;

class Grouped extends \Magento\GroupedProduct\Model\Product\Type\Grouped
{
    public function getAssociatedProducts($product)
    {
        if (!$product->hasData($this->_keyAssociatedProducts)) {
            $associatedProducts = [];

            $this->setSaleableStatus($product);

            $collection = $this->getAssociatedProductCollection(
                $product
            )->addAttributeToSelect(
                ['name', 'price', 'special_price', 'special_from_date', 'special_to_date', 'tax_class_id']
            )->addFilterByRequiredOptions()->addStoreFilter(
                $this->getStoreFilter($product)
            )->addAttributeToFilter(
                'status',
                ['in' => $this->getStatusFilters($product)]
            );

            $collection->setOrder('price','ASC');

            foreach ($collection as $item) {
                $associatedProducts[] = $item;
            }

            $product->setData($this->_keyAssociatedProducts, $associatedProducts);
        }
        return $product->getData($this->_keyAssociatedProducts);
    }
}

5) Run below commands:

php bin/magento setup:upgrade
php bin/magento cache:flush
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top