2012-05-28 77 views
1

我需要根據某個類別的特定產品的價格獲得下一個產品的集合。 例如:我有產品白色鞋子,它位於鞋子類別。我需要得到接下來的五種產品,即價格比白色鞋更高類,以及五種產品價格較低。Magento下一個按價格獲取產品

感謝您的幫助!

回答

1

我有類似的任務,在這裏我做了什麼:

 $customerGroupId = Mage::helper('customer')->getCustomer()->getGroupId(); 
     $websiteId = Mage::app()->getStore()->getWebsiteId(); 
     $finalPrice = $product->getFinalPrice(); 

     $coreResource = Mage::getSingleton('core/resource'); 
     $adapter = $coreResource->getConnection('catalog_read'); 
     $prevSelect = $adapter->select() 
      ->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array()) 
      ->join(
       array('p' => $coreResource->getTableName('catalog/category_product')), 
       'p.product_id = i.entity_id', 
       array('product_id') 
      ) 
      ->where('p.category_id = ?', $categoryId) 
      ->where('i.customer_group_id = ?', $customerGroupId) 
      ->where('i.website_id = ?', $websiteId) 
      ->where('p.product_id != ?', $product->getId()) 
      ->where('i.final_price < ?', $finalPrice) 
      ->order('i.final_price DESC') 
      ->limit(self::PRODUCTS_BLOCK_SIZE); 

     $lowerIds = array_reverse($adapter->fetchCol($prevSelect)); 

     $nextSelect = $adapter->select() 
      ->from(array('i' => $coreResource->getTableName('catalog/product_index_price')), array()) 
      ->join(
       array('p' => $coreResource->getTableName('catalog/category_product')), 
       'p.product_id = i.entity_id', 
       array('product_id') 
      ) 
      ->where('p.category_id = ?', $categoryId) 
      ->where('i.customer_group_id = ?', $customerGroupId) 
      ->where('i.website_id = ?', $websiteId) 
      ->where('p.product_id != ?', $product->getId()) 
      ->where('i.final_price > ?', $finalPrice) 
      ->order('i.final_price ASC') 
      ->limit(self::PRODUCTS_BLOCK_SIZE); 

     $higherIds = $adapter->fetchCol($nextSelect); 

     $lowerSliced = array_slice($lowerIds, -self::PRODUCTS_BLOCK_SIZE); 
     $requiredFromHigher = self::PRODUCTS_BLOCK_SIZE - count($lowerSliced); 
     $similarIds = array_merge(
      $lowerSliced, 
      array_slice($higherIds, 0, $requiredFromHigher) 
     ); 

     $collection = Mage::getResourceModel('catalog/product_collection') 
      ->addAttributeToSelect('name') 
      ->addAttributeToSelect('small_image') 
      ->addAttributeToSelect('product_url') 
      ->addAttributeToFilter('entity_id', array('in' => $similarIds)) 
      ->setPage(1, self::PRODUCTS_BLOCK_SIZE); 
+0

非常感謝! – Snowcore

+0

什麼使得這比我的解決方案更好?不要試圖小氣,只是想學習! – nachito

+0

最後的解決方案比較好,因爲以下情況:例如,如果沒有5件產品,價格更低(小於5),無論如何,我將獲得一包self :: PRODUCTS_BLOCK_SIZE產品 – Snowcore

4

從效率的角度來看,這大概可以被清理乾淨,但希望你能得到它的要點。有兩個變量,你需要設置,$product這可能是你白鞋產品爲Mage_Catalog_Model_Product對象,$category這是你類別爲Mage_Catalog_Model_Category對象。

UPDATE

這是一個更好的辦法來做到這一點,而無需加載整個產品集合$category

// Load up to 5 products with price <= $product 
$fiveLower = Mage::getModel('catalog/product')->getCollection() 
    // Add whatever attributes you want here 
    ->addAttributeToSelect(array(
     'name', 
     'product_url', 
     'small_image', 
    )) 

    // $category is an instance of Mage_Catalog_Model_Category 
    ->addCategoryFilter($category) 

    // Both of these are required to get the price 
    ->addMinimalPrice() 
    ->addFinalPrice() 

    // Filter out the current product by id 
    ->addAttributeToFilter('entity_id', array('neq' => $product->getId())); 

// Filter by final_price <= $product and limit to 5. 
$fiveLower->getSelect() 
      ->having('final_price <= ?', $product->getFinalPrice()) 
      ->order('final_price') 
      ->limit(5, 0); 

// Load up to 5 products with price > $product 
$fiveHigher = Mage::getModel('catalog/product')->getCollection() 
    ->addAttributeToSelect(array(
     'name', 
     'product_url', 
     'small_image', 
    )) 
    ->addCategoryFilter($category) 
    ->addMinimalPrice() 
    ->addFinalPrice(); 

$fiveHigher->getSelect() 
      ->having('final_price > ?', $product->getFinalPrice()) 
      ->order('final_price') 
      ->limit(5, 0); 

echo 'These are the lower priced items:' . PHP_EOL; 
foreach ($fiveLower as $item) { 
    echo $item->getName() . ' - ' . $item->getFinalPrice() . PHP_EOL; 
} 

echo 'These are the higher priced items:' . PHP_EOL; 
foreach ($fiveHigher as $item) { 
    echo $item->getName() . ' - ' . $item->getFinalPrice() . PHP_EOL; 
} 
+1

謝謝您的回答,但在這裏我們將有性能問題:您的代碼將加載特定類別中的所有產品,但只需要10個產品。 – Snowcore

+0

@Snowcore我已經更新了我的答案,將「SELECT」限制爲5個產品更高,5個產品更低或相等。 – nachito