I am working on a category summary report for the Magento store (1.6).
To this end, I want to get a collection of order items for a subset of products β that product, a unique category identifier (which creates the Magento product attribute that I created).
I can get the appropriate result set by basing the collection on a catalog / product.
$collection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToFilter('unique_category_id', '75') ->joinTable('sales/order_item', 'product_id=entity_id', array('price'=>'price','qty_ordered' => 'qty_ordered'));
He doesn't like Magento, because there are duplicate entries for the same product ID.
How can I generate code to get this result set based on order items? Merging into a product collection, filtered by attribute, eludes me. This code does not do the trick, since it assumes that the attribute is in the order item and not in the product.
$collection = Mage::getModel('sales/order_item') ->getCollection() ->join('catalog/product', 'entity_id=product_id') ->addAttributeToFilter('unique_category_id', '75');
Any help is appreciated.
source share