Comprobación de la existencia del valor de atributo. ¿Qué hace esto?
-
16-10-2019 - |
Pregunta
Tengo este código de aquí, y funciona. Sin embargo, hay una sección que no tengo del todo. Consulte las líneas [A] y [B].
public function attributeValueExists($attCode, $attributeValue)
{
$attributeModel = Mage::getModel('eav/entity_attribute');
$attributeOptionModel = Mage::getModel('eav/entity_attribute_source_table') ;
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
$attribute = $attributeModel->load($attributeId); // [B]
$attributeTable = $attributeOptionModel->setAttribute($attribute);
$options = $attributeOptionModel->getAllOptions(false);
foreach($options as $option) {
if ($option['label'] == $attributeValue) {
return $option['value'];
}
}
return false;
}
La variable obtenida de las líneas [a] y [b], $attributeTable
, no parece ser usado. Sin embargo, si comento estas dos líneas, el código no funciona. ¿Qué están haciendo realmente estas líneas y por qué lo hacen? $attributeTable
¿por?
Solución
Estás equivocado cuando dices que "no se usan". Son, de hecho:
Esta línea obtiene un ATTRIBUDE $:
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
Esta línea carga el atributo:
$attribute = $attributeModel->load($attributeId); // [B]
Esta línea usa el atributo $ en un setter en $ attributeoptionModel:
$attributeTable = $attributeOptionModel->setAttribute($attribute);
GetAlloptions requiere que el atributo se haya establecido:
$options = $attributeOptionModel->getAllOptions(false);
Creo que dónde te están tropezando es que $ AttributeTable no se usa, pero ese es solo el valor de retorno del método Setter. Magic Setters en Magento siempre devuelve el objeto principal para que pueda construir una interfaz fluida.
Otros consejos
Esta función devuelve el valor del atributo que es este atributo es seleccionar o seleccionar múltiples. Podemos escribir esta función similar:
public function attributeValueExists($attCode, $attributeValue)
{
$attributeModel = Mage::getModel('eav/entity_attribute');
$attributeOptionModel = Mage::getModel('eav/entity_attribute_source_table') ;
//retrieve attribute_id using attribute_code
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
//load attribute model using attribute_id
$attribute = $attributeModel->load($attributeId); // [B]
$options = $attributeOptionModel->setAttribute($attribute)->getAllOptions(false);
foreach($options as $option) {
if ($option['label'] == $attributeValue) {
return $option['value'];
}
}
return false;
}
Pero esto es largo y podemos usar este fragmento para recuperar el valor del atributo:
public function attributeValueExists($attCode, $attributeValue)
{
/**
* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract
*/
$attribute = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode(Mage_Catalog_Model_Product::ENTITY, $attCode);
$options = $attribute->getSource()->getAllOptions();
foreach ($options as $option) {
if ($option['label'] == $attributeValue) {
return $option['value'];
}
}
return false;
}