すべての属性セットに属性を持つ属性グループを作成して追加する
-
12-12-2019 - |
質問
既存のインストールのすべての属性セットに6つの属性を持つ属性グループを作成して追加する必要があります。
属性グループを作成するためのスクリプトを見つけました、これは素晴らしい機能ですが、これはこの新しいグループに属性を追加することはできません。
あらゆる助けが高く評価されます。
このスクリプトは、属性グループを追加するのに最適ですが、新しいグループに6つの属性を追加する方法:
<?php
//enable errors to see if something is wrong
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
//include Mage.php
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
//tell magento that you are running on developer mode, for additional error messages (if any)
Mage::setIsDeveloperMode(true);
//instantiate the application
Mage::app();
//get the type ID of the product - you will need it later
$entityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId();
//get all attribute sets
$sets = Mage::getModel('eav/entity_attribute_set')
->getResourceCollection()
//filter only sets for products - that's why you needed the product type ID
->addFilter('entity_type_id', $entityTypeId);
//loop through all the sets
foreach ($sets as $set){
//create an attribute group instance
$modelGroup = Mage::getModel('eav/entity_attribute_group');
//set the group name
$modelGroup->setAttributeGroupName('Some group name here') //change group name
//link to the current set
->setAttributeSetId($set->getId())
//set the order in the set
->setSortOrder(100);
//save the new group
$modelGroup->save();
}
. 解決
セットアップスクリプトを介してそれをするのがずっと簡単です。これが例です。
// Add new Attribute group
$groupName = 'My Attr Group';
$entityTypeId = $installer->getEntityTypeId('catalog_product');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$installer->addAttributeGroup($entityTypeId, $attributeSetId, $groupName, 100);
$attributeGroupId = $installer->getAttributeGroupId($entityTypeId, $attributeSetId, $groupName);
// Add existing attribute to group
$attributeId = $installer->getAttributeId($entityTypeId, ATTRIBUTE_CODE);
$installer->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, $attributeId, null);
.
注:$installer
変数はMage_Eav_Model_Entity_Setup
クラスのインスタンスです。あなたはさらなる参考のためにそこを見ることができます。いくつかの例についてはapp/code/core/Mage/Catalog/sql/catalog_setup
フォルダを調べることもできます。
他のヒント
インストーラスクリプトを使用する
を試してください
$filters = array(
'my_attribute_id_1',
'my_attribute_id_2'
..
);
$product_tab = 'General';
foreach($filters as $value){
$eavModel = Mage::getModel('eav/entity_setup','core_setup');
$attributeId = $eavModel->getAttributeId('catalog_product', $value);
foreach($eavModel->getAllAttributeSetIds('catalog_product') as $id) {
$attributeGroupId = $eavModel->getAttributeGroupId('catalog_product', $id, $product_tab);
$eavModel->addAttributeToSet('catalog_product', $id, $attributeGroupId, $attributeId);
}
}
. あなたがしたいのなら...
- 新製品属性を作成する
- すべてのセットの新しい属性グループ
- すべてのセット内の関連グループへのASIGN属性
- または既存の属性の変更(Group、Position、...)
...あなたはこれを試すことができます。後で再利用するためにセットアップスクリプトに入れることをお勧めします...
./** @var Mage_Catalog_Model_Resource_Setup $setup */ $setup = Mage::getResourceModel('catalog/setup', 'default_setup'); $entity = Mage_Catalog_Model_Product::ENTITY; $entityTypeId = $setup->getEntityTypeId($entity); ## config here: $attributesDataMap = ... see below foreach ($setup->getAllAttributeSetIds($entityTypeId) as $attributeSetId) { foreach ($attributesDataMap as $groupName => $groupData) { # add groups to all sets OR change position $setup->addAttributeGroup( $entityTypeId, $attributeSetId, $groupName, $groupData['sort_order'] ); if (count($groupData['attributes'])) { foreach ($groupData['attributes'] as $attributeCode => $attributeData) { $attributeId = $setup->getAttributeId($entityTypeId, $attributeCode); $attributeData = array_merge( ['group' => $groupName], $defaultData, $groupData['default'], $attributeData ); # add new attribute if (!$attributeId) { $setup->addAttribute( $entityTypeId, $attributeCode, $attributeData ); # update attribute psotion and group } else { $setup->updateAttribute( $entityTypeId, $attributeCode, $attributeData ); $setup->addAttributeToSet( $entityTypeId, $attributeSetId, $groupName, $attributeId, $attributeData['sort_order'] ); } } } } }
config:
私の場合のネストされた配列は、属性グループ名、位置、関連属性を保持しています...
.$defaultData = [ 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'backend' => '', 'visible' => true, 'searchable' => false, 'filterable' => false, 'comparable' => false, 'is_configurable' => false, 'visible_on_front' => false, 'visible_in_advanced_search' => false, 'used_in_product_listing' => false, 'user_defined' => false, 'required' => true, ]; $attributesDataMap = [ 'General' => [ 'sort_order' => null, 'default' => $defaultData, 'attributes' => [ 'ean' => [ 'label' => 'EAN', 'sort_order' => '3', ], 'mpn' => [ 'label' => 'MPN', 'sort_order' => '4', ], ], ], 'eBay' => [ 'sort_order' => 80, 'default' => $defaultData, 'attributes' => [ 'ebay_title' => [ 'label' => 'eBay Titel', 'sort_order' => '10', 'frontend_class' => 'validate-length maximum-length-80', 'note' => 'Maximum 80 chars', ], ... ] ] ];
所属していません magento.stackexchange