2012-05-28 6 views
1

J'ai besoin d'obtenir une collection de produits suivant par prix pour un produit spécifique dans une catégorie. Par exemple: J'ai le produit Blanc Chaussures, il est situé dans Chaussures catégorie. Je dois obtenir cinq prochains produits, qui a le prix plus élevé que Chaussures blanches en Chaussures catégorie, et cinq produits qui a le prix le plus bas.Magento obtenir des produits suivant par prix

Merci pour votre aide!

Répondre

1

j'avais tâche similaire, et voici ce que je l'ai fait:

 $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

Merci beaucoup! – Snowcore

+0

Qu'est-ce qui rend cela meilleur que ma solution? Ne pas essayer d'être mesquin, juste chercher à apprendre! – nachito

+0

La dernière solution est meilleure, à cause de la situation suivante: par exemple, s'il n'y a pas 5 produits, plus bas par prix (moins de 5), de toute façon, j'obtiendrai un pack de produits auto :: PRODUCTS_BLOCK_SIZE – Snowcore

4

Cela peut probablement être nettoyé du point de vue de l'efficacité, mais j'espère que vous en aurez l'essentiel. Il y a deux variables que vous aurez besoin de définir, $product qui est probablement votre White Shoes de produit comme un objet Mage_Catalog_Model_Product et $category qui est votre Chaussures catégorie comme un objet Mage_Catalog_Model_Category.

MISE À JOUR

Ceci est une meilleure façon de le faire sans charger toute la collection de produits pour $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

Merci pour votre réponse, mais ici nous aurons problème de performance: votre code chargera tous les produits dans la catégorie spécifique, mais seulement 10 produits requis. – Snowcore

+0

@Snowcore J'ai mis à jour ma réponse pour limiter le 'SELECT' à 5 produits plus élevés et 5 produits inférieurs ou égaux. – nachito

Questions connexes