-1

I need to create a new view template file for products filtered by a custom attribute set as "purpose".

So far so good, I have a template file that list products by visibility:

<?php
$visibility = array(
  Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
  Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);

$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addOrderedQty()
    ->addAttributeToFilter('visibility', $visibility)
    ->setOrder('ordered_qty', 'desc');
?>

How to make this code query for 'purpose'?

Gilberto Albino
  • 2,572
  • 8
  • 38
  • 50
  • Which data type is used for the `purpose` attribute? – Jürgen Thelen Sep 27 '13 at 07:57
  • Purpose is a string. 'weight-loss', 'mass-gain', 'energy', etc – Gilberto Albino Sep 28 '13 at 10:50
  • Hm, just to be sure: did you refresh all indexes after adding the `purpose` attribute to the product? Did you already finish at least one order successfully, after adding the `purpose` attribute? Did this order contain at least one product **not** of type `bundle`, `configurable`, or `grouped` _and_ having a proper visibility? – Jürgen Thelen Sep 28 '13 at 19:51

1 Answers1

1

You can add it the same way you added visibility:

$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addOrderedQty()
    ->addAttributeToFilter('visibility', $visibility)
    ->addAttributeToFilter('purpose', 'weight-loss')
    ->setOrder('ordered_qty', 'desc');
Jason
  • 1,987
  • 1
  • 14
  • 16
  • Thanks, but how could I pass the a purpose like "weight-loss" to the query? – Gilberto Albino Sep 26 '13 at 16:48
  • Check my revised answer, I get it now ;) – Jason Sep 26 '13 at 17:11
  • It must be that the attribute isn't being pulled. Did you mean to use the 'reports/product_collection' resource model? – Jason Sep 26 '13 at 19:31
  • I used reports/product_collection to get the products that have an order quantity 'ordered_qty'. – Gilberto Albino Sep 26 '13 at 19:52
  • Ah. When you export your products to csv, what is the value of products that have 'weight-loss' selected under its 'purpose' column? It might be a number instead. If that doesn't work, you might have to compare using another collection. – Jason Sep 26 '13 at 20:17