Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Created December 3, 2012 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save peterjaap/4194635 to your computer and use it in GitHub Desktop.
Save peterjaap/4194635 to your computer and use it in GitHub Desktop.
Sold products % by category
<?php
require_once("app/Mage.php");
Mage::app();
$orders = Mage::getModel('sales/order')->getCollection();
$i = 0;
foreach ($orders as $key => $order) {
if (!$i) {
$startDate = strtotime($order->getCreatedAt());
}
foreach ($order->getAllItems() as $itemId => $item) {
$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$catId = array_shift($_product->getCategoryIds());
$categoryItemIds[$catId][] = $item->getProductId();
}
$i++;
}
foreach($categoryItemIds as $catId=>$itemIds) {
$itemIds = array_unique($itemIds);
$cat = Mage::getModel('catalog/category')->load($catId);
$products = Mage::getResourceModel('reports/product_collection')->addAttributeToFilter('status', array('eq' => 1))->addCategoryFilter($cat)->addAttributeToSelect(array('entity_id'))->load();
echo "\nCategory ".$cat->getName().": we have sold " . count($itemIds) . " of the " . count($products) . " (" . round((count($itemIds) / count($products) * 100), 2) . "%) visible products in this store in the past " . floor((date("U") - $startDate) / 86400) . " days, spread out over " . count($orders) . " orders\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment