Magento 1.9 How to sort product in category page by stocks product
-
13-04-2021 - |
Frage
I want the products to be displayed on the category page, if available
That is, first those that are stocks , then those that are not stocks
I didn't find anything with the title in the attribute
Is there any code?
Lösung
You need to create a simple module, say Rave_MoveOutOfStock:
app/etc/modules/Rave_MoveOutOfStock.xml
<?xml version="1.0"?>
<config>
<modules>
<Rave_MoveOutOfStock>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Catalog/>
</depends>
</Rave_MoveOutOfStock>
</modules>
</config>
app/code/local/Rave/MoveOutOfStock/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Rave_MoveOutOfStock>
<version>1.0</version>
</Rave_MoveOutOfStock>
</modules>
<global>
<models>
<moveoutofstock>
<class>Rave_MoveOutOfStock_Model</class>
</moveoutofstock>
</models>
</global>
<frontend>
<events>
<catalog_product_collection_load_before>
<observers>
<review>
<type>model</type>
<class>Rave_MoveOutOfStock_Model_Observer</class>
<method>catalogProductCollectionLoadBefore</method>
</review>
</observers>
</catalog_product_collection_load_before>
</events>
</frontend>
</config>
app/code/local/Rave/MoveOutOfStock/Model/Observer.php
<?php
class Rave_MoveOutOfStock_Model_Observer extends Varien_Event_Observer
{
public function catalogProductCollectionLoadBefore($observer)
{
$toolbar = Mage::getBlockSingleton('catalog/product_list_toolbar');
if ($toolbar) {
$products = $observer->getEvent()->getCollection();
$stockId = Mage_CatalogInventory_Model_Stock::DEFAULT_STOCK_ID;
$websiteId = Mage::app()->getStore($products->getStoreId())->getWebsiteId();
$products->getSelect()->joinLeft(
array('_inv' => $products->getResource()->getTable('cataloginventory/stock_status')),
"_inv.product_id = e.entity_id and _inv.website_id=$websiteId and _inv.stock_id=$stockId",
array('stock_status')
);
$products->addExpressionAttributeToSelect('in_stock', 'IFNULL(_inv.stock_status,0)', array());
$products->getSelect()->reset('order');
$products->getSelect()->order('in_stock DESC');
if ($toolbar->getCurrentOrder()) {
$products->addAttributeToSort($toolbar->getCurrentOrder(), $toolbar->getCurrentDirection());
}
}
return $this;
}
}
Reference: Move out of stock products to the end of the catalog product list
Andere Tipps
please check this solution for out of stock product last display :
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange