Checking for attribute value existence. What does this do?
-
16-10-2019 - |
سؤال
I got this code from here, and it works. There's a section of it that I don't quite get, however. Please see lines [A] and [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;
}
The variable obtained from lines [A] and [B], $attributeTable
, doesn't seem to be used. Yet, if I comment these two lines out, the code doesn't work. What are these lines actually doing and why do $attributeTable
for?
المحلول
You are mistaken when you say they're "not used". They are, in fact:
This line fetches an $attributeId:
$attributeId = $attributeModel->getIdByCode('catalog_product', $attCode); // [A]
This line loads the attribute:
$attribute = $attributeModel->load($attributeId); // [B]
This line uses the $attribute in a setter on the $attributeOptionModel:
$attributeTable = $attributeOptionModel->setAttribute($attribute);
getAllOptions requires that the attribute have been set:
$options = $attributeOptionModel->getAllOptions(false);
I think where you're getting tripped up is that $attributeTable is not used, but that's just the return value from the setter method. Magic setters in Magento always return the parent object so that you can construct a fluent interface.
نصائح أخرى
This function returns attribute value which is this attribute is select or multiple select. We can write this function 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;
}
But this is long way and we can use this snippet to retrieve attribute value:
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;
}