0

I want to show products with zero quantity last in all site categories, search results, etc. But the sort order should be as follows: the products with a non-zero quantity (not in decreasing, default (or selected) order!) are displayed first, and then with zero quantity. That is, programmatically: qty>0 (not qty DESC!), qty=0. How can I do this with an catalog_product_collection_load_before event hook (observer)?

1 Answer 1

0

Here's a solution that works for me (in module Observer):

$collection = $observer->getCollection();

$collection->getSelect()
    ->joinLeft(
          array('_inventory_table'=>$collection->getTable('cataloginventory/stock_item')),
          '_inventory_table.product_id = e.entity_id',
          array('is_in_stock', 'manage_stock')
);
$collection->joinField(
    'inventory_qty',
    'cataloginventory_stock_item',
    'qty',
    'product_id=entity_id'
)->setOrder('inventory_qty', 'desc');

By default, when sorting by "position", all products with zero quantity are moved to the end of the list. But with other sorting options (by name, price), this code does not work and all products are displayed together. Can anyone help with this issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.