6

I need to find a way to programatically find all the bundle products that contain a particular simple product. I'm hoping there is a simple solution this problem as I have been unable to figure one out myself.

  • What -have- you tried? – Daniel Sloof Mar 13 '13 at 23:49
  • Would a MySQL query be ok? – jharrison.au Mar 14 '13 at 0:51
  • If you could post some code that you've tried then that would be helpful – seanbreeden Mar 14 '13 at 2:15
2

The following should get you started. This isn't the most performant way to do this — we're loading a collection of bundled products, getting the children IDs, and then checking those IDs for what we want. However, it should be suitable for shell scripting needs, some frontend store needs, and should give you pointers towards fetching this information in a more performant way by examining how the core methods fetch the data (i.e. which resource models, leading to which tables).

Tested in Magento CE 1.7.0.1

function findBundledProductsWithThisChildProduct($id_to_find)
{
    //grab all bundled products
    $bundles = array();
    $products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addFieldToFilter('type_id','bundle');

    //loop over bundled products
    foreach($products as $product)
    {

        //get child product IDs
        $children_ids_by_option = $product
        ->getTypeInstance($product)
        ->getChildrenIds($product->getId(),false); //second boolean "true" will return only
                                                   //required child products instead of all          

        //flatten arrays (which are grouped by option)  
        $ids = array();
        foreach($children_ids_by_option as $array)
        {
            $ids = array_merge($ids, $array);
        }

        //perform test and add to return value
        if(in_array($id_to_find, $ids))
        {
            $bundles[] = $product;
        }
    }

    return $bundles;
}

//usage
$products = findBundledProductsWithThisChildProduct($simple_product_id);
foreach($products as $product)
{
    var_dump($product->getData());
}
  • This was the solution I came up with as well, but it really isn't ideal for my particular situation. I was hoping that there was some sort of method that I was unaware of. Thanks for your help I have a better idea now where I need to go with my solution. – Gabarba Mar 14 '13 at 18:39
7

i know this is way too after the fact to be useful to you, but for future searchers...

janw's basic answer is right, but a more magento-ey way to do it would be:

$parentIds = Mage::getResourceSingleton('bundle/selection')->getParentIdsByChild($product->getId());

then you can load a product collection filtering on those ids.

1

I saw the answer of Alan Storm. But what if I have already have 100 bundle products. Then you would need to loop over all those :(

I figured out a way to query this:

<?php
function get_bundle_product_by_simple_id($id) {
    $read= Mage::getSingleton('core/resource')->getConnection('core_read');

    // fetch read database connection that is used in Mage_Core module
    $read = Mage::getSingleton('core/resource')->getConnection('core_read');

    $query = sprintf("SELECT `parent_product_id` FROM `catalog_product_bundle_selection` WHERE `product_id` = '%s'",
            htmlspecialchars( stripslashes( $id ) )
        );
    $value=$read->query( $query );
    $row = $value->fetch();

    if( is_null($row) ) {
        return $row;
    }
    return $row['parent_product_id'];
}

I pretty new to Magento, so there might be better ways. Or reasons not to do this at all. Mainly the escaping/stripping the $id

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.