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

发布于 2024-07-12 12:03:40 字数 184 浏览 8 评论 0原文

我想获取与当前产品同一类别的随机产品列表,以便在产品视图中显示 - 到目前为止,我挖出的只是

Magento 产品按类别

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

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

Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

演出会有结束 2024-07-19 12:03:40

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

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

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')
 ;
护你周全 2024-07-19 12:03:40

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

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

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

$productCollection = Mage::getResourceModel('catalog/product_collection')
                           ->addCategoryFilter($category);
痞味浪人 2024-07-19 12:03:40

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

执行以下操作:

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

这将为您提供一系列产品 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
}?>

然后,在 cms 中创建包含以下内容的静态块

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

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

那应该可以了。

what I ended up doing is in 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:

<?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

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

呆橘 2024-07-19 12:03:40
$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/>';
       }

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

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

平生欢 2024-07-19 12:03:40

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

如果您像 new Mage_Catalog_Model_Product() 那样执行,这将忽略模块并绕过 Magento API。

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.

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

勿挽旧人 2024-07-19 12:03:40

此代码将帮助您从类别 ID 2 获取产品。 这里还使用模板文件list_home.phtml来显示产品列表。

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

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

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.

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

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---
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文