I am trying to insert related products into a Magento database with a PHP script. This version belongs to the community 1.5.1.0.
I tried the method described in the question “ Programmatically add Bundle Products to Magento using SKU / ID of simple elements ”. Plugin products display well in the administration section - I can edit them, add new options and choices, etc. However, they do not appear at all in the Magento interface no matter what I try - for example, rebuilding indexes or re-saving them from the background. Adding packages through the administration interface works great.
After some searching in the database, I noticed that when using my script there are no necessary entries in the catalog_product_index_price
and catalog_product_index_price_bundle_idx
tables when adding a package through internal index updates. Re-indexing simply ignores the added package product as far as tables are concerned.
I dug the Magento source files and cannot find any hints of what I am doing wrong. All caches are disabled, the choice is in stock, and I tried to include all the data that I dug up while studying the POST request. Magento sends when you insert the product at the end.
Here's the full script I'm using for testing, as well as some desperate attempts commented out below:
$magentoPath = '/home/nikola/bin/magento-1.5/'; require_once($magentoPath . 'includes/config.php'); require_once($magentoPath . 'app/Mage.php'); $storeID = 1; $websiteIDs = array(1); $mageObj = Mage::app()->setCurrentStore($storeID); $product = Mage::getModel('catalog/product'); $cats = array("210"); $p = array( 'sku_type' => 0, 'sku' => 687, 'name' => "BarProduct", 'description' => 'Foo', 'short_description' => 'Bar', 'type_id' => 'bundle', 'attribute_set_id' => 4, '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 ); $product->setData($p); $product->setCanSaveBundleSelections(true); $product->setCanSaveCustomOptions(true); Mage::register('product', $product); Mage::register('current_product', $product); $optionRawData = array(); $selectionRawData = array(); $optionRawData[0] = array( 'required' => 1, 'option_id' => '', 'position' => 0, 'type' => 'select', 'title' => 'FooOption', 'default_title' => 'FooOption', 'delete' => '' ); $selectionRawData[0] = array(); $selectionRawData[0][] = array( 'product_id' => 1810, 'position' => 0, 'is_default' => true, 'selection_id' => '', 'option_id' => '', 'selection_price_type' => 0, 'selection_price_value' => 0.0, 'selection_qty' => 1, 'selection_can_change_qty' => 1, 'delete' => '' ); $product->setBundleOptionsData($optionRawData); $product->setBundleSelectionsData($selectionRawData); $product->save(); ?>