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?

1
  • Check my answer and let me know if not solve. Nov 17, 2018 at 5:27

2 Answers 2

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);
6
  • 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? Nov 17, 2018 at 5:37
  • Both method is best for use but, Method 2 is more better. Nov 17, 2018 at 5:42
  • You can accept and upvote answer if this answer useful :) Nov 17, 2018 at 5:46
  • Thank you I have used method 2 and it worked better than first method Nov 17, 2018 at 6:07
  • Happy to help !! Happy Coding :) Nov 17, 2018 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 and acknowledge that you have read and understand our privacy policy and code of conduct.

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