¿Cómo obtengo el nombre del conjunto de atributos?


Estoy tratando de obtener el nombre del conjunto de atributos en la plantilla de vista de producto de Magento. Puedo obtener el valor del atributo por $_product->getAttributeText('attribute'), pero ¿cómo obtengo el nombre del conjunto de atributos?

Me gustaría mostrar un atributo solo si pertenece a un determinado conjunto de atributos.

Author: Mahmood Rehman, 2010-01-19

5 answers

Siempre que tenga un objeto product, puede acceder a su conjunto de atributos de la siguiente manera:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName  = $attributeSetModel->getAttributeSetName();

Esto le dará el nombre del conjunto de atributos, que luego puede comparar usando strcmp:

if(0 == strcmp($attributeSetName, 'My Attribute Set')) {
    print $product->getAttributeText('attribute');
}

¡Espero que eso ayude!

 68
Author: Joseph Mastey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-15 17:42:45

Para más sexiedad puedes acortarlo a:

$attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
 24
Author: trish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-29 17:36:07

Intente el siguiente código:

$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetName   = 'Default';
$attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_name', $attributeSetName)
                    ->getFirstItem()
                    ->getAttributeSetId();
echo $attributeSetId;

Encuentre más información sobre el Conjunto de atributos en el siguiente artículo.

Gracias

 13
Author: MagePsycho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-10 16:23:29

La respuesta de Joe requiere un par de alteraciones para que funcione.

En primer lugar debe ser $_product no product product, y en segundo lugar hay un error ')' en la última línea.

El siguiente código debe ser correcto:

$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($_product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
 1
Author: Bit32,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-11 15:44:28

La comparación con un valor de texto puede tener problemas si los usuarios deciden cambiar más tarde ese texto, lo cual es fácil de hacer en Magento para conjuntos de atributos. Otra opción es utilizar el id subyacente en su lugar que nunca va a cambiar.

Puede obtener esto buscando el valor de la columna attribute_set_id en la base de datos usando

select * from eav_attribute_set;

Este número también está en el enlace editar en admin que está en negrita abajo

Http://.../Indice.php/admin/catalog_product_set/edit/id/10/key/6fe89fe2221cf2f80b82ac2ae457909ce04c92c51716b3e474ecad672a2ae2f3/

Su código simplemente usaría esa propiedad del producto. Basado en el id de 10 en el enlace anterior, esto solo sería

if (10 == $_product->getAttributeSetId()) {
  //Do work
}
 0
Author: Andrew Rutter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-25 10:28:55