Pregunta

Estoy tratando de cargar un producto virtual específico con sus opciones personalizadas, pero en lugar de eso, sigo recibiendo una matriz vacía.

Mi atributo es 'part_identifier' en el conjunto de atributos 'boat_part' Es un atributo de tipo desplegable con varias opciones.La opción que estoy tratando de tirar por 'look'.

Aquí está mi 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);

En mi consulta para la colección, si cambio la matriz a

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

devolverá con éxito a todos los prorruptos, ¡pero no puedo obtener lo específico que quiero!

¿Fue útil?

Solución

Como part_identifier es un atributo desplegable, no puede filtrar la colección por la etiqueta de la opción.

Para filtrar una colección mediante una opción, debe obtener la ID de la etiqueta de esa opción (valor de opción real).

Luego, puede filtrar la colección por esa opción 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))
}

Otros consejos

Esto es tarde y tal vez ni siquiera es válido en el momento de preguntar, aunque encontré una forma más concisa haciendo:

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

Luego lo usas en el filtro de colección como de costumbre

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top