Php 在Magento 1.7中显示导航中每个类别的畅销书
我正在寻找一种可能性,以获得最畅销的产品类别,以显示在一个特定的导航部分。展示产品不是问题,而是获取产品 我已经用不同的关键词在谷歌上进行了密集搜索,但我得到的只是过时的插件、对bestseller.phtml的修改(它不再存在于Magento 1.7中)和在资源模型上设置过滤器,但我没有找到任何结果 所以我试着自己购买产品(到目前为止,它应该只获得任何产品的销量,而不是最好的):Php 在Magento 1.7中显示导航中每个类别的畅销书,php,navigation,magento-1.7,Php,Navigation,Magento 1.7,我正在寻找一种可能性,以获得最畅销的产品类别,以显示在一个特定的导航部分。展示产品不是问题,而是获取产品 我已经用不同的关键词在谷歌上进行了密集搜索,但我得到的只是过时的插件、对bestseller.phtml的修改(它不再存在于Magento 1.7中)和在资源模型上设置过滤器,但我没有找到任何结果 所以我试着自己购买产品(到目前为止,它应该只获得任何产品的销量,而不是最好的): $category->getId(); $children=$category->getChildren(); f
$category->getId();
$children=$category->getChildren();
foreach($childrenas$child)
{
$childCategoryIdString=$child->getId();
$childCategoryId=substr($childCategoryIdString,14);
$childCategory=Mage::getModel('catalog/category')
->加载($childCategoryId);
$productCollection=Mage::getModel('目录/产品')
->getCollection()
->addCategoryFilter($childCategory)
->加载();
$allIds=$productCollection->getAllIds();
对于($i=0;$iload($allIds[$i]);
echo$product->getOrderedQty().'uUZ';
}
}
这有两个问题:首先,它使Magento更慢。第二个
$product->getOrderedQty()
,我在各种搜索结果中找到的方法,不起作用。现在我真的不知道还有什么我可以尝试和寻找一些帮助,这是高度赞赏。谢谢 在示例脚本中使用了大量对象包装器。封装到多个循环中的load
等方法将产生巨大的延迟,并且可能会占用大量内存(取决于产品集合的大小)
前几天,当我解决这个问题时,我决定使用直接的ORM方法而不是对象来获得更好的性能
展示畅销书有两种可能的方式。使用聚合畅销书表(如sales\u bestsellers\u aggregated\u daily
)消耗的资源更少,但它有一个巨大的缺点——这些表中的数据不是自动更新的。它用于管理报告部分,只有当您选择刷新统计信息时才会更新
另一种但更可靠的方法是连接sales\u flat\u order\u item
表以检索每个产品的sales\u数量。很明显,这会消耗更多的资源,因为你必须自己计算
在我的脚本中,我选择了后一条路径。我修改了它以满足您的逻辑要求。此外,我还添加了两个联接
来获取类别名称,您可能不需要这些。但是说够了:)下面是我的test.php
shell脚本的代码:
<?php
require_once 'abstract.php';
/**
* Magento Test Bestsellers script
*
* @category Mage
* @package Mage_Shell
*/
class Mage_Shell_Test extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
// benchmarking
$memory = memory_get_usage();
$time = microtime();
echo "Starting mem usage: $memory\n";
$catId = $this->getArg('category');
/** @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getResourceModel('catalog/product_collection');
// join sales order items column and count sold products
$expression = new Zend_Db_Expr("SUM(oi.qty_ordered)");
$condition = new Zend_Db_Expr("e.entity_id = oi.product_id AND oi.parent_item_id IS NULL");
$collection->addAttributeToSelect('name')->getSelect()
->join(array('oi' => $collection->getTable('sales/order_item')),
$condition,
array('sales_count' => $expression))
->group('e.entity_id')
->order('sales_count' . ' ' . 'desc');
// join category
$condition = new Zend_Db_Expr("e.entity_id = ccp.product_id");
$condition2 = new Zend_Db_Expr("c.entity_id = ccp.category_id");
$collection->getSelect()->join(array('ccp' => $collection->getTable('catalog/category_product')),
$condition,
array())->join(array('c' => $collection->getTable('catalog/category')),
$condition2,
array('cat_id' => 'c.entity_id'));
$condition = new Zend_Db_Expr("c.entity_id = cv.entity_id AND ea.attribute_id = cv.attribute_id");
// cutting corners here by hardcoding 3 as Category Entiry_type_id
$condition2 = new Zend_Db_Expr("ea.entity_type_id = 3 AND ea.attribute_code = 'name'");
$collection->getSelect()->join(array('ea' => $collection->getTable('eav/attribute')),
$condition2,
array())->join(array('cv' => $collection->getTable('catalog/category') . '_varchar'),
$condition,
array('cat_name' => 'cv.value'));
// if Category filter is on
if ($catId) {
$collection->getSelect()->where('c.entity_id = ?', $catId)->limit(1);
}
// unfortunately I cound not come up with the sql query that could grab only 1 bestseller for each category
// so all sorting work lays on php
$result = array();
foreach ($collection as $product) {
/** @var $product Mage_Catalog_Model_Product */
if (isset($result[$product->getCatId()])) {
continue;
}
$result[$product->getCatId()] = 'Category:' . $product->getCatName() . '; Product:' . $product->getName() . '; Sold Times:'. $product->getSalesCount();
}
print_r($result);
// benchmarking
$memory2 = memory_get_usage();
$time2 = microtime();
$memDiff = ($memory2 - $memory)/1000000;
$timeDiff = $time2 - $time;
echo 'Time spent:' . $timeDiff . "s\n";
echo "Ending mem usage: $memory2\n";
echo "Mem used : {$memDiff}M\n";
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f test.php -- [options]
php -f test.php -- --category 1
--categories <category> Filter by Category, if not specified, all categories are outputted
help This help
USAGE;
}
}
$shell = new Mage_Shell_Test();
$shell->run();
$visibility=array(
图像\目录\模型\产品\可见性::可见性\两者,
图像目录模型产品可见性::目录中的可见性
);
$category=新的Mage_Catalog_Model_category();
$category->load(2)//我的猫id是10
$prodCollection=$category->getProductCollection()->addAttributeToFilter('visibility',$visibility)->setOrder('ordered_qty','desc');
//随便你
希望这有帮助谢谢您的回复!不幸的是,这个代码段做了一些奇怪的事情,因为它输出了相同的两个产品的四倍(这表示至少echo$\u product->getId()),而我加载的类别有六个项。因此,我希望他们或没有人(因为到目前为止还没有销售任何产品)。很抱歉我的回复太晚,但感谢您提供的信息丰富的答案!运行脚本将返回四个空数组。因为到目前为止还没有售出的产品,所以它们完全是空的(好吧,不应该至少是其中的文本字符串吗?)。我有四个父类别,但出于测试目的将$catId定义为“17”(我的子类别ID之一)。我监督过什么吗?只需注意一下,这些畅销书在万磁王的cron调度系统中被cron作业填充
<?php
require_once 'abstract.php';
/**
* Magento Test Bestsellers script
*
* @category Mage
* @package Mage_Shell
*/
class Mage_Shell_Test extends Mage_Shell_Abstract
{
/**
* Run script
*
*/
public function run()
{
// benchmarking
$memory = memory_get_usage();
$time = microtime();
echo "Starting mem usage: $memory\n";
$catId = $this->getArg('category');
/** @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$collection = Mage::getResourceModel('catalog/product_collection');
// join sales order items column and count sold products
$expression = new Zend_Db_Expr("SUM(oi.qty_ordered)");
$condition = new Zend_Db_Expr("e.entity_id = oi.product_id AND oi.parent_item_id IS NULL");
$collection->addAttributeToSelect('name')->getSelect()
->join(array('oi' => $collection->getTable('sales/order_item')),
$condition,
array('sales_count' => $expression))
->group('e.entity_id')
->order('sales_count' . ' ' . 'desc');
// join category
$condition = new Zend_Db_Expr("e.entity_id = ccp.product_id");
$condition2 = new Zend_Db_Expr("c.entity_id = ccp.category_id");
$collection->getSelect()->join(array('ccp' => $collection->getTable('catalog/category_product')),
$condition,
array())->join(array('c' => $collection->getTable('catalog/category')),
$condition2,
array('cat_id' => 'c.entity_id'));
$condition = new Zend_Db_Expr("c.entity_id = cv.entity_id AND ea.attribute_id = cv.attribute_id");
// cutting corners here by hardcoding 3 as Category Entiry_type_id
$condition2 = new Zend_Db_Expr("ea.entity_type_id = 3 AND ea.attribute_code = 'name'");
$collection->getSelect()->join(array('ea' => $collection->getTable('eav/attribute')),
$condition2,
array())->join(array('cv' => $collection->getTable('catalog/category') . '_varchar'),
$condition,
array('cat_name' => 'cv.value'));
// if Category filter is on
if ($catId) {
$collection->getSelect()->where('c.entity_id = ?', $catId)->limit(1);
}
// unfortunately I cound not come up with the sql query that could grab only 1 bestseller for each category
// so all sorting work lays on php
$result = array();
foreach ($collection as $product) {
/** @var $product Mage_Catalog_Model_Product */
if (isset($result[$product->getCatId()])) {
continue;
}
$result[$product->getCatId()] = 'Category:' . $product->getCatName() . '; Product:' . $product->getName() . '; Sold Times:'. $product->getSalesCount();
}
print_r($result);
// benchmarking
$memory2 = memory_get_usage();
$time2 = microtime();
$memDiff = ($memory2 - $memory)/1000000;
$timeDiff = $time2 - $time;
echo 'Time spent:' . $timeDiff . "s\n";
echo "Ending mem usage: $memory2\n";
echo "Mem used : {$memDiff}M\n";
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f test.php -- [options]
php -f test.php -- --category 1
--categories <category> Filter by Category, if not specified, all categories are outputted
help This help
USAGE;
}
}
$shell = new Mage_Shell_Test();
$shell->run();
$catId = $this->getArg('category');
/** @var $resource Mage_Core_Model_Resource */
$resource = Mage::getModel('core/resource');
/** @var $adapter Zend_Db_Adapter_Abstract */
$adapter = $resource->getConnection('core_read');
$select = $adapter->select()
->from(array('c' => $resource->getTableName('catalog/category')), array('cat_id'=>'entity_id'))
->join(array('ccp' => $resource->getTableName('catalog/category_product')), 'c.entity_id = ccp.category_id', array())
->join(array('oi' => $resource->getTableName('sales/order_item')), 'ccp.product_id = oi.product_id', array('max_qty' => new Zend_Db_Expr('SUM(oi.qty_ordered - oi.qty_canceled)'), 'product_id' => 'product_id'))
->where('oi.parent_item_id is null')
->group('c.entity_id')
->group('oi.product_id')
->order('entity_id ASC')
->order('max_qty DESC');
if ($catId) {
$select->where('c.entity_id = ?', $catId);
}
$res = $adapter->fetchAll($select);
$result = array();
foreach ($res as $oneRes) {
if (isset($result[$oneRes['cat_id']])) {
continue;
}
$result[$oneRes['cat_id']] = $oneRes;
}
array_walk($result, function($var, $key) {
echo 'Category Id:' . $key . ' | Product Id:' . $var['product_id'] . ' | Sales Count:' . $var['max_qty'] . "\n";
});
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);
$category = new Mage_Catalog_Model_Category();
$category->load(2); //My cat id is 10
$prodCollection = $category->getProductCollection()->addAttributeToFilter('visibility', $visibility)->setOrder('ordered_qty', 'desc');
<?php foreach($_productCollection as $_product): ?>
//whatever you want
<?php endforeach; ?>