First of all: I am using Magento 1.7.0.2. I have programmed a function, which create a bundled product on the fly and add it to the cart. This product shows in the backend correctly, but in the frontend I'am missing the options, when I want to edit this item from the cart.
I also ran into an issue, that I can't properly add this generated items to the cart. It display me an error:
Some of the products below do not have all the required options. Please edit them and configure all the required options.
If I open the generated product in the backend and save it there, it display every option in the fronted correctly, but the other error is still appearing.
Here is my code so far:
$storeID = 1;
$websiteIDs = array(1);
$cats = array( $this->categoryPresent );
$productCounter = 0;
$presentParams['additional'] = 171; //Product ID
$presentParams['box'] = 167; //Product ID
$presentParams['count'] = 3; //amount of ordered presents
//Create base bundle with all required attributes
$product = Mage::getModel('catalog/product');
$p = array(
'sku_type' => 0,
'sku' => 'present - ' . strtotime('now'),
'name' => "Present",
'description' => 'Present',
'short_description' => 'Present',
'type_id' => 'bundle',
'attribute_set_id' => 9,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 0,
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => $storeID,
'website_ids' => $websiteIDs,
'base_price_amount' => '',
'base_price_base_amount' => 1,
'base_price_base_unit' => 'St',
'options_container' => 'container1'
);
$product->setData($p);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
$optionRawData = array();
$selectionRawData = array();
//Insert Box
$productCounter++;
$optionRawData[] = array(
'required' => 1,
'option_id' => '',
'position' => $productCounter,
'type' => 'select',
'title' => 'Box',
'default_title' => 'Box',
'delete' => '',
);
$selectionRawData[$productCounter-1] = array();
$selectionRawData[$productCounter-1][] = array(
'product_id' => $presentParams['box'],
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => $productCounter,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => '',
'delete' => ''
);
//Insert Additional
$productCounter++;
$optionRawData[] = array(
'required' => 1,
'option_id' => '',
'position' => $productCounter,
'type' => 'select',
'title' => 'Zubehör',
'default_title' => 'Zubehör',
'delete' => '',
);
$selectionRawData[$productCounter-1] = array();
$selectionRawData[$productCounter-1][] = array(
'product_id' => $presentParams['additional'],
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => $productCounter,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => '',
'delete' => ''
);
// Set the Bundle Options & Selection Data ; Save the present
Mage::register('product', $product);
Mage::register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
$product->setBundleOptionsData($optionRawData);
$product->setBundleSelectionsData($selectionRawData);
$product->save();
//Add Product to cart
$cart = Mage::getSingleton('checkout/cart');
//Try an other way of setting the bundled_option
/*$option_ids = $product->getTypeInstance(true)->getOptionsIds($product);
$selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection($option_ids, $product);
$bundled_items = array();
foreach($selectionCollection as $key => $option){
//$bundled_items[$key] = $option->product_id;
$bundled_items[$key] = 1;
}
$params = array(
'product' => $product->getId(),
'related_product' => null,
'bundle_option' => $bundled_items,
'qty' => $presentParams['count'],
);*/
$cart->addProduct( $product, $presentParams['count']);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
I think, there is just a little piece of code, which is missing. I hope, someone can help to solve this problem.
UPDATE
The bundled products are generated in the front-end. So one thing I've found to do then is indexing the new product. Here is the additional code:
//Index new item
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$stockItem->setForceReindexRequired(true);
Mage::getSingleton('index/indexer')->processEntityAction(
$stockItem,
Mage_CatalogInventory_Model_Stock_Item::ENTITY,
Mage_Index_Model_Event::TYPE_SAVE
);
$product->setForceReindexRequired(true)->setIsChangedCategories(true);
Mage::getSingleton('index/indexer')->processEntityAction($product, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_SAVE);
With this the generated products are correctly listed in the list-view of the responding category. But the contents of the bundle are still missing.