magento - 如何创建具有关联产品的可配置产品?

标签 magento

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

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

当我点击“继续”时,除关联产品外,所有产品数据均按预期显示。

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

最佳答案

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

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

并在 $SKU 变量中设置简单的产品 sku。我已经检查过,当我选择全局变量时,然后在配置产品中看到关联的产品后。

工作正常!!!

关于magento - 如何创建具有关联产品的可配置产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14949314/

相关文章:

magento - Magento 站点地图应该包含空类别吗?

Magento SOAP API 产品列表分页

php - Magento Admin::删除特定角色/用户的菜单项

ajax - 在自定义 Controller + Action 中使用 Magento 内置消息系统

magento - 在 magento 管理员角色权限中使用事件观察器添加网站字段

magento - Nginx Magento SSL ERR_TOO_MANY_REDIRECTS

magento - 如何使用 CentOS 在 Amazon Web Service 托管上设置 Magento?

magento - 将捆绑产品添加到购物车而无需指定选项

php - 在 Magento 商店中更改字体

php - 导出大型 Magento 目录的最佳方式是什么