0

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; 
1
0

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,

4
  • Thanks actually it works well :) But in my case I need to pass the image name to the function and according the name the path will be changed it means everytime I need to upload the file to the correct path if not mistaken is there any way to do that ? – Saghi Ghorbani Dec 26 '14 at 8:16
  • @SaghiGhorbani how you upload the image and where it stored and what name you assign for it.. – Manikandan Arunachalam Dec 26 '14 at 8:43
  • @SaghiGhorbani just create temporary image using php imagick and with name that should be unique then use this temporary image path. – Manikandan Arunachalam Dec 26 '14 at 8:53
  • The image name will POST and I need to save it in temp folder in Magento and put the path like media/temp/[image_name.jpg] everytime the image_name should be the same as the name has been posted. – Saghi Ghorbani Dec 26 '14 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.