【问题标题】:Cannot associate products programmatically无法以编程方式关联产品
【发布时间】:2013-01-05 07:01:08
【问题描述】:

我正在开发一个 magento 模块,其中以编程方式创建可配置和简单的产品。以下代码创建了可配置和简单的产品,但不关联它们:

$sProduct = Mage::getModel('catalog/product');

$sProduct
    ->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE)
    ->setSku($sku)
    ->setName($name)
    ->setPrice($price)
    ->setAttributeSetId($set_id) //id of set which contains size_range_c attribute
    ->setData("size_range_c", $option_id)
    ->setData("color", $colorId) //another attribute in set but not required
    ->setDescription($description)
    ->setShortDescription($short_description)
    ->setTaxClassId(0);

$sProduct->setStockData(array(
    'is_in_stock' => 1,
    'qty' => $qty
));

$sProduct->save();

$cProduct = Mage::getModel('catalog/product');
$cProduct->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
    ->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
    ->setSku($csku)
    ->setName($cname)
    ->setPrice($price)
    ->setAttributeSetId($set_id) //same set is used for both configurable and simple product
    ->setUrlKey($url)
    ->setDescription($description)
    ->setShortDescription($short_description)
    ->setTaxClassId(0);

$all_child_products = array();
$all_child_products[$sProduct->getId()] = array(
    $sv = array('attribute_id' => $attrId, //attrbute id of size_range_c
                'label' => $attr_name, //option name
                'value_index' => $option_id, //option_id
                'pricing_value' => 0, 
                'is_percent' => 0)
);

$size_values = array();
$size_values[] = $sv;

$_attributes = Mage::getResourceModel('eav/entity_attribute_collection')
    ->addFieldToFilter('attribute_code', 'size_range_c');
$attribute = $_attributes->getFirstItem(); //get data for size_range_c

$cProduct->setConfigurableProductsData($all_child_products);
$cProduct->setConfigurableAttributesData(array(
    array_merge($attribute->getData(), array('label' => '', 'values' => $size_values))
));

$cProduct->setCanSaveConfigurableAttributes(true);
$cProduct->setCanSaveCustomOptions(true);

$cProduct->save();

基本上,我为每种颜色创建一个可配置的产品,然后为该颜色的每种尺寸创建一个简单的产品,然后将它们关联起来。可配置产品和简单产品都需要 size_range_c 属性。当我从管理面板查看这些产品时,一切似乎都很好。我什至尝试手动将简单产品与可配置产品相​​关联,但以编程方式创建的产品不会出现在相关产品部分。

【问题讨论】:

    标签: php magento e-commerce


    【解决方案1】:

    我什至尝试将简单的产品手动关联到 可配置,但以编程方式创建的产品不会出现在 相关产品部分。

    任何可配置的产品都是使用一个或多个可配置属性创建的,比如本例中的颜色。一个可配置的产品不能有两个具有相同属性值的关联产品,比如说具有相同的红色。即使我们单击“重置过滤器”所有产品,Magento 也不会显示在“关联产品”部分。如果可配置产品是在颜色属性之后制作的,它将显示所有设置了颜色属性值的简单产品(颜色不为空),所以我认为对于您的产品,您没有为任何产品设置颜色属性,所以它不在“关联产品”中显示任何产品。

    以编程方式将简单产品与可配置产品相​​关联的快速方法是使用以下代码:

    Mage::getResourceModel('catalog/product_type_configurable')->saveProducts($pConfigurable, array($idSimpleProduct));
    

    注意:$pConfigurable 是可配置产品的实例,$idSimpleProduct 是您要关联的简单产品的 id。同样简单的产品必须在使用此功能之前设置颜色属性。如果其他简单产品已经与相同的颜色值关联,则该关联将被覆盖。

    另一种将简单产品关联到可配置产品(和 magento 方式)的解决方案可能是我在您的代码中看到的那样,使用 setConfigurableAttributesData 将数据设置为可配置产品模型实例,然后调用保存,但我认为该数组您的代码示例中的数据不完整。

    【讨论】:

    • 我正在使用您在此答案中的代码,但它不会显示在商店中。它将简单的产品链接到可配置的产品,但在 DB 中没有链接。如何将颜色链接到可配置产品?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多