我怎样才能获得magento的特定产品?

时间:2011-10-12 09:51:57

标签: magento

我想获得最新的3个项目,并且应该跳过前两个产品。我在解释这个假设我有一个数组a [] =(0,1,2,3,4,5)。 现在我想得到一个只有三个项目(2,3,4)的数组。 我怎么能在magento做到这一点。

3 个答案:

答案 0 :(得分:1)

这将是在Magento中实现这一目标的方法。

$collection = Mage::getModel('catalog/product')->getCollection()
->setOrder('entity_id', 'DESC') //Order by product id(most recient)
->getSelect()
    ->limit(10,2); //This will return up to 10 products skiping the first 2.


//Now do what you want with it.
foreach($collection as $product){
    echo $product->getId();
}

答案 1 :(得分:0)

//Get the product IDs with the first two elements in the original array removed and
//only the next 3 after that
$product_ids = array_slice($a, 2, 3); //$a is the array you mentioned in your post

//create an array to store the products
$products = array();

//loop through the product IDs and load the actual Product model
foreach($product_ids as $pid) {
    $products[] = Mage::getModel('catalog/product')->load($pid);
}

在您的示例之后,$products将包含ID为2,3和4的产品。

答案 2 :(得分:-1)

有不同的方法可以做到这一点。 我给你两个解决方案。我希望我理解你的问题。

<?php
...
$collection = Mage::getModel('catalog/product')->getCollection();
$items = $collection->getItems();// You get all activated products of your catalog
$newItems = array_splice($items, 2); // You remove the first two elements of the array
....
?>

第二种解决方案是过滤您的收藏品,只获得有限数量的选定产品,因为您没有加载所有产品,只加载您需要的产品。

<?php
...
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()
    ->where ('entity_id IN (?)', array(0,1,2,3,4,5))
    ->limit(5,2); // You get 5 products up to the 2 result (offset). You can remove this line if you slice your array before to give it in the where clause.

$items = $collection->getItems();
....
?>