0

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.

2
  • you mean on listing page?
    – Knight017
    Apr 13, 2019 at 10:40
  • The product view page @Knight017 Apr 13, 2019 at 10:48

1 Answer 1

3

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
4
  • Genius Satish. Thank you so much. Can you advise how to do this without overriding core file... do I need to create a new module in app/code? Apr 13, 2019 at 11:10
  • 1
    yes, you have to create new module for that. Apr 13, 2019 at 11:11
  • 1
    @YorkieMagento check my updated comment. Apr 13, 2019 at 11:16
  • Fantastic - thank you so much, appreciate the help. Apr 13, 2019 at 11:37

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.