php Magento 产品列表按类别 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6748730/
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-26 01:08:32  来源:igfitidea点击:

Magento Product Listing by Category ID

phpmagentoprogramming-languagesmagento-1.5

提问by Cameron Owen

I have a magento (1.5) store and a wordpress (3.2) blog.

我有一个 magento (1.5) 商店和一个 wordpress (3.2) 博客。

The wordpress blogs acts as the main site and the home-page index.

wordpress 博客充当主站点和主页索引。

Using Mage-Enabler, I have integrated wordpress and magento together and I am able to pull the checkout block, quick links and all the files.

使用 Mage-Enabler,我将 wordpress 和 magento 集成在一起,我能够提取结帐块、快速链接和所有文件。

My question is that I would like to display the top-sellers categories on the home-page. I would usually do this with the XML in the CMS. e.g.

我的问题是我想在主页上显示最畅销的类别。我通常会使用 CMS 中的 XML 来执行此操作。例如

{{block type="catalog/product_list" category_id="your_category_id" template="catalog/product/list.phtml"}} 

But this is not possible in this instance as the store home-page is not seen e.g when a user clicks on the shop-online button on the navigation it takes them onto the category lander page showing all the categories and a search.

但是在这种情况下这是不可能的,因为商店主页是不可见的,例如,当用户点击导航上的在线购物按钮时,它会将他们带到显示所有类别和搜索的类别登陆页面。

My logic has lead me to do this via PHP e.g

我的逻辑让我通过 PHP 来做到这一点,例如

<?php

$categoryId = 123; // a category id that you can get from admin
$category = Mage::getModel('catalog/category')->load($category_Id);

?>

... this would be inside a copy of the product > list.phtml page.

...这将在产品> list.phtml 页面的副本中。

Is this possible to pull products in a list via a specfic category via PHP templates rather than the block types XML in the admin?

这是否可以通过 PHP 模板而不是管理中的块类型 XML 通过特定类别将产品拉入列表中?

Thanks

谢谢

Cameron

卡梅伦

回答by Adam Moss

This oughtta do what you need:

这应该做你需要的:

<?php

$categoryid = 12;

$category = Mage::getModel('catalog/category');
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');

foreach ($collection as $_product) { ?>

<a href="<?php echo $_product->getProductUrl() ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /></a> <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>

<?php } ?>