0

My current code:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->setOrder('id', 'ASC');
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('status', '1');
$collection->addAttributeToFilter('type_id', array('neq' => 'bundle'));
$collection->addAttributeToFilter('type_id', array('neq' => 'configurable'));
$collection->addAttributeToFilter('type_id', array('neq' => 'grouped'));

The problem is that I still get the child products of configurable products where the parent is disabled or not assigned to any category. So I get the child product but it is not available in the webshop.
How can I filter the attributes of the parent products?

1

Firstly, I will recommend you to use nin instead of adding 3 neq. Secondly, you can do one thing, you can add filter of product_id's having parent_id

$collection1 = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('id'); 
        ->addAttributeToFilter('status', '0');
        ->addAttributeToFilter('type_id', array('eq' => 'configurable'));

And then use these product id to filter your previous collection.

$id = [];
    foreach($collection1 as $coll){
    $id[] = $coll->getId();
    }
$collection = Mage::getModel('catalog/product')->getCollection()
        ->setOrder('id', 'ASC')
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status', '1')
        ->addAttributeToFilter('parent_id', array('nin' => $id))
        ->addAttributeToFilter('type_id', array('nin' => ['bundle', 'configurable', 'grouped']));
| improve this answer | |
  • Thanks for your answer. That looks great. But I get the error: Call to undefined method Mage_Catalog_Model_Resource_Product_Collection::getId() – alobeejay Oct 17 '17 at 9:27
  • Sorry, I missed that it will be collection object. Get all the product ids from that and then use it – Vivek Oct 17 '17 at 9:39
  • Now I get the error "Invalid attribute name: parent_id" – alobeejay Oct 17 '17 at 9:52
  • Then remove this line ->addAttributeToFilter('parent_id', array('nin' => $id)) $parentId = Mage::getModel('catalog/product_type_grouped')->getParentIdsByChild($item->getId()); // check for grouped product And filter your products while fetching them if( in_array($parentId , $id) ) – Vivek Oct 17 '17 at 9:58
  • I use $collection->getSelect()->limit() and need the correct item numbers for each request. This is why I need to filter this out while getting the collection. Doing so while fetching will result in a mismatch of the collection count and the number of products I get in the end. – alobeejay Oct 17 '17 at 10:07
0

Viveks answer brought me on the right track. The final solution is this:

$collection1 = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('id')
->addAttributeToFilter('status', '1')
->addWebsiteFilter(Mage::app()->getWebsite()->getId())
->addAttributeToFilter('type_id', array('eq' => 'configurable'));

$collection1Ids = [];
foreach($collection1 as $coll) {
    $collection1Ids[] = $coll->getId();
}

$collection = Mage::getModel('catalog/product')->getCollection()
    ->setOrder('id', 'ASC')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', '1')
    ->addWebsiteFilter(Mage::app()->getWebsite()->getId())
    ->addAttributeToFilter('type_id', array('nin' => ['bundle', 'configurable', 'grouped']));

if(!empty($collection1Ids)) {
    $collection->joinTable('catalog/product_relation', 'child_id=entity_id', array(
        'parent_id' => 'parent_id'
    ), null, 'left')
    ->addAttributeToFilter(array(array('attribute' => 'parent_id', 'null' => true), array('attribute' => 'parent_id', 'in' => $collection1Ids)));
}
| improve this answer | |
  • You can also accept your own anser ;) – sv3n Oct 20 '17 at 17:08

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.