php – 以编程方式添加捆绑产品未显示在前端

前端之家收集整理的这篇文章主要介绍了php – 以编程方式添加捆绑产品未显示在前端前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从 PHP脚本将捆绑的产品插入Magento数据库.有问题的版本是Community 1.5.1.0.

我尝试了问题“Programmatically add Bundle Products in Magento,using the SKU / ID of Simple Items”中描述的方法.插入的产品很好地显示在管理部分 – 我可以编辑它们,添加新选项和选择等.但是,无论我尝试什么,它们都不会出现在Magento前端 – 例如重建索引或从后端重新保存它们.通过管理界面添加捆绑工作正常.

在对数据库进行一些挖掘之后,我注意到在使用我的脚本时,catalog_product_index_price和catalog_product_index_price_bundle_idx表中没有必要的条目,而通过后端添加bundle通常会更新索引.就这些表而言,重新索引只是忽略添加的捆绑产品.

我挖掘了Magento源文件,找不到任何关于我做错的提示.所有缓存都被禁用,选择有库存,我试图包括我在研究Magento在后端插入产品时发送的POST请求时挖出的所有数据.

这是我用于测试的完整脚本,以及在底部注释的一些绝望尝试:

  1. $magentoPath = '/home/nikola/bin/magento-1.5/';
  2. require_once($magentoPath . 'includes/config.PHP');
  3. require_once($magentoPath . 'app/Mage.PHP');
  4.  
  5. $storeID = 1;
  6. $websiteIDs = array(1);
  7.  
  8. $mageObj = Mage::app()->setCurrentStore($storeID);
  9.  
  10. $product = Mage::getModel('catalog/product');
  11.  
  12. $cats = array("210");
  13. $p = array(
  14. '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
  15. );
  16.  
  17. $product->setData($p);
  18.  
  19. $product->setCanSaveBundleSelections(true);
  20. $product->setCanSaveCustomOptions(true);
  21.  
  22. Mage::register('product',$product);
  23. Mage::register('current_product',$product);
  24.  
  25. $optionRawData = array();
  26. $selectionRawData = array();
  27.  
  28. $optionRawData[0] = array(
  29. 'required' => 1,'option_id' => '','position' => 0,'type' => 'select','title' => 'FooOption','default_title' => 'FooOption','delete' => ''
  30. );
  31. $selectionRawData[0] = array();
  32. $selectionRawData[0][] = array(
  33. 'product_id' => 1810,'is_default' => true,'selection_id' => '','selection_price_type' => 0,'selection_price_value' => 0.0,'selection_qty' => 1,'selection_can_change_qty' => 1,'delete' => ''
  34. );
  35.  
  36. $product->setBundleOptionsData($optionRawData);
  37. $product->setBundleSelectionsData($selectionRawData);
  38.  
  39. $product->save();
  40.  
  41. /*
  42. $stockItem = Mage::getModel('cataloginventory/stock_item');
  43. $stockItem->loadByProduct($product->getId());
  44. if (!$stockItem->getId()) {
  45. $stockItem->setProductId($product->getId())->setStockId(1);
  46. }
  47. $stockItem->setData('is_in_stock',1);
  48. $stockItem->save();
  49.  
  50. $pi = Mage::getSingleton('bundle/price_index');
  51. $pi->addPriceIndexToProduct($product);
  52. $pi->save();
  53. */
  54.  
  55. ?>
请尝试使用以下代码&走着瞧吧:-
  1. <?PHP
  2. $magentoPath = '/home/nikola/bin/magento-1.5/';
  3. require_once($magentoPath . 'includes/config.PHP');
  4. require_once($magentoPath . 'app/Mage.PHP');
  5.  
  6. $storeID = 1;
  7. $websiteIDs = array(1);
  8. $cats = array("210");
  9.  
  10. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  11.  
  12. $product = Mage::getModel('catalog/product');
  13.  
  14. $p = array(
  15. 'sku_type' => 0,'sku' => '687','website_ids' => $websiteIDs
  16. );
  17.  
  18. $product->setData($p);
  19. Mage::register('product',$product);
  20.  
  21. /**
  22. * Section of Bundle Options
  23. *
  24. * required Properties of Bundle Options are:-
  25. * 1. title
  26. * 2. option_id
  27. * 3. delete
  28. * 4. type
  29. * 5. required
  30. * 6. position
  31. * 7. default_title
  32. */
  33. $optionRawData = array();
  34. $optionRawData[0] = array(
  35. 'required' => 1,'delete' => '',);
  36.  
  37. /**
  38. * Section of Bundle Selections
  39. *
  40. * required Properties of Bundle Selections
  41. * 1. selection_id
  42. * 2. option_id
  43. * 3. product_id
  44. * 4. delete
  45. * 5. selection_price_value
  46. * 6. selection_price_type
  47. * 7. selection_qty
  48. * 8. selection_can_change_qty
  49. * 9. position
  50. * 10. is_default
  51. */
  52. $selectionRawData = array();
  53. $selectionRawData[0] = array();
  54. $selectionRawData[0][] = array(
  55. 'product_id' => 1810,'is_default' => 1,'delete' => ''
  56. );
  57.  
  58. $product->setCanSaveConfigurableAttributes(false);
  59. $product->setCanSaveCustomOptions(true);
  60.  
  61. // Set the Bundle Options & Selection Data
  62. $product->setBundleOptionsData($optionRawData);
  63. $product->setBundleSelectionsData($selectionRawData);
  64. $product->setCanSaveBundleSelections(true);
  65. $product->setAffectBundleProductSelections(true);
  66.  
  67. $product->save();
  68. ?>

希望能帮助到你.

猜你在找的PHP相关文章