Pergunta

Estou tentando carregar produtos virtuais específicos com suas opções personalizadas, mas continuo recebendo uma matriz vazia.

Meu atributo é 'part_identifier' no conjunto de atributos 'boat_part'. É um atributo do tipo suspenso com várias opções.A opção que estou tentando usar é 'Olhar'.

Aqui está o meu código:

$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);

Na minha consulta para a coleção, se eu alterar o array para

->addAttributeToFilter($attribute, array('like'=>'%%'))

ele retornará com êxito todos os produtos, mas não consigo obter o específico que desejo!

Foi útil?

Solução

Como part_identifier for um atributo suspenso, você não poderá filtrar a coleção pelo rótulo da opção.

Para filtrar uma coleção por uma opção, você precisa obter o ID desse rótulo de opção (valor real da opção).

Então você pode filtrar a coleção por esse ID de opção:

// 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))
}

Outras dicas

Isso é tarde e talvez nem seja válido no momento de perguntar, embora eu tenha encontrado uma maneira mais concisa:

$attributeOptionId = Mage::getSingleton('eav/config')
   ->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'your_attribute_code')
   ->getSource()
   ->getOptionId('Option Text Value/Label');

Então você usa no filtro de coleta normalmente

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top