簡體   English   中英

Magento中的可配置產品排名

[英]Configurable products ranking in Magento

我試圖在我的magento網站上生成“最暢銷的產品”列表。 我嘗試按照這里的答案進行操作。 可以,但是,我意識到這僅適用於簡單產品。 我正在嘗試為可配置產品創建排名,因此我認為我需要首先獲取簡單產品的總訂單,但我不確定該如何編程。 這基本上是我想到的想法:

獲取與可配置產品關聯的簡單產品

獲取每個簡單產品的總訂單並匯總

再次將總和傳遞給可配置產品(我在這里考慮使用數組),然后調用按排名排序的必要數據。

到目前為止,我已經提出了以下代碼:

$storeId = Mage::app()->getStore()->getId();
$_collection = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('type_id', 'configurable');
foreach($_collection as $c):
$configurable = $c->getTypeInstance()->getUsedProductIds();
foreach($configurable as $_config):
    $_simple = Mage::getModel('catalog/product')->load($_config);
                echo $_simple->getName()."<br/>";
                echo "qty:".(int)$_simple->ordered_qty."<br/>";
            endforeach;
        endforeach;

但是,在嘗試對其進行測試時,我所有的數量都返回0,所以我被卡住了。 如何實現可配置產品的排名? 請指教。 另外,如果可能的話,我想對核心文件進行盡可能少的更改。

另外,我的排名列表限制為10,如果其他產品的銷量為0,則它​​們將自動按字母順序排序。 例如,如果2種產品的銷售額相同,則結果相同。

經過8個多小時的嘗試來解決這個問題。 我已經提出了一些解決方法(至少似乎可以解決我的問題),代碼肯定需要大量改進,因此我很樂意接受對我的答案的任何修改和建議。 這是我想出的代碼:

$filler = array();

$productCollection = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id', 'configurable')
            ->addOrderedQty()
            ->setOrder('ordered_qty', 'desc')
            ->setPage(1, 10);

foreach($productCollection as $product):
    echo "name:".$product->getName()." - qty:".(int)$product->ordered_qty."<br/>";
    array_push($filler, $product->getId());
endforeach;

if(count($productCollection) < 10):
    $add = 10 - count($productCollection);

    $fillCollection = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id', 'configurable')
            ->setOrder('name', 'desc');

    foreach($fillCollection as $item): 
        if(in_array($item->getId(), $filler)):
        else:
            if($add > 0):
                echo "name:".$item->getName()." - qty: 0 <br/>";
                $add = $add - 1;
            endif;
        endif;
    endforeach;
endif;  
unset($filler);

因此,這的基本思想是,我僅獲得具有訂單的產品的集合,因為它看起來像是不檢索具有0個訂單的產品。 之后,我得到了仍需要在我的排名上顯示10個產品的項目數。 然后,只要顯示的產品少於10個,我就會獲得另一個產品集合並回顯它們。 我使用填充器數組來確保不顯示第一個集合已調用的產品。 有了此代碼,我現在在我的magento網站中對最暢銷產品進行了排名。

檢查此解決方案:

我發現可配置產品的銷售量已正確匯總,但未包含在結果中; 他們的子產品出現了。 我的解決方案是包括可配置的產品,對catalog_product_super_link表進行左連接,並過濾掉任何具有parent_id東西。 這是您需要進行的更改:

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-查找以下兩行...

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

...並將其更改為:

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

我的更改添加了兩個新的可選參數,它們都默認為true ,以免破壞現有功能。

  • $getComplexChildProducts設置為false ,可配置產品的所有子項將從結果中刪除。
  • $getRemovedProducts確定是否還應顯示以前訂購的產品(此后已從Magento中刪除)。

請注意,您的報告統計信息需要最新才能獲得准確的結果。

來源和確認: https : //stackoverflow.com/a/8419579/1136132

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM