0

I am using product collection in my custom phtml file I try to add filter only show in stock product but it will not work

My code :

$currentCategory = Mage::registry('current_category');
$catId =47;
$last = date('Y-m-d', strtotime('+1 days'));
$first = date('Y-m-d', strtotime('-365 days'));
$collection = Mage::getModel('catalog/category')->load($catId)
             ->getProductCollection()
             ->addAttributeToSelect('*') // add all attributes - optional
             ->addAttributeToFilter('status', 1) // enabled
             ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
             ->setPageSize(8)
             ->addAttributeToFilter('updated_at', array(
                        'from' => $first,
                        'to' => $last,
                        'date' => true,
                        )); 
$_helper = $this->helper('catalog/output');

 ?>
2

I can see that your code does not contain any stock filter you can use this bit of code on the collection

$oCollection = Mage::getModel('cataloginventory/stock_item')
                ->getCollection()
                ->addFieldToFilter('is_in_stock',0);
| improve this answer | |
0
Use Below Query

$productCollection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(array('name', 'image', 'price'))
                        ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

Mage::getSingleton('cataloginventory/stock')
    ->addInStockFilterToCollection($productCollection);

$this->_productCollection = $productCollection;

Refrence->Get Product Collection Containing In-Stock Products Only

| improve this answer | |
0

You have to do a collection who display products just who have One minimum quantity (1) and Not out of stock.

$productCollection = Mage::getModel('catalog/product')
         ->getCollection()
         ->addAttributeToSelect('*')
         ->joinField('qty',
                     'cataloginventory/stock_item',
                     'qty',
                     'product_id=entity_id',
                     '{{table}}.is_in_stock=1',
                     'left')
         ->addAttributeToFilter('qty', array("gt" => 1))
         ->setCurPage(1) // limit to one page
         ->setPageSize(50); //limit to 50 products

    foreach($productCollection as $product) { 
        if($product->getTypeId() == 'simple')
            echo $product->getName().'<br>';
            echo $product->getProductUrl().'<br>';
            // you can get the other data ...
    }
| improve this answer | |
  • @Ak Jh Please accept the answer that you found helful – PЯINCƏ Sep 16 '17 at 2:05
0

Try this.

You can change setting from admin panel.

For that goto your admin panel and System -> Configuration

Select Inventory tab under CATALOG menu.

Expand Stock options tab and set Display Out of Stock Products to No.

This may be help.

| improve this answer | |

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