【问题标题】:Configurable products ranking in MagentoMagento 中的可配置产品排名
【发布时间】:2013-09-05 07:30:47
【问题描述】:

我正在尝试在我的 magento 网站上生成“畅销产品”列表。我尝试遵循here 的答案。这行得通,但是,我意识到这仅适用于简单产品。我正在尝试为可配置产品创建排名,因此我认为我需要首先获得简单产品的总订单,我不确定如何以编程方式进行。这基本上是我的想法:

获取与可配置产品关联的简单产品

获取每个简单产品的总订单数并求和

再次将总和传递给可配置产品(我正在考虑在这里使用数组)然后调用按排名排序的必要数据。

到目前为止,我已经想出了这个代码:

$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 种产品的销售量相同,情况也是如此。

【问题讨论】:

    标签: php magento


    【解决方案1】:

    经过 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 网站上获得了最畅销产品的排名。

    【讨论】:

      【解决方案2】:

      检查这个解决方案:

      我发现可配置产品销售额的汇总正确,但未包含在结果中;他们的子产品会出现。我的解决方案是包含可配置产品,在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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        • 2011-06-25
        相关资源
        最近更新 更多