【问题标题】:How do I create a configurable product with associated products?如何创建具有关联产品的可配置产品?
【发布时间】:2013-02-19 03:25:50
【问题描述】:

以下代码确实创建了一个可配置的产品,但是,当我在后端打开产品时,会出现以下消息:

选择可配置的属性 “只有范围为“全局”、输入类型为“下拉”和用于创建可配置产品“是”的属性可用。” 将显示一个复选框(“颜色组”),在继续之前必须选中它。

当我点击“继续”时,所有产品数据都如预期的那样存在,但相关产品除外。

//Mage Product
$mpr = Mage::getModel('catalog/product');
$mpr
    ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
    ->setTaxClassId(5)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
    ->setWebsiteIds(array(1))
    ->setAttributeSetId(4) // You can determine this another way if you need to.
    ->setSku("C12345")
    ->setName("C12345")
    ->setQty(25)
    ->setShortDescription('short description')
    ->setDescription('description')
    ->setPrice(1)
    ->setStockData(array(
        'use_config_manage_stock' => 1,
        'is_in_stock' => 1,
        'is_salable' => 1,
    ));

    $productData = array(
         '7039604' =>
            array('0' => array('attribute_id' => '85', 'label' => 'ROYAL','value_index' => '28563', 'is_percent' => 0, 'pricing_value' => '')
                 ,'1' => array('attribute_id' => '192', 'label' => '14', 'value_index' => '28728', 'is_percent' => 0, 'pricing_value' => '')
            )
    );
    $attributeData = array(
        '0' => array(
            'id' => NULL
            ,'label' => 'Color'
            ,'position' => NULL
            ,'values' => array(
                '0' => array('value_index' => 28563, 'label' => 'ROYAL', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '85')
            )
            ,'attribute_id' => 85
            ,'attribute_code' => 'color'
            ,'frontend_label' => 'Color'
            ,'html_id' => 'config_super_product__attribute_0')
        ,'1' => array(
            'id' => NULL
            ,'label' => 'Rivers Size'
            ,'position' => NULL
            ,'values' => array(
                '0' => array('value_index' => 28728, 'label' => '14', 'is_percent' => 0, 'pricing_value' => '0', 'attribute_id' => '192')
            )
            ,'attribute_id' => 192
            ,'attribute_code' => 'rivers_size'
            ,'frontend_label' => 'Rivers Size'
            ,'html_id' => 'config_super_product__attribute_1')
    );
$mpr->setConfigurableProductsData($productData);
$mpr->setConfigurableAttributesData($attributeData);
$mpr->setCanSaveConfigurableAttributes(true);
$mpr->save();

【问题讨论】:

  • 我修复了以下属性集 id 不正确的错误。 ->setAttributeSetId(10) 现在,当我创建可配置产品并在后端打开它时,我收到以下消息:Select Configurable Attributes Only 范围为“Global”的属性,输入类型“Dropdown”并用于创建可配置产品“Yes”可用。 Material Size Cotton Color Color Group 我选择了尺码和颜色,然后点击继续,但是仍然没有关联的产品。

标签: magento


【解决方案1】:

在$mpr->save()之前添加这段代码;

$SKU = "any-simple product sku enter here";
$productid = Mage::getModel('catalog/product')
                  ->getIdBySku(trim($SKU));
$mpr->assignProduct($productid);

并在 $SKU 变量中设置简单的产品 sku。并且我检查了当我选择全局变量然后在配置产品中看到相关产品之后。

效果很好!!!

【讨论】:

    【解决方案2】:

    如果您被重定向到select attribute 页面,这意味着您在此示例中设置的属性数据未正确保存。
    脚本运行后尝试查看catalog_product_super_attribute(应添加新行)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多