我正在使用此代码以编程方式创建magento中的产品
我有2个关于产品标签和产品页面布局的相关问题。
<?php
//$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku('some-sku-value-here');
$product->setAttributeSetId('9');# 9 is for default
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setCategoryIds(array(42)); # some cat id's,
$product->setWebsiteIDs(array(1)); # Website id, 1 is default
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setPrice(39.99); # Set some price
//Default Magento attribute
$product->setWeight(4.0000);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # default tax class
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
$product->setCreatedAt(strtotime('now'));
try {
$product->save();
}
catch (Exception $ex) {
//Handle the error
}
?>
1)我也试图改变programmaticaly产品页面布局 - 它总是应该是“1页面布局”。
那么如何将programmaticaly id = page_layout设置为“1页面布局”?
2)我已经知道我无法在产品中添加标签。只有相反的情况才有可能。
那么如何以编程方式将标签关系添加到产品ID?
答案 0 :(得分:1)
尝试使用此页面布局
$product->setPageLayout('one_column');
对于标签试试这个:
假设您已经拥有标识为$tagId
$productsIds = array(1,4,5,6);//put here your product ids.
$tag = Mage::getModel('tag/tag')->load($tagId);
$tagRelationModel = Mage::getModel('tag/tag_relation');
$tagRelationModel->addRelations($tag, $productIds);
$model->save();//not sure if this line is needed.