我需要在某个类别中按特定产品的价格获得一系列产品。 例如:我有产品白鞋,它位于鞋类别。我需要接下来的五款产品,其价格高于鞋子类别中的 White Shoes ,以及五种价格较低的产品。
感谢您的帮助!
答案 0 :(得分:4)
这可以从效率的角度来清理,但希望你能得到它的要点。您需要设置两个变量,$product
可能是您作为Mage_Catalog_Model_Product
对象的白鞋产品,而$category
是您的鞋类类别为Mage_Catalog_Model_Category
对象。
<强>更新强>
这是一种更好的方法,无需加载$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 :(得分: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);