Dodaj niestandardowe atrybuty do niestandardowego zestawu atrybutów programowo


10

Cześć Czy ktoś może mi w tym pomóc?

Utworzyłem niestandardowy zestaw atrybutów i niestandardowy atrybut jako

$installer = $this;
/* @var $installer Mage_Eav_Model_Entity_Setup */
$installer->startSetup();

//Create Attribute set with Based on Default attribute set
//$installer->removeAttributeSet(Mage_Catalog_Model_Product::ENTITY, 'New Attr Set');
/*
$skeletonID=$installer->getAttributeSetId('catalog_product','Default');
$entityTypeId = Mage::getModel('catalog/product')
->getResource()
->getEntityType()
->getId(); //product entity type

$attributeSet = Mage::getModel('eav/entity_attribute_set')
->setEntityTypeId($entityTypeId)
->setAttributeSetName("New Attr Set");

$attributeSet->validate();
$attributeSet->save();

$attributeSet->initFromSkeleton($skeletonID)->save();


//Create attribute new_attr
//$installer->removeAttribute('catalog_product', 'new_attr');
$data= array (
'attribute_set' =>  'New Attr Set',
'group' => 'General',
'label'    => 'New Attr',
'visible'     => true,
'type'     => 'int', // multiselect uses comma-sep storage
'input'    => 'boolean',
'system'   => false,
'required' => false,
'user_defined' => 1,//defaults to false; if true, define a group
'source' => 'eav/entity_attribute_source_boolean',
'default' => 1,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
 );

 $installer->addAttribute('catalog_product','new_attr',$data);
 */

Ten kod dodaje atrybut „new_attr” do grupy „General”, więc atrybut niestandardowy jest wyświetlany we wszystkich zestawach atrybutów, takich jak „Default”.

Chcę dodać niestandardowy atrybut „new_attr” tylko do niestandardowego zestawu atrybutów „Nowy zestaw atrybutów” w grupie „Ogólne”. Czy to jest możliwe?


Czy to w ogóle możliwe do osiągnięcia?
Magento_Newbie,

Odpowiedzi:


18

Tak, jest to możliwe.

Najpierw ustaw te klucze w tablicy danych $ na następujące wartości, aby uniknąć dodawania atrybutu do wszystkich zestawów atrybutów:

'user_defined'         => true,
'group'                => ''

Następnie dodaj atrybut do zestawu atrybutów:

$attributeSetId = $this->getAttributeSetId($entityTypeId, 'New Attr Set');
$this->addAttributeToSet($entityTypeId, $attributeSetId, 'General', 'new_attr', 10);

0

Moja funkcja dodawania atrybutu (według kodu) do zestawu atrybutów

    public function addToAttributeSet($code, $attributeSetName = 'Default', $groupName = 'Details')
{
    try {
        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');

        $attributeSetId = $setup->getAttributeSetId('catalog_product', $attributeSetName);
        $attributeGroupId = $setup->getAttributeGroupId('catalog_product', $attributeSetId, $groupName);
        $attributeId = $setup->getAttributeId('catalog_product', $code);

        $setup->addAttributeToSet($entityTypeId = 'catalog_product', $attributeSetId, $attributeGroupId, $attributeId);

        $this->_success[] = 'Added Attribute to SET ' . $attributeSetName . ' (' . $code . ')';
        return true;

    } catch (Exception $e) {
        $this->_errors[] = 'ERROR when added Attribute to SET ' . $attributeSetName . ' (' . $code . ')';
        $this->_errors[] = $e->getMessage();
        return false;
    }
}
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.