1

i have programmatically created bundled product and also able to add selection and option data to it, but my problem is that product is not visible in front end. when i save that product even without changing the any of the fields in admin, it starts getting displayed.

i am stuck and have no idea what to do if you have any idea please help me out. below is the code is used

$storeID = 0;
$websiteIDs = array(1);
$cats = array(19);

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

/** @var $productCheck Mage_Catalog_Model_Product */
$productCheck = Mage::getModel('catalog/product');

$p = array(
        'sku_type' => 1, //0 = dynamic, 1 = fixed
        'sku' => '687',
        'name' => "BarProduct",
        'description' => 'Foo',
        'short_description' => 'Bar',
        'type_id' => 'bundle',
        'attribute_set_id' => 4,
        'weight_type' => 0, //0 = dynamic, 1 = fixed
        'visibility' => 4,
        'price_type' => 0, //0 = dynamic, 1 = fixed
        'price_view' => 0, //0 = as low as, 1 = price range <---- DOES NOT SEEM TO HAVE ANY EFFECT
        'status' => 1,
        'category_ids' => $cats,
        'store_id' => $storeID,
        'website_ids' => $websiteIDs
);
$productCheck->setData($p);
$productCheck->setStockData(array(
        'is_in_stock' => 1,
        'qty' => 1
    ));


Mage::register('product', $productCheck);

$selectionRawData = array();
$selectionRawData[0] = array();

$optionRawData = array();
$ChildProduct = array(1,2);
$i = 0;
foreach ($ChildProduct as $child){
    $optionRawData[$i] = array(
            'required' => 1,
            'option_id' => '',
            'position' => 0,
            'type' => 'select',
            'title' => 'FooOption',
            'default_title' => 'FooOption',
            'delete' => '',
    );

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

$i++;
}

Mage::register('productCheck', $productCheck);
Mage::register('current_product', $productCheck);
$productCheck->setCanSaveConfigurableAttributes(false);
$productCheck->setCanSaveCustomOptions(true);
$productCheck->setCanSaveBundleSelections(true);
// Set the Bundle Options
$productCheck->setBundleOptionsData($optionRawData);
//set option data
$productCheck->setBundleSelectionsData($selectionRawData);

$productCheck->setAffectBundleProductSelections(true);

$productCheck->save();
6
  • Could it be a reindexing issue? The index is refreshed upon saving a product. Try and refresh them from the backend after importing. Commented Sep 11, 2013 at 6:45
  • tried reindexing, dint work.. Commented Sep 11, 2013 at 9:18
  • Hi did you solve your problem?
    – Martin
    Commented Aug 7, 2014 at 9:56
  • @Martin try using the above code its working, i have updated it Commented Aug 8, 2014 at 6:45
  • i found it added as answer, in my case was it a fixed price
    – Martin
    Commented Aug 8, 2014 at 10:55

5 Answers 5

0

I'm using this code when initializing Mage before creating products:

Mage::app()->setCurrentStore(
                Mage::getModel('core/store')
                        ->load(Mage_Core_Model_App::ADMIN_STORE_ID)
);

and they seem to be saving without any issues.

0

Try:

//Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Mage::app('admin');
0

I think the issue is how you are setting to store id to the product:

$storeID = 0;

Store Id 0 is the admin store so it would appear that you are adding the products only to the admin store. If this is the case they would not appear on any other store. Further more when you save the product in the admin section this then sets the store id correctly and so the product will now appear.

I would suggest either setting this value to the correct id or loading the id from the database.

0

In your case

'price_type' => 0, //0 = dynamic, 1 = fixed

must be set to 1 (item must have price),

'price_type' => 1, //0 = dynamic, 1 = fixed

Or

$__magproduct->setPriceType(1); //0 = dynamic, 1 = fixed

in object mode.

-1

Just set website id in the end, just before product save.

$productCheck->setWebsiteIds(array(0));
$productCheck->save();
1
  • everything is perfect in these code, created nearly 20K products using these code, just some code's were placed at wrong place Commented Feb 25, 2014 at 14:09

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