0

I am working on product collection, my requirement is display only parent Products and Catalog Search visibility products only.

$collection = mage::getModel('catalog/product')->getCollection()->joinTable('catalog/product_relation', 'child_id=entity_id', array(
            'parent_id' => 'parent_id'
        ), null, 'left')->addAttributeToFilter(array(
            array(
                'attribute' => 'parent_id',
                'null' => null
            )
        ));
$visibility = array(
                Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
                Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
                );

               $collection->addAttributeToFilter('visibility', $visibility);

But this is not giving correct result of product collection.My all products which are present in both cases are not display in my collection.

4
  • try this magento.stackexchange.com/a/32125/18722
    – ND17
    Nov 6, 2015 at 4:49
  • I am using same example for filter parent products only this is working but when i use visibility filter then my other records also not display.
    – Mathew
    Nov 6, 2015 at 4:51
  • In collection u want parent products which gives all type of products except associated products
    – ND17
    Nov 6, 2015 at 6:31
  • I want all products but don't want any child product and "Not Visible Individually" products.
    – Mathew
    Nov 6, 2015 at 6:46

1 Answer 1

1

Use this:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('visibility', 4);
$collection->joinTable('catalog/product_relation', 'child_id=entity_id', array(
            'parent_id' => 'parent_id'
        ), null, 'left')->addAttributeToFilter(array(
            array(
                'attribute' => 'parent_id',
                'null' => null
            )
        ));
5
  • This is also not working and gives incorrect records.
    – Mathew
    Nov 6, 2015 at 4:56
  • This gives "Not Visible Individually" products also display.
    – Mathew
    Nov 6, 2015 at 5:30
  • i am just tested this is working only visible products shown and get correct data
    – ND17
    Nov 6, 2015 at 6:28
  • No this is not giving correct data.
    – Mathew
    Nov 6, 2015 at 6:48
  • This is not display all products when apply website filter with stock filter also.
    – Mathew
    Nov 6, 2015 at 6:49

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.