【发布时间】:2017-01-04 13:15:49
【问题描述】:
我尝试使用一些 Magento 代码来过滤产品集合。我想查找日期在某个日期之前或尚未设置日期(即为空)的所有产品。
我有:
function getProduct($product_id) {
global $proxy, $sessionId, $conn, $start_date, $time_to_run;
if ($product_id == 'all') {
$result=Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('price_adjust_active', array('null' => true))
->addAttributeToFilter('price_adjust_active', '1')
->addAttributeToFilter(array(
array('attribute'=> 'price_adjust_last_run','lt' => date('Y-m-d H:i:s', $time_to_run)),
array('attribute'=> 'price_adjust_last_run', 'null' => true)
))
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
->setOrder('entity_id', 'DESC')
->setPageSize(1);
} else {
$result=Mage::getModel("catalog/product")
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('eq' => $product_id))
->addAttributeToFilter('price_adjust_active', array('null' => true))
->addAttributeToFilter('price_adjust_active', '1')
->addAttributeToFilter(array(
array('attribute'=> 'price_adjust_last_run','lt' => date('Y-m-d H:i:s', $time_to_run)),
array('attribute'=> 'price_adjust_last_run', 'null' => true)
))
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
->setOrder('entity_id', 'DESC')
->setPageSize(1);
}
并且我可以成功过滤掉日期设置在我指定日期之前的产品。我只是无法让“null”属性起作用。正如您从我的代码中看到的那样,我有 2 个不同的过滤器,它们似乎都没有给我想要的结果。
两个错误的尝试是:
->addAttributeToFilter('price_adjust_active', array('null' => true))
或
array('attribute'=> 'price_adjust_last_run', 'null' => true)
【问题讨论】:
-
可以这样使用 ->addAttributeToFilter('news_from_date', array('or'=> array( 0 => array('date' => true, 'to' => $todayEndOfDayDate) , 1 => 数组('is' => new Zend_Db_Expr('null'))) ), 'left')
-
你可以在这个文件app\code\core\Mage\Catalog\Block\Product\New.php中找到代码
标签: magento