Question

I need to create function that is creating bundle product, I already created my function and it works great, I mean its creating the bundle product for me, my problem is how can I add product's image, (image, small-image and thumbnail), I did some searches cannot find any good way to implement it, will be appreciate if anyone can help, Thanks :)

The following is my code that create the bundle product :

$productsId = array(6,8);
        $categoryId = array();
        $sum = 0;
        $skuSum = 0;
        $ProductsName = array();

        for($i=0,$j=count($productsId);$i<$j;$i++)
        {
            $productCheck = Mage::getModel('catalog/product');
            $categoryId[] = $productCheck->load($productsId[$i])->getCategoryIds();
            $productPrice = $productCheck->load($productsId[$i])->getPrice();
            $productSku = $productCheck->load($productsId[$i])->getSku();
            $ProductsName[] = $productCheck->load($productsId[$i])->getName();
            $sum += $productPrice;
            $skuSum +=  $productSku;
        }

        $bundleProductName = implode('-', $ProductsName);
        $categoryIDs = call_user_func_array('array_merge', $categoryId);
        $uniqueCategoryIDs = array_unique($categoryIDs);

        require_once('includes/config.php');
        require_once('app/Mage.php');

        $storeID = 1;
        $websiteIDs = array(1);
        //$cats = array(8,11,9);
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

        $p = array(
            'sku_type' => 1,
            'sku' => $skuSum,
            'name' => $bundleProductName,
            'description' => 'Pendant chian created by user',
            'short_description' => 'Pendant chian created by user',
            'type_id' => 'bundle',
            'attribute_set_id' => 4,
            'weight_type' => 0,
            'visibility' => 4,
            'price_type' => 1,
            'price_view' => 0,
            'status' => 1,
            'created_at' => strtotime('now'),
            'category_ids' => $uniqueCategoryIDs,
            'store_id' => $storeID,
            'website_ids' => $websiteIDs,
            'price' => $sum
        );

        $productCheck->setData($p);

        Mage::register('product', $productCheck);
        $optionRawData = array();

        $optionRawData[0] = array(
            'required' => 1,
            'option_id' => '',
            'position' => 0,
            'type' => 'multi',
            'title' => 'pendant chain',
            'default_title' => 'pendantChain',
            'delete' => '',
        );

        $selectionRawData = array();
        for($i=0,$j=count($productsId);$i<$j;$i++)
        {
            $selectionRawData[0][] = array(
                'product_id' => $productsId[$i],
                'selection_qty' => 1,
                'selection_can_change_qty' => 1,
                'position' => 0,
                'is_default' => 1,
                'selection_id' => '',
                'selection_price_type' => 0,
                'selection_price_value' => 0.0,
                'option_id' => '',
                'delete' => ''
            );
        }
        /*$selectionRawData[0] = array();
        $selectionRawData[0][] = array(
            'product_id' => 7,
            'selection_qty' => 1,
            'selection_can_change_qty' => 1,
            'position' => 0,
            'is_default' => 1,
            'selection_id' => '',
            'selection_price_type' => 0,
            'selection_price_value' => 0.0,
            'option_id' => '',
            'delete' => ''
        );*/

        Mage::register('productCheck', $productCheck);
        Mage::register('current_product', $productCheck);
        $productCheck->setCanSaveConfigurableAttributes(false);
        $productCheck->setCanSaveCustomOptions(true);
        // Set the Bundle Options & Selection Data
        $productCheck->setBundleOptionsData($optionRawData);
        $productCheck->setBundleSelectionsData($selectionRawData);
        $productCheck->setCanSaveBundleSelections(true);
        $productCheck->setAffectBundleProductSelections(true);
        $productCheck->save();

        if($productCheck->save()){
            return true;
        }
        else
            return false; 
Was it helpful?

Solution

use this code to save picture for bundle product,

$filepath is your location for the picture

$bundleProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);

Hope this helps,

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top