我正在開發一個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屬性對於可配置產品和簡單產品都是必需的。當我從管理面板看這些產品時,一切似乎都很好。我甚至嘗試將簡單產品手動關聯到可配置,但以編程方式創建的產品不會顯示在關聯產品部分。
我正在使用此答案中的代碼,但它不會在商店中顯示。它將簡單的產品鏈接到可配置的產品,但數據庫中沒有鏈接。如何將顏色鏈接到可配置產品? – Refilon