Magento BestSeller模块-对可配置产品求和并重新添加它们 [英] Magento BestSeller Module - Summing Configurable Products And Adding Them Back In

查看:95
本文介绍了Magento BestSeller模块-对可配置产品求和并重新添加它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经困扰了我好一阵子了.基本上,我们要实现的目标是在我们的首页上的畅销书中,以列出的售出数量列出产品.对于简单产品来说,这很好用,但是对于可配置产品,它们将以订购数量0显示.

This has been bugging me for quite a while. Basically, what we are trying to achieve is in the bestsellers on our front page, to have the products listed in the amount sold. For simple products this works fine, however for configurable products they will be displayed as a quantity ordered of 0.

我不知何故需要找到一种方法来获取可配置产品,找到与之相连的简单产品,将这些简单产品的销售总额相加,将其添加回可配置产品ID中,并将此信息反馈回去列出已售出正确数量的可配置产品.

I somehow need to find a way to get the configurable products, find the simple products attached to them, sum the amount sold of these simple products, add this back to the configurable products ID and feed this information back in so it will list the configurable product with the right amount that has been sold.

我相信,我已经放置了需要更改的代码区域.如果有人可以帮助,将不胜感激!

I have placed, what I believe, the areas of code that require changing. If anyone could help it would be very much appreciated!

Collection.php

Collection.php

class Luxe_Bestsellers_Model_Mysql4_Product_Collection extends Mage_Reports_Model_Mysql4_Product_Collection
{
public function addOrderedQty($from = '', $to = '', $getComplexProducts=false)
{
    $qtyOrderedTableName = $this->getTable('sales/order_item');
    $qtyOrderedFieldName = 'qty_ordered';

    $productIdFieldName = 'product_id';

    if (!$getComplexProducts) {
        $compositeTypeIds = Mage::getSingleton('catalog/product_type')->getCompositeTypes();
        $productTypes = $this->getConnection()->quoteInto(' AND (e.type_id NOT IN (?))', $compositeTypeIds);
    } else {
        $productTypes = '';
    }

    if ($from != '' && $to != '') {
        $dateFilter = " AND `order`.created_at BETWEEN '{$from}' AND '{$to}'";
    } else {
        $dateFilter = "";
    }

    $this->getSelect()->reset()->from(
        array('order_items' => $qtyOrderedTableName),
        array('ordered_qty' => "SUM(order_items.{$qtyOrderedFieldName})")
    );

     $_joinCondition = $this->getConnection()->quoteInto(
            'order.entity_id = order_items.order_id AND order.state<>?', Mage_Sales_Model_Order::STATE_CANCELED
     );
     $_joinCondition .= $dateFilter;
     $this->getSelect()->joinInner(
        array('order' => $this->getTable('sales/order')),
        $_joinCondition,
        array()
     );


    $this->getSelect()
        ->joinInner(array('e' => $this->getProductEntityTableName()),
            "e.entity_id = order_items.{$productIdFieldName} AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}")
        ->group('e.entity_id')
        ->having('ordered_qty > 0');

    return $this;
}

}

List.php

class Luxe_Bestsellers_Block_List extends Mage_Catalog_Block_Product_List
{
protected $_defaultToolbarBlock = 'bestsellers/list_toolbar';

protected function _beforeToHtml() {
    $this->addPriceBlockType('bundle', 'bundle/catalog_product_price', 'bundle/catalog/product/price.phtml');
    return parent::_beforeToHtml();    
}

public function _toHtml()
{
    if ($this->_productCollection->count()) {
        return parent::_toHtml();
    } else {
        return '';
    }
}

public function getTimeLimit()
{
    if ($this->getData('time_limit_in_days')) {
        return intval($this->getData('time_limit_in_days'));
    } else {
        return intval(Mage::getStoreConfig('bestsellers/bestsellers/time_limit_in_days'));
    }
}

public function getBlockTitle()
{
    if ($this->getData('title')) {
        return $this->getData('title');
    } else {
        return Mage::getStoreConfig('bestsellers/bestsellers/title');
    }
}

public function isShowOutOfStock() {
    return (bool)Mage::getStoreConfig('bestsellers/bestsellers/show_out_of_stock');
}

public function getProductsLimit()
{
    if ($this->getData('limit')) {
        return intval($this->getData('limit'));
    } else {
        return $this->getToolbarBlock()->getLimit();
    }
}

public function getDisplayMode()
{
    return $this->getData('display_mode');
}

/**
 * Retrieve loaded category collection
 *
 * @return Mage_Eav_Model_Entity_Collection_Abstract
 */
protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $layer = Mage::getModel('catalog/layer');
        $bestsellers = Mage::getResourceModel('reports/product_collection');
        if ($this->getTimeLimit()) {
            $product = Mage::getModel('catalog/product');
            $todayDate = $product->getResource()->formatDate(time());
            $startDate = $product->getResource()->formatDate(time() - 60 * 60 * 24 * $this->getTimeLimit());
            $bestsellers->addOrderedQty($startDate, $todayDate, true);
        } else {
            $bestsellers->addOrderedQty('', '', true);
        }

        $bestsellers->addStoreFilter()
                ->setOrder('ordered_qty', 'desc')
                ->setPageSize($this->getProductsLimit());

        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($bestsellers);

        if ($layer->getCurrentCategory()->getId() != Mage::app()->getStore()->getRootCategoryId()) {
            $bestsellers->addCategoryFilter($layer->getCurrentCategory());
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($bestsellers);
        }

        if (!$this->isShowOutOfStock()) {
            Mage::getModel('cataloginventory/stock')->addInStockFilterToCollection($bestsellers);
        }

        $bestsellers->getSelect()->where('order.store_id = ?', Mage::app()->getStore()->getId());

        $productIds = array();
        foreach ($bestsellers as $p) {
            $productIds[] = $p->getId();
        }
        $collection = Mage::getResourceModel('catalog/product_collection');

        Mage::getModel('catalog/layer')->prepareProductCollection($collection);

        $attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
        $collection->addIdFilter($productIds)
                   ->addAttributeToSelect($attributes)
                   ->addMinimalPrice()
                   ->addFinalPrice();
        $this->_productCollection = $collection;
    }
    return $this->_productCollection;
}

/**
 * Translate block sentence
 *
 * @return string
 */
public function __()
{
    $args = func_get_args();
    $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), 'Mage_Catalog');
    array_unshift($args, $expr);
    return Mage::app()->getTranslator()->translate($args);
}
}

推荐答案

感谢您发布该示例代码!我能够使用它来创建一个对我们俩都适用的解决方案.

Thanks for posting that sample code! I was able to use it to create a solution which should work well for both of us.

我发现可配置产品的销售额已正确汇总,但未包含在结果中;他们的子产品出现了.我的解决方案是包括可配置的产品,在catalog_product_super_link表上进行左联接,并过滤掉具有parent_id的所有内容.这是您需要进行的更改:

I found that configurable product sales are being summed correctly but aren't being included in the results; their child products appear instead. My solution was to include configurable products, do a left join on the catalog_product_super_link table, and filter out anything that has a parent_id. Here are the changes you'll need to make:

Collection.php:

Collection.php:

    public function addOrderedQty($from = '', $to = '', $getComplexProducts=false, $getComplexChildProducts = true, $getRemovedProducts = true)
    {
        $qtyOrderedTableName = $this->getTable('sales/order_item');
        $qtyOrderedFieldName = 'qty_ordered';

        $productIdFieldName = 'product_id';

        if (!$getComplexProducts) {
            $compositeTypeIds = Mage::getSingleton('catalog/product_type')->getCompositeTypes();
            $productTypes = $this->getConnection()->quoteInto(' AND (e.type_id NOT IN (?))', $compositeTypeIds);
        } else {
            $productTypes = '';
        }

        if ($from != '' && $to != '') {
            $dateFilter = " AND `order`.created_at BETWEEN '{$from}' AND '{$to}'";
        } else {
            $dateFilter = "";
        }

        $this->getSelect()->reset()->from(
            array('order_items' => $qtyOrderedTableName),
            array(
                'ordered_qty' => "SUM(order_items.{$qtyOrderedFieldName})",
                'order_items_name' => 'order_items.name'
            )
        );

         $_joinCondition = $this->getConnection()->quoteInto(
                'order.entity_id = order_items.order_id AND order.state<>?', Mage_Sales_Model_Order::STATE_CANCELED
         );
         $_joinCondition .= $dateFilter;
         $this->getSelect()->joinInner(
            array('order' => $this->getTable('sales/order')),
            $_joinCondition,
            array()
         );

         // Add join to get the parent id for configurables
         $this->getSelect()->joinLeft(
             array('cpsl' => $this->getTable('catalog/product_super_link')),
             'cpsl.product_id = order_items.product_id',
             'cpsl.parent_id'
         );

        if(!$getComplexChildProducts)
            $this->getSelect()->having('parent_id IS NULL');

        if($getRemovedProducts)
        {
             $this->getSelect()
                ->joinLeft(array('e' => $this->getProductEntityTableName()),
                    "e.entity_id = order_items.{$productIdFieldName} AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}")
                ->group('order_items.product_id');
        }
        else
        {
            $this->getSelect()
                ->joinInner(array('e' => $this->getProductEntityTableName()),
                    "e.entity_id = order_items.{$productIdFieldName} AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}")
                ->group('e.entity_id');
        }


        $this->getSelect()->having('ordered_qty > 0');

        // This line is for debug purposes, in case you'd like to see what the SQL looks like
        // $x = $this->getSelect()->__toString();

        return $this;
    }

List.php-找到以下两行...

List.php - Find the following two lines...

$bestsellers->addOrderedQty($startDate, $todayDate, true);
$bestsellers->addOrderedQty('', '', true);

...并将其更改为:

... and change them to:

$bestsellers->addOrderedQty($startDate, $todayDate, true, false, false);
$bestsellers->addOrderedQty('', '', true, false, false);

我的更改添加了两个新的可选参数,默认情况下均为true,以免破坏现有功能.

My changes added two new optional parameters, which both default to true, as to not break existing functionality.

  • $getComplexChildProducts设置为false时,将从结果中删除可配置产品的所有子项.
  • $getRemovedProducts确定是否还应显示以前订购的产品(此后已从Magento中删除).
  • When $getComplexChildProducts is set to false, all child items of the configurable product will be removed from the results.
  • $getRemovedProducts determines whether or not previously ordered products (which have since been deleted from Magento) should also appear.

请注意,您的报告统计信息需要最新才能获得准确的结果.

Please note that your report statistics will need to be up-to-date in order to get accurate results.

希望这会有所帮助!如果您有任何问题,请告诉我.

Hope this helps! Let me know if you have any questions.

这篇关于Magento BestSeller模块-对可配置产品求和并重新添加它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆