Pregunta

Estoy tratando de modificar algún código para emitir productos con un atributo 'destacado' establecido en "SÍ"

Aquí está lo que estoy trabajando:

<?php

 class Mage_Catalog_Block_Product_Featured extends Mage_Catalog_Block_Product_Abstract{
    public function __construct(){
        parent::__construct();


        $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'featured');
        $attributeId = $attribute->getId();


        $storeId    = Mage::app()->getStore()->getId();
        $products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image', 'featured'))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->addViewsCount();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        $products->setPageSize(16)->setCurPage(1);
        $this->setProductCollection($products);
    }
}

Esto hace productos de salida, pero no los destacados.

  • ¿Cómo modifico la consulta para que solo seleccione los productos destacados
  • Parece que está agregando todos los atributos a la selección, ¿es correcto y cómo salgo un valor de atributo en mi plantilla de PHTML?
¿Fue útil?

Solución

trata de hacer como

$products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image', 'featured'))
            ->addAttributeToFilter('featured',array('eq'=>true))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->addViewsCount();

También asegúrese de que su código de atributo sea featured

en el archivo phtml puede obtener atributo como

<?php $_helper = $this->helper('catalog/output'); ?>
<?php echo $_helper->productAttribute($_product, $_product->getAttributeCode(), 'attribute_code') ?>

Otros consejos

Por favor, intente el código de gráficos

public function __construct()
    {
        parent::__construct();

        $collection = Mage::getResourceModel('catalog/product_collection');
        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addAttributeToSelect('featured')
            ->addStoreFilter()
            ->addAttributeToFilter('featured',true)
            ->setPageSize(16)
            ->setCurPage(1);

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

        $collection->setPageSize(16)
            ->setCurPage(1);
        $this->setProductCollection($collection);
    }

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