douzhang3822 2013-01-17 22:31
浏览 79
已采纳

Magento MySQL - “OR”条件为两个addAttributeToFilter()

In Magento I need to get products without special price within a specific category.

Here is what I come up with:

<?php
$dateYesterday = date( 'm/d/y', mktime( 0, 0, 0, date( 'm' ), date( 'd' ) - 1, date( 'y' ) ) );

$catagoryModel = Mage::getModel( 'catalog/category' )->load( $currentCategoryId );

$productCollection = Mage::getResourceModel( 'catalog/product_collection' );

$productCollection->addAttributeToSelect( 'name' );

$productCollection->addCategoryFilter( $catagoryModel );

$productCollection->addAttributeToFilter( 'special_price', 
    array( 'eq' => '' ) 
);

$productCollection->addAttributeToFilter( 'special_to_date', 
    array( 'date' => true, 'to' => $dateYesterday ) 
);

?>

In the above query, I need to use the "OR" conditional between the two last filters, semantically like this:

$productCollection->addAttributeToFilter( 'special_price', 
array( 'eq' => '' ) );

// OR

$productCollection->addAttributeToFilter( 'special_to_date', 
array( 'date' => true, 'to' => $dateYesterday ) );

In other words:

( special_price = '' OR special_to_date <= $dateYesterday )

Can you help? Thanks!

  • 写回答

2条回答 默认 最新

  • dongqiang5865 2013-01-17 22:40
    关注

    If I remember well, you need to use an array of conditions to create a OR.
    Check out this link to understand what I mean.
    In your case you should join your two conditions in one, like this

    $productCollection->addAttributeToFilter(
                    array(
                        array('attribute'=>'special_price', 'eq'=>''),
                        array('attribute'=>'special_to_date', 'date' => true, 'to' => $dateYesterday )
                    )
                );
    

    I have no Magento installation to try this.
    Maybe the second condition should be slightly different.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?