Script de configuration Supprimer l'attribut Group à partir de l'attribut
-
13-12-2019 - |
Question
J'ai mis en place un script qui crée un nouvel ensemble d'attributs.Et j'ai besoin Supprimer les groupes d'attributs "Options-cadeaux" et "Profil récurrent" de l'ensemble d'attributs créés.Comment puis-je faire cela?
<?php
$installer=$this;
$installer->startSetup();
$inheritBase = $installer->getDefaultAttributeSetId('catalog_product');
$attributeSetName = 'Custom Name';
$ignoreAttributes = array(
'news_from_date' => 'news_from_date',
'news_to_date' => 'news_to_date',
'weight' => 'weight',
'country_of_manufacture' => 'country_of_manufacture',
'manufacturer' => 'manufacturer',
'featured' => 'featured',
'color' => 'color',
'delivery_time' => 'delivery_time',
'shipping_charges' => 'shipping_charges',
'marketplace_state' => 'marketplace_state',
'generate_meta' => 'generate_meta',
'percent_donat' => 'percent_donat',
'organization_charity' => 'organization_charity',
'gift_message_available' => 'gift_message_available',
'recurring_profile' => 'recurring_profile',
'is_recurring' => 'is_recurring'
);
$installer->addAttributeSet('catalog_product', $attributeSetName);
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($inheritBase)
->load();
$currentSet = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($installer->getAttributeSetId('catalog_product', $attributeSetName))
->load();
$aCurrentGroups = array();
foreach($currentSet AS $group)
{
$aCurrentGroups[$group->attribute_group_name] = array("id" => $group->getId(), "sort_order" => $group->sort_order);
}
foreach ($groups as $group) {
$groupId = 0;
$sort_order = 0;
if(!isset($aCurrentGroups[$group->attribute_group_name])) {
$installer->addAttributeGroup('catalog_product', $attributeSetName, $group->attribute_group_name, $group->sort_order);
$newGroup = $installer->getAttributeGroup('catalog_product', $attributeSetName, $group->attribute_group_name);
$groupId = $newGroup['attribute_group_id'];
$sort_order = $newGroup['sort_order'];
} else {
$groupId = $aCurrentGroups[$group->attribute_group_name]["id"];
$sort_order = $aCurrentGroups[$group->attribute_group_name]["sort_order"];
}
$groupAttributesCollection = Mage::getModel('eav/entity_attribute')
->getResourceCollection()
->setAttributeGroupFilter($group->getId())
->load();
foreach ($groupAttributesCollection as $attribute) {
if(!isset($ignoreAttributes[$attribute->getAttributeCode()])) {
$installer->addAttributeToGroup('catalog_product', $attributeSetName, $groupId, $attribute->getId(), $attribute->sort_order);
}
}
}
$installer->endSetup();
La solution
Essayez le code ci-dessous pour supprimer le groupe spécifique et les attributs spécifiques tout en créant un nouvel attribut défini par script.Ça marche pour moi,
<?php
$installer = $this;
$installer->startSetup();
$attributeSetName = 'Custom Name';
$ignoreGroup = array('Gift Options','Recurring Profile');//Add Attribute Group Name To Remove
$ignoreAttribute = array('news_to_date','weight','news_from_date');//Add Attribute Code to Remove
$entityTypeId = Mage_Catalog_Model_Product::ENTITY;
$installer->addAttributeSet($entityTypeId,$attributeSetName,0);
$inheritBase = $installer->getDefaultAttributeSetId($entityTypeId);
$newAttributeSetId = $installer->getAttributeSetId($entityTypeId,$attributeSetName);
$groups = Mage::getModel('eav/entity_attribute_group')->getResourceCollection()->setAttributeSetFilter($inheritBase)->load();
foreach($groups as $group){
if(!in_array($group->getAttributeGroupName(),$ignoreGroup))
{
$installer->addAttributeGroup($entityTypeId,$newAttributeSetId,$group->getAttributeGroupName(),$group->getSortOrder());//Add Group To new Attribute Set
$newGroup = $installer->getAttributeGroup($entityTypeId,$newAttributeSetId,$group->getAttributeGroupName());
$attributeCollections = Mage::getModel('eav/entity_attribute')->getResourceCollection()->setAttributeGroupFilter($group->getId())->load();
foreach($attributeCollections as $attribute){
if(!in_array($attribute->getAttributeCode(),$ignoreAttribute))
{
$installer->addAttributeToGroup($entityTypeId,$newAttributeSetId,$newGroup['attribute_group_id'],$attribute->getId(),$attribute->getSortOrder());
}
}
}
}
$installer->endSetup();
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange