【问题标题】:How to compare two fields in a Magento query?如何比较 Magento 查询中的两个字段?
【发布时间】:2011-11-08 17:16:18
【问题描述】:

我正在使用我在某处找到的此代码获取所有有效的特殊产品:

  $collection = $this->_addProductAttributesAndPrices($collection)
 ->addStoreFilter()
 ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
 ->addAttributeToFilter('special_to_date', array('or'=> array(
    0 => array('date' => true, 'from' => $todayDate),
    1 => array('is' => new Zend_Db_Expr('null')))
 ), 'left')
 ->setPageSize($this->get_prod_count())
 ->setCurPage($this->get_cur_page());

现在我只想获得特价

我一直在阅读这个页面:http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/using_collections_in_magento

我试过这个没有成功:

     ->addAttributeToFilter('special_price', array('lt' => 'price'))

感谢您的帮助!

【问题讨论】:

  • 第二个参数“价格”被视为字符串
  • 没错,这就是我问的原因,我不知道比较这些值的正确方法

标签: magento magento-1.5


【解决方案1】:

你可以试试this,谢谢Zyava!

就我而言:

 ->addAttributeToFilter('price', ['gt' => new Zend_Db_Expr('final_price')])

【讨论】:

    【解决方案2】:

    一个效率较低的解决方案是遍历集合,

    $products = array();
    $collection = Mage::getResourceModel('catalog/product_collection');
    foreach($collection as $col) {
        $product = Mage::getModel('catalog/product')->load($col->getId());
        if( $product->getPrice() > $product->getSpecialPrice() ) {
            $products[] = $product;
        }
     }
    

    你最终会有一系列产品......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 2011-04-15
      • 2021-09-01
      • 2021-01-02
      相关资源
      最近更新 更多