0

我正在尝试以编程方式创建捆绑产品,并通过以下方式设置选项:

            $new_options[$count] = array(
                'required' => 0,
                'position' => 1,
                'parent_id' => $parentProduct->getId(),
                'type' => 'select',
                'title' => $product->getName(),
                'default_title' => $product->getName()
            );
            $new_selections[$count] = array(array(
                'product_id' => $product->getEntityId(),
                'selection_qty' => $child['qty'],
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1,
                'selection_price_type' => 0,
                'selection_price_value' => 0.0
            ));
      ...
    $parentProduct->setBundleOptionsData($new_options);
    $parentProduct->setBundleSelectionsData($new_selections);

这看起来是正确的(如https://stackoverflow.com/a/4415800/494643中所述)。但是,它不起作用 - 我收到一个 SQL 异常,抱怨Column 'selection_id' cannot be null'. 我该如何解决这个问题?选择id是一个auto_increment列,所以在创建之前无法获取,但是看起来无法创建?

4

1 回答 1

0

解决方案在于Mage_Bundle_Model_Selection模型的 _beforeSave 函数。

这会导致它在保存选择对象之前尝试将 selection_id 写入catalog_product_bundle_selection_price表,因此在它生成 selection_id 之前。因此答案是强制它在保存之前生成一个id,所以再次查看_beforeSave函数,我们可以看到它只有在store不为0时才保存价格。

具体来说,这部分:

$storeId = Mage::registry('product')->getStoreId();
if (!Mage::helper('catalog')->isPriceGlobal() && $storeId) {

这意味着如果我们先将注册表产品的 store id 设置为 0,我们可以使其保存选择,而不保存价格。然后我们需要将商店重置为之前的值,然后再次保存,以记录价格——尽管只有在重新加载对象以获取新生成的选择 id 之后。

于 2013-11-11T10:52:23.083 回答