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();
            }
        }
 
}
 
?>

Fatal error: Uncaught Error: [] operator not supported for strings in /home/silvatec/public_html/wp-content/themes/layerswp/core/helpers/post.php:62 Stack trace: #0 /home/silvatec/public_html/wp-content/themes/layerswp/partials/content-single.php(81): layers_post_meta(1558) #1 /home/silvatec/public_html/wp-includes/template.php(690): require('/home/silvatec/...') #2 /home/silvatec/public_html/wp-includes/template.php(647): load_template('/home/silvatec/...', false) #3 /home/silvatec/public_html/wp-includes/general-template.php(167): locate_template(Array, true, false) #4 /home/silvatec/public_html/wp-content/themes/layerswp/single.php(20): get_template_part('partials/conten...', 'single') #5 /home/silvatec/public_html/wp-includes/template-loader.php(74): include('/home/silvatec/...') #6 /home/silvatec/public_html/wp-blog-header.php(19): require_once('/home/silvatec/...') #7 /home/silvatec/public_html/index.php(17): require('/home/silvatec/...') #8 {main} thrown in /home/silvatec/public_html/wp-content/themes/layerswp/core/helpers/post.php on line 62