【问题标题】:Programmatically add Bundle Products in Magento, using the SKU / ID of Simple Items使用简单项目的 SKU / ID 以编程方式在 Magento 中添加捆绑产品
【发布时间】:2011-03-07 17:32:40
【问题描述】:

我在 Magento 中有一些简单的目录产品,所以我有它们的 SKU 和 ID。现在我想使用捆绑项目的数组元素“bundle_options”和“bundle_selections”创建一个捆绑产品,Magento Admin 编码在其观察者类中使用这些元素。

同样在 Observer 类中,有两个函数“setBundleOptionsData()”和“setBundleSelectionsData()”的方法调用,我无法找到任何函数定义。

请任何专业人士在这里发帖,因为我需要一些适当的方法来做这件事。如果需要,覆盖模块或使用事件,我会这样做,但我需要非常专业的帮助。 提前致谢。

编辑:-
关于上面提到的“setBundleOptionsData()”&“setBundleSelectionsData()”这两种方法,我几乎可以肯定的是他们使用了某种PHP魔术方法,但我不知道这些魔术方法的主要逻辑在哪里是写的吗?

请任何人提供一些正确的答案。非常感谢任何帮助。

【问题讨论】:

  • 我仍在等待一些机构提供更多有用的信息。
  • 请查看此链接以获取有关如何以正确方式以编程方式添加捆绑产品的更多有价值信息。 stackoverflow.com/questions/6161128/…

标签: php magento shopping-cart bundle product


【解决方案1】:

为此,我没有使用任何 Web 服务。我只是使用了以下专门用于捆绑产品的方法,它们是:-

  1. setBundleOptionsData()
  2. setBundleSelectionsData()
  3. setCanSaveBundleSelections(true)

对于第一种方法,Bundle Options的详细信息以数组的形式作为参数提供给方法。同样,对于第二种方法“setBundleSelectionsData()”,我们将Bundle Selections的详细信息作为参数以数组的形式提供给该方法。

这是在 Magento 中添加任何捆绑产品的主要逻辑。 希望对新手有所帮助!!!


请查看this link,了解有关以适当方式创建捆绑产品的更多详细信息。

【讨论】:

    【解决方案2】:
             $MyOptions[0] = array (
                'title' => 'My Bad','default_title' => 'My Bad',
                'delete' => '',
                'type' => 'radio',
                'required' => 0,
                'position' => 0
            );
    

    $optionModel = Mage::getModel('bundle/option') ->addSelection('op111') ->setTitle('op111') ->setDefaultTitle('op111') ->setParentId($product_id) ->setStoreId($product->getStoreId()); $optionModel->save();

    【讨论】:

      【解决方案3】:

      在这方面遇到了困难,但发现这让我克服了困难:

                      $items[] = array(
                      'title' => 'test title',
                      'option_id' => '',
                      'delete' => '',
                      'type' => 'radio',
                      'required' => 1,
                      'position' => 0);
      
                  $selections = array();
                  $selectionRawData[] = array(
                      'selection_id' => '',
                      'option_id' => '',
                      'product_id' => '159',
                      'delete' => '',
                      'selection_price_value' => '10',
                      'selection_price_type' => 0,
                      'selection_qty' => 1,
                      'selection_can_change_qty' => 0,
                      'position' => 0);
                  $selections[] = $selectionRawData;
      
                  $productId = 182;
                  $product    = Mage::getModel('catalog/product')
                  ->setStoreId(0);
                  if ($productId) {
                      $product->load($productId);
                  }
                  Mage::register('product', $product);
                  Mage::register('current_product', $product);
                  $product->setCanSaveConfigurableAttributes(false);
                  $product->setCanSaveCustomOptions(true);
      
                  $product->setBundleOptionsData($items);
                  $product->setBundleSelectionsData($selections);
                  $product->setCanSaveCustomOptions(true);
                  $product->setCanSaveBundleSelections(true);
      
                  $product->save();
      

      具体来说,

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

      是关键

      编辑:: 在尝试添加多个选项/选择时,看起来也有一些特殊性。 setBundleOptionsData 采用一组选项,即

      Array(
      [1] => Array
          (
              [title] => Option 2
              [option_id] => 
              [delete] => 
              [type] => select
              [required] => 1
              [position] => 
          )
      
      [0] => Array
          (
              [title] => Option 1
              [option_id] => 
              [delete] => 
              [type] => select
              [required] => 1
              [position] => 
          ))
      

      然后选择将是一个选择数组的数组,其索引对应于选项数组:

      Array(
      [1] => Array
          (
              [2] => Array
                  (
                      [selection_id] => 
                      [option_id] => 
                      [product_id] => 133
                      [delete] => 
                      [selection_price_value] => 0.00
                      [selection_price_type] => 0
                      [selection_qty] => 1
                      [selection_can_change_qty] => 1
                      [position] => 0
                  )
      
              [3] => Array
                  (
                      [selection_id] => 
                      [option_id] => 
                      [product_id] => 132
                      [delete] => 
                      [selection_price_value] => 0.00
                      [selection_price_type] => 0
                      [selection_qty] => 1
                      [selection_can_change_qty] => 1
                      [position] => 0
                  )
      
          )
      
      [0] => Array
          (
              [0] => Array
                  (
                      [selection_id] => 
                      [option_id] => 
                      [product_id] => 206
                      [delete] => 
                      [selection_price_value] => 0.00
                      [selection_price_type] => 0
                      [selection_qty] => 1
                      [selection_can_change_qty] => 1
                      [position] => 0
                  )
      
              [1] => Array
                  (
                      [selection_id] => 
                      [option_id] => 
                      [product_id] => 159
                      [delete] => 
                      [selection_price_value] => 0.00
                      [selection_price_type] => 0
                      [selection_qty] => 1
                      [selection_can_change_qty] => 1
                      [position] => 0
                  )
      
          ))
      

      【讨论】:

      • 感谢 TON 亲爱的,你把我从巨大的混乱中救了出来。特别是多个选项和产品的代码是救命稻草。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      相关资源
      最近更新 更多