Pregunta

Tengo que enumerar los productos más vendidos y su nombre de categoría. Para esto que usé

$_storeId = Mage::app()->getStore()->getId();
$_products = Mage::getResourceModel('reports/product_collection')
        ->addOrderedQty()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('visibility', array(
            Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
            Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH
        ))
        ->setOrder('ordered_qty', 'desc');

Y para obtener un producto individual:

foreach ($_products as $_product) {
//code need to be added here
}

Pero no estoy obteniendo ningún camino para obtener el nombre de la categoría $_product.

He intentado

$_product->getCollection()->getCategoryIds();

Pero mi script muere y no se ejecuta más.

¿Fue útil?

Solución

Esta no es técnicamente una colección de productos de tipo catalog/product, lo que necesitas hacer es:

$_storeId = Mage::app()->getStore()->getId();
$_products = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', array(
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH
    ))
    ->setOrder('ordered_qty', 'desc');
foreach ($_products as $_product){
    $product = Mage::getModel('catalog/product')->load($_product->getId());
    $category_ids = $product->getCategoryIds();
    foreach ($category_ids as $category_id){
        $category = Mage::getModel('catalog/category')->load($category_id);
        <!-- do what you want to with the category here --> 
    }
}

Editar: Según el comentario, para limitar el tamaño de la colección:

$_products = Mage::getResourceModel('reports/product_collection')
    ->addOrderedQty()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', array(
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
        Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH
    ))
    ->setOrder('ordered_qty', 'desc');
$_products->getSelect()->limit(5); //or whatever integer you want
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top