【发布时间】:2015-04-08 17:00:50
【问题描述】:
我正在尝试以编程方式创建可配置产品。我只对每个尺寸属性的价格有疑问。管理面板中的定价值始终为空。这是同样的问题,但没有答案Creating configurable products programmatically - pricing_value not saved我正在使用magento community 1.9.1
try {
$configProduct = new Mage_Catalog_Model_Product();
$configProduct->setSku('dp_' . strtotime('now'))
->setAttributeSetId(9)
->setTypeId('configurable')
->setName('Some cool product name')
->setCategoryIds(array(3))
->setWebsiteIDs(array(1))
->setDescription('Full description here')
->setShortDescription('Short description here')
->setPrice(39.99)
->setWeight(1.0000)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setStatus(1)
->setTaxClassId(0)
->setMediaGallery (array('images' => array (), 'values' => array ()))
->addImageToMediaGallery($path, $mediaAttribute, false, false )
->setStockData(array(
'is_in_stock' => 1,
'qty' => 999,
'use_config_manage_stock' => 1,
'is_salable' => 1
))
->setCreatedAt(strtotime('now'));
$configProduct->setCanSaveConfigurableAttributes(true);
$configProduct->setCanSaveCustomOptions(true);
$configProduct->getTypeInstance()->setUsedProductAttributeIds(array(132)); //attribute ID of attribute 'size' in my store
$configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray();
$configProduct->setConfigurableAttributesData($configurableAttributesData);
$configurableProductsData = array();
$configurableProductsData['10'] = array(
'0' => array(
'label' => (string)Mage::getModel('catalog/product')->load(10)->getName(),
'attribute_id' => '132', //attribute ID of attribute 'size' in my store
'value_index' => '3', //value of 'X' index of the attribute 'size'
'is_percent' => false, //fixed/percent price for this option
'pricing_value' => '21.00' //value for the pricing
)
);
$configurableProductsData['11'] = array(
'0' => array(
'label' => (string)Mage::getModel('catalog/product')->load(11)->getName(),
'attribute_id' => '132', //attribute ID of attribute 'size' in my store
'value_index' => '4', //value of 'XL' index of the attribute 'size'
'is_percent' => false, //fixed/percent price for this option
'pricing_value' => '21.00' //value for the pricing
)
);
$configProduct->setConfigurableProductsData($configurableProductsData);
$configProduct->save();
$createdProductUrl = Mage::getModel('catalog/product')->loadByAttribute('sku', $configProduct->getSku())->getUrlPath();
echo json_encode(array("createdProductUrl"=>"http://magento.dev/index.php/test/" . $createdProductUrl));
} catch (Exception $ex) {
echo $ex->getMessage();
}
【问题讨论】:
-
可能需要定义基础货币?
标签: php magento magento-1.7