Sort grouped products in Magento by “in stock” status

Share

Many of the products in our Magento catalog are grouped products. We’ve noticed that unless they’re looking for a specific variation of a specific product, they tend to just order the first of the grouped products listed on the product details page. We want to avoid customers moving on to other products (or *gasp* other web sites) to order, so we want any out of stock products moved to the bottom of the grouped products table.

Getting Ready to Modify

First of all, this modification was done while running Magento 1.5.0.1. It may work on others, but no promises.

The collection of grouped products that gets displayed on the product details page is assembled in this file:

[magento_webroot]/app/code/core/Mage/Catalog/Model/Product/Type/Grouped.php

Before we do anything else, let’s make sure we make our changes in a way that won’t get erased next
time we update Magento. Copy everything inside the file I just referenced, and paste it inside this brand new one.
If this new file already exists, skip this step. We don’t want to undo any modifications you have
already made to this file:

[magento_webroot]/app/code/local/Mage/Catalog/Model/Product/Type/Grouped.php

Magento will read this new file any time it wants to do anything that would have used the old file. When we update Magento, the old file will get overwritten, but our new file will stay in place with all its customizations.

Down to Business

In the new file we just created (or opened up because it was already there), find the method named getAssociatedProducts.

This is where we’ll do all our work.

On about the 8th line of that method, the $collection variable is created, and the next few lines define what goes in the collection. This is how it looks by default:

$collection = $this->getAssociatedProductCollection($product)
	->addAttributeToSelect('*')
	->addFilterByRequiredOptions()
	->setPositionOrder()
	->addStoreFilter($this->getStoreFilter($product))
	->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)));

We’re going to add two lines before the addAttributeToSelect method is called.
The first line declares a join between the database table with product information and
the database table with stock information. It lets us use stock information as an attribute of product information.

->joinField('inventory_in_stock', 'cataloginventory_stock_item', 'is_in_stock',
'product_id=entity_id','is_in_stock>=0', 'left')

The second line tells Magento that we want to sort by the is_in_stock flag. That groups together items that in stock so we can display them all together.

->setOrder('inventory_in_stock','desc')

Here’s what it looks like all together:

$collection = $this->getAssociatedProductCollection($product)
	->joinField('inventory_in_stock', 'cataloginventory_stock_item', 'is_in_stock',
'product_id=entity_id','is_in_stock>=0', 'left')
	->setOrder('inventory_in_stock','desc')
	->addAttributeToSelect('*')
	->addFilterByRequiredOptions()
	->setPositionOrder()
	->addStoreFilter($this->getStoreFilter($product))
	->addAttributeToFilter('status', array('in' => $this->getStatusFilters($product)));

That should do it. Give the file a save, refresh your cache (if it’s on, which it is by default)
and check the details page of a grouped product.

Comments are closed.