Created
May 20, 2016 03:28
-
-
Save ProxiBlue/4e8cc8e1e8bb02a72168ca51d9c1d889 to your computer and use it in GitHub Desktop.
Magento 1: Find All bundles that contain a simple
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* http://magento.stackexchange.com/questions/1354/how-to-find-all-bundle-products-that-contain-a-simple | |
* | |
*/ | |
require_once '../shell/abstract.php'; | |
class Mage_Shell_findProducts extends Mage_Shell_Abstract { | |
public function run() { | |
$simple_product_id = '5'; | |
$products = $this->findBundledProductsWithThisChildProduct($simple_product_id); | |
foreach ($products as $product) { | |
$product = mage::getModel('catalog/product')->load($product->getEntityId()); | |
echo $product->getName() . "\n"; | |
} | |
} | |
public 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; | |
} | |
} | |
$shell = new Mage_Shell_findProducts(); | |
$shell->run(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment