【问题标题】:Programatically added bundle product isn't showing up in frontend以编程方式添加的捆绑产品未显示在前端
【发布时间】:2011-09-03 21:42:41
【问题描述】:

我正在尝试从 PHP 脚本将捆绑产品插入 Magento 数据库。有问题的版本是 Community 1.5.1.0。

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

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

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

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

$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();

/*
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());
if (!$stockItem->getId()) {
  $stockItem->setProductId($product->getId())->setStockId(1);
}
$stockItem->setData('is_in_stock', 1);
$stockItem->save();

$pi = Mage::getSingleton('bundle/price_index');
$pi->addPriceIndexToProduct($product);
$pi->save();
*/

?>

【问题讨论】:

    标签: php magento bundle


    【解决方案1】:

    请尝试使用以下代码并看看会发生什么:-

    <?php
    $magentoPath = '/home/nikola/bin/magento-1.5/';
    require_once($magentoPath . 'includes/config.php');
    require_once($magentoPath . 'app/Mage.php');
    
    $storeID = 1;
    $websiteIDs = array(1);
    $cats = array("210");
    
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    
    $product = Mage::getModel('catalog/product');
    
    $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);
    Mage::register('product', $product);
    Mage::register('current_product', $product);
    
    /**
     * Section of Bundle Options
     * 
     * Required Properties of Bundle Options are:-
     * 1. title
     * 2. option_id
     * 3. delete
     * 4. type
     * 5. required
     * 6. position
     * 7. default_title
     */
    $optionRawData = array();
    $optionRawData[0] = array(
      'required' => 1,
      'option_id' => '',
      'position' => 0,
      'type' => 'select',
      'title' => 'FooOption',
      'default_title' => 'FooOption',
      'delete' => '',
    );
    
    /**
     * Section of Bundle Selections
     * 
     * Required Properties of Bundle Selections
     * 1.   selection_id
     * 2.   option_id
     * 3.   product_id
     * 4.   delete
     * 5.   selection_price_value
     * 6.   selection_price_type
     * 7.   selection_qty
     * 8.   selection_can_change_qty
     * 9.   position
     * 10.  is_default
     */
    $selectionRawData = array();
    $selectionRawData[0] = array();
    $selectionRawData[0][] = array(
      'product_id' => 1810,
      'selection_qty' => 1,
      'selection_can_change_qty' => 1,
      'position' => 0,
      'is_default' => 1,
      'selection_id' => '',
      'selection_price_type' => 0,
      'selection_price_value' => 0.0,
      'option_id' => '',
      'delete' => ''
    );
    
    $product->setCanSaveConfigurableAttributes(false);
    $product->setCanSaveCustomOptions(true);
    
    // Set the Bundle Options & Selection Data
    $product->setBundleOptionsData($optionRawData);
    $product->setBundleSelectionsData($selectionRawData);
    $product->setCanSaveBundleSelections(true);
    $product->setAffectBundleProductSelections(true);
    
    $product->save();
    ?>
    

    希望对你有帮助。

    【讨论】:

    • 有效!非常非常感谢你。我只是将您的版本与我的版本进行了比较,看来问题出在Mage::register('current_product', $product); 部分——将其注释掉就可以了。再次感谢和欢呼!
    • 是的,变量“current_product”的注册似乎不合适,因为直到最后一行代码才保存任何产品。我还添加了另一行,这也很重要,“$product-&gt;setAffectBundleProductSelections(true);”。为工作干杯!
    • @KnowledgeCraving 你能帮我解决这个问题吗stackoverflow.com/questions/12698975/…。谢谢:)_
    • @Knowledge Craving 我这样添加了您的代码,产品被添加到管理部分,如果我打开产品并从管理面板重新保存(不做任何更改),它就会开始显示在前端.如果知道任何解决方案,请帮助我。我正在使用 magento 1.7.02
    【解决方案2】:

    我尝试过使用您的代码,但它似乎在 Magento 1.7.0.2 中不起作用。显然无法保存产品。

    我所做的是添加了以下几行:

     Mage::register('product', $product);
     Mage::register('current_product', $product);
     $product->setCanSaveConfigurableAttributes(false);
     $product->setCanSaveCustomOptions(true);
    

    就在行前:

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

    这似乎解决了无法保存文件的问题。

    【讨论】:

    • 感谢您提供该信息!我也在更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多