2

I've actually been successful in programmatically creating the bundled product, and it shows up in the Admin correctly, but does not show in the front-end until I save from within the Admin. I'm trying to create a large number of bundled products, so having to go in the admin and save them individually isn't acceptable.

Here is my code:

foreach($productRow as $product){

$data = array(

    'attribute_set_id' => 64, //MANUALLY SET

    'sku_type'                              => 0, //0 = dynamic, 1 = fixed
    'sku'                                   => $product['INVENTORY_PARTNUMBER']."B",
    'name'                                  => $product['INVENTORY_ITEMNAME'],
    'description'                           => $product['WEB_DESCRIPTION'],
    'short_description'                     => $product['WEB_DESCRIPTION'],
    'type_id'                               => 'bundle',
    'entity_type_id'                        => 4,
    'weight_type'                           => 0, //0 = dynamic, 1 = fixed
    'visibility'                            => 4,
    'price_type'                            => 0, //0 = dynamic, 1 = fixed
    'price_view'                            => 1, //0 = as low as, 1 = price range <---- DOES NOT SEEM TO HAVE ANY EFFECT
    'status'                                => 1,
    'created_at'                            => strtotime('now'),
    'category_ids'                          => $cats,
    'store_id'                              => $storeID,
    'website_ids'                           => $websiteIDs,
    'freightquote_height'                   => '36',
    'freightquote_width'                    => '36',
    'freightquote_length'                   => '36',

    'freightquote_nmfc'                     => 0,
    'freightquote_commodity'                => 'GeneralMerchandise',
    'freightquote_content'                  => 'NewCommercialGoods',
    'freightquote_hzmt'                     => 0,
    'freightquote_enable'                   => 0,
);

$productCheck = Mage::getModel('catalog/product');

    $productCheck->setData($data);
    Mage::register('product', $productCheck);
    Mage::register('current_product', $product);

    $productCheck->setStockData(array(
        'is_in_stock' => 1,
        'qty' => 1
    ));
    $productCheck->setPriceView(1);

    $optionRawData = array();
    $selectionRawData = array();

    $i = 0;

    /**
     * Turning the SKU into an "option" so that
     * it can be added to the bundle
     */
    $product['INVENTORY_PARTNUMBER'] = $product['INVENTORY_PARTNUMBER'].", the sku";

    foreach ($options as $option => $name){

        if(isset($product[$option])){
            $optionRawData[$i] = array(
                'required' => 0,
                'option_id' => '',
                'position' => $i,
                'type' => 'radio',
                'title' => $name,
                'default_title' => $name,
                'delete' => '',
            );

            $partsArray = explode ("|", $product[$option]);


            foreach ($partsArray as $pn){

                    $product_id = Mage::getModel("catalog/product")->getIdBySku($pn);

                        $selectionRawData[$i][] = array(
                            'product_id' => $product_id,
                            'selection_qty' => 1,
                            'selection_can_change_qty' => 0,
                            'position' => $i2,
                            'is_default' => 0,
                            'selection_id' => '',
                            'selection_price_type' => 0,
                            'selection_price_value' => 0.0,
                            'option_id' => '',
                            'delete' => ''
                        );

                    $i2++;
            }
            $i++;
        }

    }

    $productCheck->setCanSaveConfigurableAttributes(false);
    $productCheck->setCanSaveCustomOptions(false);

    // Set the Bundle Options & Selection Data
    $productCheck->setBundleOptionsData($optionRawData);
    $productCheck->setBundleSelectionsData($selectionRawData);
    $productCheck->setCanSaveBundleSelections(true);
    $productCheck->setAffectBundleProductSelections(true);

    try {
        $productCheck->save();
    }
    catch(exception $e) {
        echo "<pre>";
        print_r($e);
        echo "</pre>";
    }

Everything I can find shows that I've created and saved the bundled product correctly, but it won't show in the front end until I save it from the back end. Indexing doesn't do anything, and caching is off.

  • did you issue was solved, if yes than please share what had fixed that issue. as i am facing the same issue. – Deepak Mallah Sep 12 '13 at 6:20
4

The answer was pretty simple: Website ID was zero and need to be 1

EDIT: Wanted to add to my answer since finding information for automating bundled products is lacking, and its the one feature that has yet to fully find its way into Magmi.

If you're having trouble with $product->save(); try $product->getResource()->save($product);

To programmatically update a bundled product, check this post to load the bundled product data correctly. Then, you can check if a bundled product exists; from there you only need to match your option titles to the existing product, and get that options ID with $option->getOptionId() so that you don't create a duplicate option.

Updating any other attribute on your bundled product (except for the elusive "Ship Bundle Items" attribute) can be added to an associative array as you would an attribute for any other product, and use $product->addData($data) to update your product attributes.

| improve this answer | |

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.