如何从magento电子商务中的特定类别获取产品

时间:2021-02-18 01:02:24

I'd like to get a list of random products from the same category as the current product for displaying within the product view - so far all I've dug up is

我想获得与产品视图中显示的当前产品相同类别的随机产品列表 - 到目前为止,我挖出的所有产品都是

Magento products by categories

Magento产品按类别划分

Does anyone know how to do this?

有谁知道如何做到这一点?

6 个解决方案

#1


21  

You basically load up the category, get the Product Collection and then filter appropriately.

您基本上加载类别,获取产品集合,然后适当地过滤。

$products = Mage::getModel('catalog/category')->load($category_id)
 ->getProductCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('status', 1)
 ->addAttributeToFilter('visibility', 4)
 ->addAttributeToFilter('special_price', array('neq' => ""))
 ->setOrder('price', 'ASC')
 ;

#2


18  

Here is the code to get products from any particular category:-

以下是从任何特定类别获取产品的代码: -

$productCollection = Mage::getResourceModel('catalog/product_collection')
                           ->addCategoryFilter($category);

#3


7  

what I ended up doing is in app/design/frontend/default/theme_name/template/catalog/product/list_random.phtml

我最终做的是在app / design / frontend / default / theme_name / template / catalog / product / list_random.phtml

doing something like:

做类似的事情:

<?php 
$_categories=$this->getCurrentChildCategories();

$_category = $this->getCurrentCategory();
$subs = $_category->getAllChildren(true);
$result = array();
foreach($subs as $cat_id) {
    $category = new Mage_Catalog_Model_Category();
    $category->load($cat_id);
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
        $result[] = $product->getId();
    }

}
shuffle($result);
?>

this will get you an array of product id's. You can loop through them and create products on the fly using:

这将为您提供一系列产品ID。您可以使用以下方法循环浏览它们并动态创建产品:

<?php 
$i=0; 
foreach ($result as $_product_id){ 
    $i++;
    $_product = new Mage_Catalog_Model_Product();
    $_product->load($_product_id);
    //do something with the product here
}?>

then, create a static block in the cms with the following content

然后,使用以下内容在cms中创建一个静态块

{{block type="catalog/navigation" template="catalog/product/list_random.phtml"}} 

Finally, in the Catalog->Manage categories section, choose the category, then the display settings tab. Switch the display mode to "Static block and products" and then choose your block from the drop list.

最后,在目录 - >管理类别部分中,选择类别,然后选择显示设置选项卡。将显示模式切换为“静态块和产品”,然后从下拉列表中选择块。

And that should do it.

这应该做到这一点。

#4


3  

$products = Mage::getModel('catalog/category')->load(category_id); //put your category id here
       $productslist = $products->getProductCollection()->addAttributeToSelect('*');
       foreach($productslist as $product)
       {
        echo 'price: ' . $product->getPrice() . '<br/>';
       }

This is the by far the convenient code in order to fetch product details of perticular category.Hope it helps you.

这是迄今为止获取特定类别的产品详细信息的方便代码。希望它可以帮助您。

#5


2  

You should instantiate a model by calling Mage::getModel('catalog/product') in this case because then you get a configured object instance, extended by any configured modules.

在这种情况下,您应该通过调用Mage :: getModel('catalog / product')来实例化模型,因为这样您就会得到一个配置的对象实例,由任何已配置的模块扩展。

If you do it like new Mage_Catalog_Model_Product() this will ignore modules and bypass the Magento API.

如果你像新的Mage_Catalog_Model_Product()那样做,这将忽略模块并绕过Magento API。

#6


0  

This code will helps you to get products from category id 2. And also here uses a template file list_home.phtml for the product listing.

此代码将帮助您从类别ID 2获取产品。此处还使用模板文件list_home.phtml作为产品列表。

 echo $this->getLayout()->createBlock("catalog/product_list")
    ->setCategoryId(2)->setTemplate("catalog/product/list_home.phtml")->toHtml();

list_home.phtml

list_home.phtml

<?php
$this->getChild('toolbar')->setCurrentMode('list'); //uses list mode
$_productCollection = $this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
    ?>

    <?php if (!$_productCollection->count()): ?>
        <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
    <?php else: ?>

--use code for listing---

#1


21  

You basically load up the category, get the Product Collection and then filter appropriately.

您基本上加载类别,获取产品集合,然后适当地过滤。

$products = Mage::getModel('catalog/category')->load($category_id)
 ->getProductCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('status', 1)
 ->addAttributeToFilter('visibility', 4)
 ->addAttributeToFilter('special_price', array('neq' => ""))
 ->setOrder('price', 'ASC')
 ;

#2


18  

Here is the code to get products from any particular category:-

以下是从任何特定类别获取产品的代码: -

$productCollection = Mage::getResourceModel('catalog/product_collection')
                           ->addCategoryFilter($category);

#3


7  

what I ended up doing is in app/design/frontend/default/theme_name/template/catalog/product/list_random.phtml

我最终做的是在app / design / frontend / default / theme_name / template / catalog / product / list_random.phtml

doing something like:

做类似的事情:

<?php 
$_categories=$this->getCurrentChildCategories();

$_category = $this->getCurrentCategory();
$subs = $_category->getAllChildren(true);
$result = array();
foreach($subs as $cat_id) {
    $category = new Mage_Catalog_Model_Category();
    $category->load($cat_id);
    $collection = $category->getProductCollection();
    foreach ($collection as $product) {
        $result[] = $product->getId();
    }

}
shuffle($result);
?>

this will get you an array of product id's. You can loop through them and create products on the fly using:

这将为您提供一系列产品ID。您可以使用以下方法循环浏览它们并动态创建产品:

<?php 
$i=0; 
foreach ($result as $_product_id){ 
    $i++;
    $_product = new Mage_Catalog_Model_Product();
    $_product->load($_product_id);
    //do something with the product here
}?>

then, create a static block in the cms with the following content

然后,使用以下内容在cms中创建一个静态块

{{block type="catalog/navigation" template="catalog/product/list_random.phtml"}} 

Finally, in the Catalog->Manage categories section, choose the category, then the display settings tab. Switch the display mode to "Static block and products" and then choose your block from the drop list.

最后,在目录 - >管理类别部分中,选择类别,然后选择显示设置选项卡。将显示模式切换为“静态块和产品”,然后从下拉列表中选择块。

And that should do it.

这应该做到这一点。

#4


3  

$products = Mage::getModel('catalog/category')->load(category_id); //put your category id here
       $productslist = $products->getProductCollection()->addAttributeToSelect('*');
       foreach($productslist as $product)
       {
        echo 'price: ' . $product->getPrice() . '<br/>';
       }

This is the by far the convenient code in order to fetch product details of perticular category.Hope it helps you.

这是迄今为止获取特定类别的产品详细信息的方便代码。希望它可以帮助您。

#5


2  

You should instantiate a model by calling Mage::getModel('catalog/product') in this case because then you get a configured object instance, extended by any configured modules.

在这种情况下,您应该通过调用Mage :: getModel('catalog / product')来实例化模型,因为这样您就会得到一个配置的对象实例,由任何已配置的模块扩展。

If you do it like new Mage_Catalog_Model_Product() this will ignore modules and bypass the Magento API.

如果你像新的Mage_Catalog_Model_Product()那样做,这将忽略模块并绕过Magento API。

#6


0  

This code will helps you to get products from category id 2. And also here uses a template file list_home.phtml for the product listing.

此代码将帮助您从类别ID 2获取产品。此处还使用模板文件list_home.phtml作为产品列表。

 echo $this->getLayout()->createBlock("catalog/product_list")
    ->setCategoryId(2)->setTemplate("catalog/product/list_home.phtml")->toHtml();

list_home.phtml

list_home.phtml

<?php
$this->getChild('toolbar')->setCurrentMode('list'); //uses list mode
$_productCollection = $this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
    ?>

    <?php if (!$_productCollection->count()): ?>
        <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
    <?php else: ?>

--use code for listing---