4

Magentoコレクションの商品価格と販売価格を比較しようとしています。具体的には、販売価格が基本価格以上のすべての製品を検索しようとします。

addAttributeToFilter論理的には、フィールド名を呼び出しに入れようとしました。

->addAttributeToFilter('special_price', array('gteq' => 'special_price'));

しかし、それはうまくいきませんでした。これを行うための良い方法はありますか?販売価格が基本価格以上であるかどうかを手動で確認するために、販売価格のあるすべての製品を入手する必要はありませんか?

解決

これは非常に奇妙で、Magentoコレクションが奇妙な場合があることを示しています。コールチェーンに入ったからといっ->addAttributeToSelect('price')て、きちんとした方法で価格を取得できるわけではありません。だから私がしなければならなかったことは、クエリの他の部分でそれにアクセスできるように条件付きに入れられました。私はを選びました->addAttributeToFilter('price', array('gteq' => 0))。私は特別価格で同じことをしなければなりませんでした。

それから、where条件を設定することになると、私はまだ簡単な名前の価格と特別価格にアクセスできなかったので、それらのテーブル値を使用する必要があったので、私のwhereステートメントは最終的に$products->getSelect()->where('_table_special_price.value >= _table_price.value');

結局、これは私のチェーン全体がどのように見えたかです(そこにはいくつかのカスタム属性があります):

$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('price')
->addAttributeToSelect('special_price')
->addAttributeToFilter('price', array('gteq' => 0))
->addAttributeToFilter('special_price', array('gteq' => 0))
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('discontinued', array('neq' => 1))
->addAttributeToFilter('status', 1)
;

$ products-> getSelect()-> where(' _table_special_pricevalue>= _table_pricevalue');

4

2 に答える 2

6

これを試して:

->addAttributeToFilter('special_price', array ('gteq' => new Zend_Db_Expr('price') ) );

それが機能しない場合は、これを行うことができます:

->getSelect()->where("special_price >= price");
于 2012-08-27T19:59:54.347 に答える
0

これに似た何かをしなければなりませんでした。

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);
于 2016-08-24T04:56:06.377 に答える