-
-
Save Anxiro/aac4aa840015aff699c1068ac7105a23 to your computer and use it in GitHub Desktop.
Sold products % by category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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