Le chargement d'une collection de produits par un attribut spécifique déroulante valeur
-
13-12-2019 - |
Question
Je suis en train de charger un spécifique des produits virtuels avec ses options de personnalisation, mais au lieu de cela, je reçois un tableau vide.
Mon attribut est 'part_identifier" dans l'attribut 'boat_part" C'est une liste déroulante type d'attribut avec plusieurs options.L'option que je suis en train de tirer est "Look".
Voici mon code:
$attribute = 'msh_boat_part_identifier';
$boatLookProducts = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter($attribute, array('like'=>'Look'))
->load()
->getItems();
foreach($boatLookProducts as $boatLookProduct)
{
$return[] = array(
"id" => (int)$boatLookProduct->getEntityId(),
"name" => $boatLookProduct->getName(),
"type" => "profile",
"shortDesc" => $boatLookProduct->getShortDescription(),
"desc" => $boatLookProduct->getDescription(),
"active" => ($boatLookProduct->getStatus() === "2" ? false : true)
);
}
echo Mage::helper('pb')->ConvertToJson($return);
Dans ma requête pour la collecte, si je change le tableau
->addAttributeToFilter($attribute, array('like'=>'%%'))
il va réussir à retourner tous les proructs, mais je ne peux pas obtenir le spécifique un je veux!
La solution
Comme part_identifier
est une liste déroulante attribut, vous ne pouvez pas filtrer la collection par l'option étiquette.
Afin de filtrer une collection par une option, vous avez besoin pour obtenir que l'option de l'étiquette d'identification du (réelle valeur d'option).
Ensuite, vous pouvez filtrer la collection par l'option ID:
// use your own attribute code here
$attributeCode = 'your_attribute_here';
$attributeOption = 'Look';
$attributeDetails = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
$options = $attributeDetails->getSource()->getAllOptions(false);
$selectedOptionId = false;
foreach ($options as $option){
// print_r($option) and find all the elements
echo $option['value'];
echo $option['label'];
if ($option['label'] == $attributeOption) {
$selectedOptionId = $option['value'];
}
}
if ($selectedOptionId) {
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter($attributeCode, array('eq' => $selectedOptionId))
}
Autres conseils
Ceci est en retard et peut-être même pas valable au moment de la demander, même si j'ai trouvé une manière plus concise en faisant:
$attributeOptionId = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'your_attribute_code')
->getSource()
->getOptionId('Option Text Value/Label');
Ensuite, vous l'utilisez dans le filtre de collection comme d'habitude