cancel
Showing results for 
Search instead for 
Did you mean: 

Sort the Simple Products of a Grouped Product

SOLVED

Sort the Simple Products of a Grouped Product

I believe the simple products of a Grouped product are sorted by ID. I would like to sort them by name. In the file /vendor/magento/module-grouped-product/Model/Product/Type/Grouped.php there is

function getAssociatedProducts($product)

.

Would adding this work?

->addAttributeToSort('name', 'asc')

 

If so, where would I add it to not mess with core files?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Sort the Simple Products of a Grouped Product

Hello @mraccine 

 

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

need to change it

$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)]
            )->setOrder('name','ASC');

you need to create around plugin for same

 

if works then mark as solution

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

View solution in original post

2 REPLIES 2

Re: Sort the Simple Products of a Grouped Product

Hello @mraccine 

 

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

need to change it

$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)]
            )->setOrder('name','ASC');

you need to create around plugin for same

 

if works then mark as solution

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Sort the Simple Products of a Grouped Product

Thanks, that worked perfectly!