In Magento v1.9.0.1, there isn’t a direct built-in method to show products in a specific order like “by name ascending” or “by price descending”. The platform is designed for flexibility but assumes that the sorting options (e.g., popularity, newness) are sufficient.
However, you can achieve custom product sorting by creating a module and overriding the core product collection logic. This involves modifying the way products are retrieved from the database before they are displayed on the frontend.
Here’s an overview of how to implement this:
- Create a new Magento extension or module.
- In the
app/code/local/YourCompany/Module/etc/config.xmlfile, define a dependency on the catalog module:
<config>
<modules>
<YourCompany_Module>
<active>true</active>
<codePool>local</codePool>
</YourCompany_Module>
</modules>
<global>
<helpers>
<YourCompany_Module>
<class>YourCompany_Module_Helper</class>
</YourCompany_Module>
</helpers>
</global>
</config>
- Create the helper file
app/code/local/YourCompany/Module/Helper/Data.php:
class YourCompany_Module_Helper_Data extends Mage_Core_Helper_Abstract {
public function getSortedProductCollection($category_id) {
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('name', 'price'))
->setStoreId(Mage::app()->getStore()->getId())
->joinField('category_id', 'catalog/category_product', 'category_id', 'entity_id', null, array(
'table' => 'catalog/category_product',
'condition' => "category_id=$category_id",
));
return $collection;
}
}
- Create a new block to handle the custom product collection:
<block type="core/template" as="sortedProducts" template="module/sorted_products.phtml">
<action method="setCollection">
<argument name="collection" translate="false" type="singleton">
<yourcompany_module>/helper_data->getSortedProductCollection</yourcompany_module>
</argument>
</action>
</block>
- Customize the template file
app/design/frontend/default/default/template/module/sorted_products.phtmlto display your custom collection:
<?php if (count($this->getItems())): ?>
<ul class="products-grid">
<?php foreach ($this->getCollection() as $_product): ?>
<li class="item">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<?php $_imgSize = 210; ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($_imgSize); ?>"
alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
</a>
<!-- Display product information here -->
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
- Assign the new block to a category layout file:
<block type="core/text_list" name="sorted_products">
<block type="YourCompany_Module/sortedProducts" template="module/sorted_products.phtml"/>
</block>
Note: The above example demonstrates how to retrieve products belonging to a specific category. You may need to modify the collection query or add additional filters based on your exact requirements.
To implement sorting by name, price, or other attributes, you would alter the joinField part of the Helper file accordingly. For instance:
$collection->setOrder('price', 'desc');
Remember that any customizations to Magento’s core files can make future upgrades more complex. It is recommended to use a local extension for development and then deploy it as an external package for production environments.
For further assistance with Magento development or module creation, feel free to reach out to Laravel Company, where our team of experts specialize in e-commerce solutions and custom Magento implementations.