Close

April 12, 2017

Magento Check if Product is Part of a Bundle

We had a spare product that we used in a previous bundle for a sale, but had lost track of it. We wrote a quick and dirty script, to parse through all of our bundles, to find if that product still exists in the bundle.

You would need to adjust the “require_once” depending on what folder you are running this script in.

Calling this script would be as simple as passing in the simple id you are looking for in a bundle:
http://website.com/myreports/product_in_bundle.php?subid=2186

<?php
require_once('../app/Mage.php');
error_reporting(E_ALL);
Mage::app();
$subtarget=$_GET["subid"];
print "<h1> Checking Product $subtarget against all Bundles</h1>";
 $products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToSelect('type')
    ->addFieldToFilter('type_id', array('eq' => 'bundle')) // simple/configurable etc
;
 
foreach($products as $prod) {
    $subcoll = $prod->getTypeInstance(true)
                    ->getSelectionsCollection(
                        $prod->getTypeInstance(true)
                    ->getOptionsIds($prod), $prod);
        $bundleid=$prod->getId();
       print "<BR><h1>$bundleid</h1>";
        foreach ($subcoll as $item) {
            $subid=$item->product_id;
            print "<BR>SubId:$subid";
            if($subid==$subtarget){
                print "<span style='color:red'>FOUND IT!!!</span>";
                die();
            }
        }
 
}
 
?>