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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/454462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:47:41  来源:igfitidea点击:

How to get products from a particular category in magento ecommerce

phpmagentoe-commerce

提问by Mukesh Chapagain

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?

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

回答by Josh Pennington

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')
 ;

回答by Mukesh Chapagain

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

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

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

回答by Dan Klassen

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.

那应该这样做。

回答by Chiragit007

$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.

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

回答by emanuel

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.

如果你这样做new Mage_Catalog_Model_Product()会忽略模块并绕过 Magento API。

回答by safin chacko

This code will helps you to get productsfrom category id 2. And also here uses a template file list_home.phtmlfor 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---