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();
            }
        }
 
}
 
?>
Posted in Magento, Tech Tips Tagged with: , , ,

Customer Love

I must take the time to write this. You always show the utmost in consideration in terms of your computer expertise. Your fast response and quick solutions to problems are greatly appreciated. I highly recommend your service to anyone with Data Scientist, Software Engineer and IT Management Consultant........and MORE! Thanks Jeremy, I have used your services ...sheesh, I lost count, at least 5 or 6 years and plan on continuing, as long as computers exist. And I have a feeling computers will be around for awhile, such as ...oh, I don't know, FOREVER! Take care and talk soon. Dan Ventola - President of The National Weather Station, LLC

- Dan Ventola - Owner of The National Weather Station