magento attributes product

Atributo de producto de Magento Obtener valor



attributes (13)

¿Cómo obtener un valor de atributo de producto específico si conozco la identificación del producto sin cargar todo el producto?


Este funciona-

echo $_product->getData(''ATTRIBUTE_NAME_HERE'');


No tiene que cargar todo el producto. Las colecciones de Magentos son muy poderosas e inteligentes.

$collection = Mage::getModel(''catalog/product'')->getCollection(); $collection->addAttributeToFilter(''entity_id'', $product->getId()); $collection->addAttributeToSelect(''manufacturer''); $product = $collection->getFirstItem(); $manufacturer = $product->getAttributeText(''manufacturer'');

En el momento en que llame a getFirstItem (), la consulta se ejecutará y el producto resultante es muy mínimo:

[status] => 1 [entity_id] => 38901 [type_id] => configurable [attribute_set_id] => 9 [manufacturer] => 492 [manufacturer_value] => JETTE [is_salable] => 1 [stock_item (Varien_Object)] => Array ( [is_in_stock] => 1 )


Parece imposible obtener valor sin cargar el modelo del producto. Si echas un vistazo a la aplicación de archivo / code / core / Mage / Eav / Model / Entity / Attribute / Frontend / Abstract.php, verás el método

public function getValue(Varien_Object $object) { $value = $object->getData($this->getAttribute()->getAttributeCode()); if (in_array($this->getConfigField(''input''), array(''select'',''boolean''))) { $valueOption = $this->getOption($value); if (!$valueOption) { $opt = new Mage_Eav_Model_Entity_Attribute_Source_Boolean(); if ($options = $opt->getAllOptions()) { foreach ($options as $option) { if ($option[''value''] == $value) { $valueOption = $option[''label'']; } } } } $value = $valueOption; } elseif ($this->getConfigField(''input'')==''multiselect'') { $value = $this->getOption($value); if (is_array($value)) { $value = implode('', '', $value); } } return $value; }

Como puede ver, este método requiere un objeto cargado para obtener datos (tercera línea).


Podría escribir un método que lo haría directamente a través de sql, supongo.

Se vería algo como esto:

Variables:

$store_id = 1; $product_id = 1234; $attribute_code = ''manufacturer'';

Consulta:

SELECT value FROM eav_attribute_option_value WHERE option_id IN ( SELECT option_id FROM eav_attribute_option WHERE FIND_IN_SET( option_id, (SELECT value FROM catalog_product_entity_varchar WHERE entity_id = ''$product_id'' AND attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code=''$attribute_code'') ) ) > 0) AND store_id=''$store_id'';

Sin embargo, debería obtener el valor de la tabla correcta en función del atributo backend_type (campo en eav_attribute), por lo que lleva al menos 1 consulta adicional.


Primero debemos asegurarnos de que el atributo deseado esté cargado y luego emitirlo. Utilizar esta:

$product = Mage::getModel(''catalog/product'')->load(''<product_id>'', array(''<attribute_code>'')); $attributeValue = $product->getResource()->getAttribute(''<attribute_code>'')->getFrontend()->getValue($product);


Prueba esto

$attribute = $_product->getResource()->getAttribute(''custom_attribute_code''); if ($attribute) { echo $attribute_value = $attribute ->getFrontend()->getValue($_product); }


Puedes obtener el valor del atributo siguiendo el camino

$model = Mage::getResourceModel(''catalog/product''); $attribute_value = $model->getAttributeRawValue($productId, ''attribute_code'', $storeId);


Si tiene un atributo text / textarea llamado my_attr, puede obtenerlo por: product->getMyAttr();


Una forma que conozco:

$product->getResource()->getAttribute($attribute_code) ->getFrontend()->getValue($product)


puedes usar

<?php echo $product->getAttributeText(''attr_id'') ?>


Consulte la answer Daniel Kocherga , ya que funcionará para usted en la mayoría de los casos.

Además de ese método para obtener el valor del atributo, a veces puede querer obtener la etiqueta de un select o multiselect . En ese caso, he creado este método que almaceno en una clase de ayudante:

/** * @param int $entityId * @param int|string|array $attribute atrribute''s ids or codes * @param null|int|Mage_Core_Model_Store $store * * @return bool|null|string * @throws Mage_Core_Exception */ public function getAttributeRawLabel($entityId, $attribute, $store=null) { if (!$store) { $store = Mage::app()->getStore(); } $value = (string)Mage::getResourceModel(''catalog/product'')->getAttributeRawValue($entityId, $attribute, $store); if (!empty($value)) { return Mage::getModel(''catalog/product'')->getResource()->getAttribute($attribute)->getSource()->getOptionText($value); } return null; }


$orderId = 1; // YOUR ORDER ID $items = $block->getOrderItems($orderId); foreach ($items as $item) { $options = $item->getProductOptions(); if (isset($options[''options'']) && !empty($options[''options''])) { foreach ($options[''options''] as $option) { echo ''Title: '' . $option[''label''] . ''<br />''; echo ''ID: '' . $option[''option_id''] . ''<br />''; echo ''Type: '' . $option[''option_type''] . ''<br />''; echo ''Value: '' . $option[''option_value''] . ''<br />'' . ''<br />''; } } }

todas las cosas que usará para recuperar el orden de pedido de la opción personalizada del producto de valor en Magento 2: https://www.mageplaza.com/how-get-value-product-custom-option-cart-order-magento-2.html


Mage::getResourceModel(''catalog/product'')->getAttributeRawValue($productId, ''attribute_code'', $storeId);