[英]Magento - Programmatically create custom product attribute via set up script
[英]create attribute set in Magento setup script
創建屬性並將它們分配給現有屬性集是一個已解決的問題,但我們嘗試創建一個屬性集並使用默認屬性填充該屬性集時遇到了問題,並且特定屬性失敗。 這是使用的代碼:
$setup->addAttributeSet('catalog_product', 'women_sizing_denim');
$oAttributeSetModel = Mage::getModel("eav/entity_attribute_set")
->load($setup->getAttributeSetId('catalog_product', 'women_sizing_denim'))
->initFromSkeleton($setup->getAttributeSetId('catalog_product', 'default'))
->save();
我可以通過調試來驗證initfromSkeleton
方法確實從廣告中加載默認的attribute_set中的屬性,但是在save()
,新集合為空。
可以向集合中添加新屬性,因此它確實存在並且可以正確創建,但是缺少默認屬性會使其不可用,因為都需要SKU,價格,名稱等。
我記得,基於默認屬性集創建屬性集的問題是,您需要保存兩次屬性集,一次是在調用initSkeleton()
之前,一次是在調用之后。
我已經不記得確切的原因了,這是很久以前的事了。 無論如何,這對我有用:
// Mage_Eav_Model_Entity_Setup
$oEntitySetup = $this;
$oEntitySetup->startSetup();
$sNewSetName = 'myset';
$iCatalogProductEntityTypeId = (int) $oEntitySetup->getEntityTypeId('catalog_product');
$oAttributeset = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId($iCatalogProductEntityTypeId)
->setAttributeSetName($sNewSetName);
if ($oAttributeset->validate()) {
$oAttributeset
->save()
->initFromSkeleton($iCatalogProductEntityTypeId)
->save();
}
else {
die('Attributeset with name ' . $sNewSetName . ' already exists.');
}
$oEntitySetup->endSetup();
請注意,安裝程序類需要擴展
Mage_Catalog_Model_Resource_Eav_Mysql4_Setup
以便
$oEntitySetup->getEntityTypeId('catalog_product');
可以叫。
我使用了JürgenThelen的答案。
但是我發現新的屬性集沒有默認選項和選項組,例如常規和發票等。
因此,要解決這個問題,請在initFromSkeleton()中包含$ installer-> getAttributeSetId('catalog_product','default')
if($attributeSet->validate()) {
$attributeSet
->save()
->initFromSkeleton($installer->getAttributeSetId('catalog_product', 'default'))
->save();
} else {
die('Attributeset with name ' . $setName . ' already exists.');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.