【问题标题】:Magento Price filter "from x to unlimited"Magento 价格过滤器“从 x 到无限”
【发布时间】:2011-10-27 10:03:49
【问题描述】:

我知道 Magento 如何按范围过滤价格。

但是,我如何过滤价格为 X 或更高的产品?我不想要价格的上限。

如果您需要进一步的解释,请询问。

一个更清楚的小例子。 我希望能够添加一个价格过滤器,例如支持从 200 美元到 800 000 美元

【问题讨论】:

  • 您希望它像常规的分层导航过滤器一样工作吗?因此,在左列的过滤器块中(通常)
  • 您是尝试以编程方式还是通过网站上的某些界面来执行此操作?
  • 我知道通过现有接口是不可能的。所以我寻找一种编程方式。是的,Jonathan Day,它应该像常规的分层导航过滤器一样工作

标签: magento


【解决方案1】:

丑陋的答案:为了对价格字段应用限制,您需要恢复到(几乎)普通的旧 SQL。
首先是一点背景。

产品集合使用价格索引表 catalog_product_index_price 中预先计算的值。

当在集合上调用 addPriceData() 时,使用表别名 price_index 加入索引表。

假设我们有一个产品集合和一个包含价格下限的变量,初始化如下:

$lowerPriceLimit = 200;

/** @var $products Mage_Catalog_Model_Resource_Product_Collection */
$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('name');

您可能希望针对final_price 字段进行测试,因为这是购买产品时将使用的值(与pricespecial_price 或其他相比)。

这是一个如何添加条件的示例:

// Join the price_index table
$products->addPriceData();

// Apply price limit
$products->getSelect()->where('price_index.final_price >= ?', $lowerPriceLimit);

这是另一种选择。如果你想多用一点Magento 方式(即使用更复杂的方法),请使用:

$customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$websiteId = Mage::app()->getWebsite()->getId();

$products->joinField(
    'filter_price',                // field alias
    'catalog/product_index_price', // table
    'final_price',                 // real field name
    'entity_id=entity_id',         // primary condition
    array(                         // additional conditions
        'website_id' => $websiteId,
        'customer_group_id' => $customerGroupId,
        'final_price' => array('gteq' => $lowerPriceLimit)
    )
);

如果您还以第二种方式使用addPriceData(),您最终会在价格指数上使用双内连接,但它仍然可以工作...

都是相当低级的,但从好的方面来说,至少这仍然是相当符合标准的 SQL,所以它也应该是相当向上兼容的。

也许您可以将此与 Sylvain 的答案结合起来,使其成为分层导航价格范围过滤器的一部分。

【讨论】:

    【解决方案2】:

    下面是一个示例,说明如何创建自己的过滤器并提供一些说明。我们使用它来为我们的免费产品提供过滤器,您需要适应您的情况。 你需要覆盖类Mage_Catalog_Model_Layer_Filter_Price

    在下面的代码中,$index 定义价格范围过滤器中的索引。如果您有 4 个价格范围,则索引 1 是第一个,索引 4 是最后一个。 $range 是价格范围,如果你有一个范围 = 100 和一个指数 = 1,价格范围将在 1 到 100 之间。如果你有指数 = 4 和范围 = 1000,你会得到产品价格范围从3000 美元到 4000 美元(以美元为例)。

    代码可能会有所改进,例如可以在方法_getItemsData()$data = parent::_getItemsData() 中做,然后使用$data 进行更改

    class MyCompany_Catalog_Model_Layer_Filter_Price extends Mage_Catalog_Model_Layer_Filter_Price {
        protected function _getItemsData()
        {
        $range      = $this->getPriceRange();
        $dbRanges   = $this->getRangeItemCounts($range);
    
        // Display an item Free Products to allow to select only them in the layer navigation block
        if($count = $this->_getResource()->getCountFreeProducts($this)){
            $data[] = array(
                'label' => Mage::helper('catalog')->__('Free Products'),
                'value' => '1,1',
                'count' => $this->_getResource()->getCountFreeProducts($this) );    
        }else{
            $data = array();
        }
    
        foreach ($dbRanges as $index=>$count) {
            $data[] = array(
                'label' => $this->_renderItemLabel($range, $index),
                'value' => $index . ',' . $range,
                'count' => $count,
            );
        }
    
        return $data;
        }
    }
    

    【讨论】:

    • 这是添加过滤器的一个很好的解释,但我在这里看不到,我如何添加一个从 1009 开始到例如 500 000 结束的过滤器。
    【解决方案3】:

    如果你想要这样

    50 美元以下

    $50 - $99(5)

    $100 - $199(3)

    $200 - $299(10)

    $300 - $499(2)

    $500 - $699(17)

    $700 - $899(1)

    $900 或以上(12)

    您可以从http://www.codegyan.com/2012/01/06/customize-filter-price-range-in-magento/获得帮助

    【讨论】:

      【解决方案4】:

      我将以下代码用于我的 filter.phtml。希望对您有所帮助。

      <?php 
          $getLabel = $_item->getLabel();
          if (strpos($getLabel, 'price')!== false) :?>
              <a class="multi-select unselected" href="<?php echo $this->urlEscape($_item->getUrl()) ?>">
      
                  <?php 
                  $getValue = $_item->getValue();
                      $fitlerPrices = str_replace('-', ' - ', $getValue);
      
      
                      $file = basename($fitlerPrices); 
      
                      $parts = explode("-", $file); 
                      $getCurency = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
                      $priceBefore = $getCurency . number_format($parts[0], 0);
                      $priceAfter = $getCurency . number_format($parts[1], 0); ?>
                      <?php
                          if($i == $count){
                              //Last Item
                              echo '<span class="price">' . $priceBefore . ' and Above</span>';
                          }elseif($i <= 1){
                              //First Item 
                              echo '<span class="price">Under ' . $priceAfter . '</span>';
                          }else{
                               echo '<span class="price">' . $priceBefore . ' - ' .$priceAfter . '</span>';
                          }
                      ?>
      
                  </a>
          <?php else :?>
      
              <a class="multi-select unselected" href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel(); ?></a>
          <?php endif?>
      

      【讨论】:

      • 不鼓励仅使用代码回答,因为它们没有为未来的读者提供太多信息,请对您所写的内容提供一些解释
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 2011-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 2012-03-27
      相关资源
      最近更新 更多