doug48
7/26/2017 - 7:26 AM

Gets the product ids for bundle products that have options, that is more then one where a customer has to choose an option

Gets the product ids for bundle products that have options, that is more then one where a customer has to choose an option

<?php

require_once('abstract.php');

class Space48_Shell_ProductIds extends Mage_Shell_Abstract
{

    /**
     * Entry point
     */
    public function run()
    {
        $products = Mage::getModel('catalog/product')->getCollection();
        foreach($products as $product){
            $this->getOptions($product);
        }
    }

    public function getIds()
    {

        if(!$sku = $this->getArg('sku')){
            die('no sku');
        }

        $id = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku)->getId();

        echo 'sku: ' . $sku . ' product id = ' .  $id;
    }

    public function getOptions($product)
    {
        if($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE){
            $typeInstance = $product->getTypeInstance(true);
            $typeInstance->setStoreFilter($product->getStoreId(), $product);

            $optionCollection = $typeInstance->getOptionsCollection($product);

            $selectionCollection = $typeInstance->getSelectionsCollection(
                $typeInstance->getOptionsIds($product),
                $product
            );
            /** @var  $optionCollection Mage_Bundle_Model_Resource_Option_Collection */
            $options = $optionCollection->appendSelections($selectionCollection, false,
                true
            );
            foreach($options as $option){
                $optionSelection = $option->getData('selections');
                if(is_array($optionSelection) && count($optionSelection) > 1){
                    Mage::log('id: ' . $product->getId() . ' sku: ' . $product->getSku(), 6, 'debug.log', true);
                    break;
                }
            }

            return $options;
        }

    }

}



$ids = new Space48_Shell_ProductIds();
$ids->run();