我正在尝试以编程方式创建产品,并且我可以成功创建可配置和简单的产品,我只是不确定如何将这两者联系起来。
以下是我正在使用的内容:
foreach ($products as $sku => $row)
{
$product = Mage::getModel('catalog/product');
$product->setAttributeSetId(4)
->setTypeId('configurable')
->setStockData(array(
'manage_stock' => 0,
'min_sale_qty' => 0,
'max_sale_qty' => 0,
))
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setTaxClassId(1)
->setCreatedAt(time())
->setName($row['name'])
->setSku($sku)
->setPrice($row['price'])
->setCategoryIds(array(2))
->setStatus(1) // enabled
->setWeight(1)
->setWebsiteIDs(array(1))
->setDescription($row['description'])
->setShortDescription($row['short_desc']);
try
{
$product->save();
$parent_id = $product->getId();
echo 'Configurable Product created: ' . $parent_id . "\n";
if (!empty($parent_id))
{
foreach ($row['children'] as $child_sku => $child)
{
$product = Mage::getModel('catalog/product');
$product->setAttributeSetId(4)
->setTypeId('simple')
->setStockData(array(
'manage_stock' => 0,
'min_sale_qty' => 0,
'max_sale_qty' => 0,
))
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setTaxClassId(1)
->setCreatedAt(time())
->setName($row['name'] . ' ' . $color)
->setSku($sku . $child_sku)
->setPrice($row['price'])
->setcategoryIds(array(2))
->setStatus(1)
->setWeight(1)
->setWebsiteIDs(array(1))
->setDescription($row['description'])
->setShortDescription($row['short_desc'])
->setParentId($parent_id);
$product->save();
echo 'Simple product created: ' . $product->getId() . "\n";
}
echo "\n";
}
}
catch (Exception $e)
{
echo 'Unable to create product', "\n";
echo $e->getMessage() . "\n";
}
}
我在子创建产品中使用setParentId,但这似乎不起作用。这不是一个错误,只是似乎没有做任何事情,产品似乎没有链接。
答案 0 :(得分:2)
弗朗西斯,用a simple product
添加configurable product
不是正确的方法。
setParentId()
不是添加简单可配置产品的正确方法。
从代码中删除setParentId()
函数。
您使用以下代码
Mage::getResourceSingleton('catalog/product_type_configurable')
->saveProducts( $parent_id, $simpleProductIds);
注意如果不起作用:您可以尝试$ parent = $ product;
代码是
foreach ($products as $sku => $row)
{
$Parentproduct = Mage::getModel('catalog/product');
$Parentproduct>setAttributeSetId(4)
->setTypeId('configurable')
->setStockData(array(
'manage_stock' => 0,
'min_sale_qty' => 0,
'max_sale_qty' => 0,
))
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setTaxClassId(1)
->setCreatedAt(time())
->setName($row['name'])
->setSku($sku)
->setPrice($row['price'])
->setCategoryIds(array(2))
->setStatus(1) // enabled
->setWeight(1)
->setWebsiteIDs(array(1))
->setDescription($row['description'])
->setShortDescription($row['short_desc']);
try
{
$Parentproduct->save();
$parent_id = $Parentproduct->getId();
echo 'Configurable Product created: ' . $parent_id . "\n";
if (!empty($parent_id))
{
foreach ($row['children'] as $child_sku => $child)
{
$product = Mage::getModel('catalog/product');
$product->setAttributeSetId(4)
->setTypeId('simple')
->setStockData(array(
'manage_stock' => 0,
'min_sale_qty' => 0,
'max_sale_qty' => 0,
))
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setTaxClassId(1)
->setCreatedAt(time())
->setName($row['name'] . ' ' . $color)
->setSku($sku . $child_sku)
->setPrice($row['price'])
->setcategoryIds(array(2))
->setStatus(1)
->setWeight(1)
->setWebsiteIDs(array(1))
->setDescription($row['description'])
->setShortDescription($row['short_desc']);
$product->save();
echo 'Simple product created: ' . $product->getId() . "\n";
}
echo "\n";
$Parentproduct
Mage::getResourceSingleton('catalog/product_type_configurable')
->saveProducts($Parentproduct, $simpleProductIds);
//or
Mage::getResourceModel('catalog/product_type_configurable')
->saveProducts($Parentproduct, $simpleProductIds);
}
}
catch (Exception $e)
{
echo 'Unable to create product', "\n";
echo $e->getMessage() . "\n";
}
}