0

I am using Magento 1.9

I have created one custom listing page and in this page I have displayed all products. I have added below code for collection.

app/design/frontend/my_vendor/default/template/catalog/product/view/products.phtml

$_productCollection = Mage::getModel('catalog/product')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                            ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                            ->setPageSize(30);

Now I want to hide out of stock products as well So how can I achieve this using above code?

I also want to show only limited no. of products selected in limiter.

Right now I have set 30 products per page(look above code) so whenever I click on "60" from limiter it only shows 30 products per page instead of 60 product per page.

So how can I fix this?

  • Check my answer and let me know if not solve. – Rohan Hapani Nov 17 '18 at 5:27
1

Try to use this below code :

Method 1 :

$_productCollection = Mage::getModel('catalog/product')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                            ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                            ->setPageSize(30);
$_productCollection->getSelect()->join('cataloginventory_stock_status', 'cataloginventory_stock_status.product_id = e.entity_id', array('stock_status'));
$_productCollection->getSelect()->where("`cataloginventory_stock_status`.`stock_status` = 1");

Method 2 :

$_productCollection = Mage::getModel('catalog/product')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                            ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                            ->joinField('is_in_stock',
                            'cataloginventory/stock_item',
                            'is_in_stock',
                            'product_id=entity_id',
                            'is_in_stock=1',
                            '{{table}}.stock_id=1',
                            'left')
                            ->setPageSize(30);
  • which method is most effective. I have already used first method and tested one or two products and it seems to working fine. so it's 100% effective? – Chintan Kaneriya Nov 17 '18 at 5:37
  • Both method is best for use but, Method 2 is more better. – Rohan Hapani Nov 17 '18 at 5:42
  • You can accept and upvote answer if this answer useful :) – Rohan Hapani Nov 17 '18 at 5:46
  • Thank you I have used method 2 and it worked better than first method – Chintan Kaneriya Nov 17 '18 at 6:07
  • Happy to help !! Happy Coding :) – Rohan Hapani Nov 17 '18 at 6:08
0

you can use below code

$productCollection= Mage::getModel('catalog/product')->getCollection()
                            ->addAttributeToSelect('*')
                            ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                            ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                            ->setPageSize(30);


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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