Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何比较集合中Magento中的两个不同字段_Php_Magento - Fatal编程技术网

Php 如何比较集合中Magento中的两个不同字段

Php 如何比较集合中Magento中的两个不同字段,php,magento,Php,Magento,我试图将产品价格与其在Magento系列中的销售价格进行比较。特别是,试图找到销售价格高于或等于其基准价格的所有产品 从逻辑上讲,我尝试将字段名放入addAttributeToFilter调用中: ->addAttributeToFilter('special_price', array('gteq' => 'special_price')); 但这并不奏效。有没有一个好方法可以做到这一点,不需要我让每一个有销售价格的产品手动检查销售价格是否大于或等于其基准价格 解决方案 这很奇怪

我试图将产品价格与其在Magento系列中的销售价格进行比较。特别是,试图找到销售价格高于或等于其基准价格的所有产品

从逻辑上讲,我尝试将字段名放入
addAttributeToFilter
调用中:

->addAttributeToFilter('special_price', array('gteq' => 'special_price'));
但这并不奏效。有没有一个好方法可以做到这一点,不需要我让每一个有销售价格的产品手动检查销售价格是否大于或等于其基准价格

解决方案

这很奇怪,说明Magento收藏有时是多么奇怪。事实证明,仅仅因为您将
->addAttributeToSelect('price')
放入您的呼叫链中,并不意味着您以一种很好的方式获得了价格。所以我要做的是把它放入一个条件中,这样我就可以在查询的其他部分访问它。我选择了
->addAttributeToFilter('price',array('gteq'=>0))
。对于特价,我也必须这样做

然后,当进入where条件时,我仍然无法使用简单的名称访问price和special price,因此我不得不使用它们的表值,因此我的where语句最终是
$products->getSelect()->where(''u table\u special\u price.value>='u table\u price.value')

最后,这就是我的整个链的样子(其中有一些自定义属性):

$products->getSelect()->其中(“
\u table\u special\u price
value
=
\u table\u price
value
”)

试试这个:

->addAttributeToFilter('special_price', array ('gteq' => new Zend_Db_Expr('price') ) );
如果它不起作用,你可以这样做:

->getSelect()->where("special_price >= price");

必须做类似的事情

除了获取特殊价格>=价格的产品外,它还演示了查询计算字段的策略(以排除价格以.09或.05结尾的产品)

此外,它还演示了如何在一个(大)集合中分页,以最大限度地减少内存使用,等等。这可能对您很有用

$products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('price', array('neq' => '-1')) // Easy way to join price EAV tables to select
        ->addAttributeToFilter('special_price', array('neq' => '-1')); 

$price = 'at_price.value';
$special_price = 'at_special_price.value';

$price_cents = new Zend_Db_Expr("({$price} - FLOOR({$price})) * 100");
$special_price_cents = new Zend_Db_Expr("({$special_price} - FLOOR({$special_price})) * 100");

$allProducts->getSelect()
    ->where("(
        {$price_cents} != 9 OR 
        {$special_price_cents} != 5
    ) AND 
    {$special_price_cents} >= {$price_cents}");

// You can see the query by uncommenting this part    
//echo "\r\n\r\n";
//echo $allProducts->getSelect();
//echo "\r\n\r\n";

$products->setPageSize(100);

$pages = $products->getLastPageNumber();
$currentPage = 1;
do {    
    $products->setCurPage($currentPage);
    $products->load();

    foreach ($products as $_product) {
        echo "Product: ID-" . $_product->getId() . ", price-" . $_product->getPrice() . ", special price-" . $_product->getSpecialPrice() . "\r\n";
        echo "Memory used: " . memory_get_usage() . " / " . memory_get_peak_usage() . "\r\n";

    }

    $currentPage++;
    //make the collection unload the data in memory so it will pick up the next page when load() is called.
    $allProducts->clear();
} while ($currentPage <= $pages);
$products=Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(“*”)
->addAttributeToFilter('状态',1)
->addAttributeToFilter('price',array('neq'=>'-1'))//连接price EAV表以进行选择的简单方法
->addAttributeToFilter('special_price',数组('neq'=>'-1');
$price='at_price.value';
$special_price='at_special_price.value';
$price_cents=new Zend_Db_Expr(“({$price}-FLOOR({$price}))*100”);
$special_price_cents=新Zend_Db_Expr(({$special_price}-地板({$special_price}))*100);
$allProducts->getSelect()
->其中(”(
{$price_cents}!=9或
{$特价{美分}!=5
)及
{$price_cents}>={$price_cents});
//您可以通过取消注释此部分来查看查询
//回显“\r\n\r\n”;
//echo$allProducts->getSelect();
//回显“\r\n\r\n”;
$products->setPageSize(100);
$pages=$products->getLastPageNumber();
$currentPage=1;
做{
$products->setCurPage($currentPage);
$products->load();
foreach($产品作为$产品){
回显“产品:ID-”$\u产品->获取ID()”,价格-“$\u产品->获取价格()”,特价-“$\u产品->获取特价价格()”\r\n;
回显“已使用内存:”.Memory\u get\u usage()。“/”.Memory\u get\u peak\u usage()。“\r\n”;
}
$currentPage++;
//使集合卸载内存中的数据,以便在调用load()时它将拾取下一页。
$allProducts->clear();

}虽然你几乎把我带到了那里。请参阅编辑我的问题,了解我为使这项工作成功所做的一切。
$products = Mage::getModel('catalog/product')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('price', array('neq' => '-1')) // Easy way to join price EAV tables to select
        ->addAttributeToFilter('special_price', array('neq' => '-1')); 

$price = 'at_price.value';
$special_price = 'at_special_price.value';

$price_cents = new Zend_Db_Expr("({$price} - FLOOR({$price})) * 100");
$special_price_cents = new Zend_Db_Expr("({$special_price} - FLOOR({$special_price})) * 100");

$allProducts->getSelect()
    ->where("(
        {$price_cents} != 9 OR 
        {$special_price_cents} != 5
    ) AND 
    {$special_price_cents} >= {$price_cents}");

// You can see the query by uncommenting this part    
//echo "\r\n\r\n";
//echo $allProducts->getSelect();
//echo "\r\n\r\n";

$products->setPageSize(100);

$pages = $products->getLastPageNumber();
$currentPage = 1;
do {    
    $products->setCurPage($currentPage);
    $products->load();

    foreach ($products as $_product) {
        echo "Product: ID-" . $_product->getId() . ", price-" . $_product->getPrice() . ", special price-" . $_product->getSpecialPrice() . "\r\n";
        echo "Memory used: " . memory_get_usage() . " / " . memory_get_peak_usage() . "\r\n";

    }

    $currentPage++;
    //make the collection unload the data in memory so it will pick up the next page when load() is called.
    $allProducts->clear();
} while ($currentPage <= $pages);