0

This question already has an answer here:

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');

 ?>

marked as duplicate by Jaimin Sutariya, Manashvi Birla, liyakat, Fabian Schmengler, Siarhey Uchukhlebau Sep 4 '17 at 9:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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);
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

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 ...
    }
  • @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.

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