Magento 1.4 - 在特定类别下显示一些产品

发布于 2024-10-02 02:27:57 字数 81 浏览 2 评论 0原文

你好 我已将 20 个产品分配给名为“电话”的类别,我想创建一个模块来检索这些产品并以列表格式显示。有人可以告诉我该怎么做吗?

谢谢

Hi
I have assigned 20 products to a category called Phone, I would like to create a module to retrieve these products and displayed as a list format. Could someone tell me how to do this?

thanks

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

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

发布评论

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

评论(3

甜扑 2024-10-09 02:27:57

要创建使用类别执行某些操作的小部件(您可以通过 cms 插入),请首先使用以下命令创建标准模块结构:
/堵塞
/ETC
/帮手
/Model

请注意,在下面的代码示例和文件名中,您需要将 [Namespace]、[Module] 和 [module] 替换为您想要使用的适当的命名空间和模块。案例很重要!

首先创建 app/code/local/[Namespace]/[Module]/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <[Namespace]_[Module]>
      <version>0.0.1</version>
    </[Namespace]_[Module]>
  </modules>
  <global>
    <helpers>
      <[module]>
        <class>[Namespace]_[Module]_Helper</class>
      </[module]>
    </helpers>
    <blocks>
      <[module]>
        <class>[Namespace]_[Module]_Block</class>
      </[module]>
    </blocks>
    <models>
      <[module]>
        <class>[Namespace]_[Module]_Model</class>
      </[module]>   
    </models>
  </global>
</config>

然后创建 app/code/local/[Namespace]/[Module]/etc/widget.xml 该小部件包含一个名为“selected_category”

<?xml version="1.0"?>
<widgets>
  <[module]_category type="[module]/category">
    <name>[Module]: Category</name>
    <description type="desc">Adds a [module] for a category.</description>
    <parameters>
      <selected_category>
        <label>Categories</label>
        <visible>1</visible>
        <required>1</required>
        <type>select</type>
        <source_model>[module]/catopt</source_model>
      </selected_category>
    </parameters>
  </[module]_category>
</widgets>

然后是 app/code/local/[Namespace]/[Module]/Helper/Data.php 中的必需 Helper 文件

 <?php
    class [Namespace]_[Module]_Helper_Data extends Mage_Core_Helper_Abstract
    {
    }

然后是一个允许用户在小部件对话框中选择类别的模型。这位于 app/code/local/[Namespace]/[Module]/Model/Catopt.php

<?php
class [Namespace]_[Module]_Model_Catopt
{
    public function toOptionArray()
    {
        $category = Mage::getModel('catalog/category'); 
        $tree = $category->getTreeModel(); 
        $tree->load();
        $ids = $tree->getCollection()->getAllIds(); 
        $arr = array();
        if ($ids){ 
          foreach ($ids as $id){ 
            $cat = Mage::getModel('catalog/category'); 
            $cat->load($id); 
            array_push($arr, array('value' => $id, 'label' => $cat->getName().' ('.$cat->getProductCount().')')); 
          } 
        }
        uasort($arr, array($this, 'labelsort'));
        return $arr;
    }

    function labelsort($a, $b){
      if ( $a['label'] == $b['label'] ) 
                  return 0; 
              else if ( $a['label'] < $b['label'] ) 
                  return -1; 
              else 
                  return 1;
    }
}

最后,在模块方面,一个块位于 app/code/local/[Namespace]/[Module]/Block/ 中Category.php 此块使用自定义 .phtml 文件进行显示,但您可以通过更改块的类型和 setTemplate 的输入来更改它以使用您可能需要显示的任何其他内容。

<?php
class [Namespace]_[Module]_Block_Category
    extends Mage_Core_Block_Template
    implements Mage_Widget_Block_Interface
{

        /**
         * A model to serialize attributes
         * @var Varien_Object
         */
        protected $_serializer = null;

        /**
         * Initialization
         */
        protected function _construct()
        {
            $this->_serializer = new Varien_Object();
            $this->setTemplate('[module]/[module].phtml');
            parent::_construct();
        }

        public function getCategory(){
          return $this->getData('selected_category');
        }
}

不要忘记在 /app/etc/modules/[Namespace]_[Module].xml 下添加模块安装文件,如下所示

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Cms />
            </depends>
        </[Namespace]_[Module]>
    </modules>
</config>

最后,您需要创建一个模板文件来显示块内容。这将位于 /app/design/frontend/default/default/template/[module]/[module].phtml 下。

这个 .phtml 文件可以使用 $this->getCategory() 获取类别并从那里开始。您可以轻松自定义这些示例中包含的块以显示默认的 magento 产品列表网格,而不是使用自定义 .phtml 文件。

To create a widget (which you can insert via the cms) that uses a category to do something, begin by creating a standard module structure with:
/Block
/etc
/Helper
/Model

Note that in my code samples and filenames below you will need to replace [Namespace], [Module], and [module] with the appropriate namespace and module that you want to use. Case is important!

Begin by creating app/code/local/[Namespace]/[Module]/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <[Namespace]_[Module]>
      <version>0.0.1</version>
    </[Namespace]_[Module]>
  </modules>
  <global>
    <helpers>
      <[module]>
        <class>[Namespace]_[Module]_Helper</class>
      </[module]>
    </helpers>
    <blocks>
      <[module]>
        <class>[Namespace]_[Module]_Block</class>
      </[module]>
    </blocks>
    <models>
      <[module]>
        <class>[Namespace]_[Module]_Model</class>
      </[module]>   
    </models>
  </global>
</config>

Then create a app/code/local/[Namespace]/[Module]/etc/widget.xml This widget includes a setting called "selected_category"

<?xml version="1.0"?>
<widgets>
  <[module]_category type="[module]/category">
    <name>[Module]: Category</name>
    <description type="desc">Adds a [module] for a category.</description>
    <parameters>
      <selected_category>
        <label>Categories</label>
        <visible>1</visible>
        <required>1</required>
        <type>select</type>
        <source_model>[module]/catopt</source_model>
      </selected_category>
    </parameters>
  </[module]_category>
</widgets>

Then the obligatory Helper file in app/code/local/[Namespace]/[Module]/Helper/Data.php

 <?php
    class [Namespace]_[Module]_Helper_Data extends Mage_Core_Helper_Abstract
    {
    }

Then a model to allow the user to select the category in the widget dialog box. This goes in app/code/local/[Namespace]/[Module]/Model/Catopt.php

<?php
class [Namespace]_[Module]_Model_Catopt
{
    public function toOptionArray()
    {
        $category = Mage::getModel('catalog/category'); 
        $tree = $category->getTreeModel(); 
        $tree->load();
        $ids = $tree->getCollection()->getAllIds(); 
        $arr = array();
        if ($ids){ 
          foreach ($ids as $id){ 
            $cat = Mage::getModel('catalog/category'); 
            $cat->load($id); 
            array_push($arr, array('value' => $id, 'label' => $cat->getName().' ('.$cat->getProductCount().')')); 
          } 
        }
        uasort($arr, array($this, 'labelsort'));
        return $arr;
    }

    function labelsort($a, $b){
      if ( $a['label'] == $b['label'] ) 
                  return 0; 
              else if ( $a['label'] < $b['label'] ) 
                  return -1; 
              else 
                  return 1;
    }
}

Finally on the module side of things a block which goes in app/code/local/[Namespace]/[Module]/Block/Category.php This block is using a custom .phtml file for it's display but you can change that to use anything else you might need to show by changing the type of block and input to setTemplate.

<?php
class [Namespace]_[Module]_Block_Category
    extends Mage_Core_Block_Template
    implements Mage_Widget_Block_Interface
{

        /**
         * A model to serialize attributes
         * @var Varien_Object
         */
        protected $_serializer = null;

        /**
         * Initialization
         */
        protected function _construct()
        {
            $this->_serializer = new Varien_Object();
            $this->setTemplate('[module]/[module].phtml');
            parent::_construct();
        }

        public function getCategory(){
          return $this->getData('selected_category');
        }
}

Don't forget to add a module install file under /app/etc/modules/[Namespace]_[Module].xml like this

<?xml version="1.0"?>
<config>
    <modules>
        <[Namespace]_[Module]>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Cms />
            </depends>
        </[Namespace]_[Module]>
    </modules>
</config>

Lastly you need to create a template file to display the block content. This will go under /app/design/frontend/default/default/template/[module]/[module].phtml

This .phtml file can use $this->getCategory() to get the category and go from there. You can easily customize the block included in these samples to display the default magento product list grids instead of using a custom .phtml file.

陈甜 2024-10-09 02:27:57

无需创建模块。只需将其放在布局中的一个块中即可:它将显示链接到指定类别 (id=XXX) 的所有产品。

<!-- Show all products linked to this category -->
<block type="catalog/product_list" name="best_sellers" template="catalog/product/list.phtml">
    <action method="setCategoryId">
        <category_id>XXX</category_id>
    </action>
</block>

更新:

您可以创建一个覆盖“Mage_Catalog_Block_Product_List”的模块,并添加一个方法来限制一定数量的产品。

1- 创建“app/code/local/[Namespace]/Catalog/etc/config.xml”并将其放入其中:

<config>
    <modules>
        <[Namespace]_Catalog>
            <version>0.1.0</version>
        </[Namespace]_Catalog>
    </modules>

    <global>

        <blocks>
            <catalog>
                <rewrite>
                    <product_list>[Namespace]_Catalog_Block_Product_List</product_list>
                </rewrite>
            </catalog>
        </blocks>

    </global>
</config>

2- 通过创建类覆盖块:“app/code/local/[Namespace]/Catalog /Block/Product/List.php"

class [Namespace]_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_List
{
    /**
     * Default number of product to show.
     *
     * @var int default = 5
     */
    private $_productCount = 5;

    /**
     * Initialize the number of product to show.
     *
     * @param int $count
     * @return Mage_Catalog_Block_Product_List
     */
    public function setProductCount($count)
    {
        $this->_productCount = intval($count);

        return $this;
    }


    /**
     * Get the number of product to show.
     *
     * @return int
     */
    public function getProductCount()
    {
        return $this->_productCount;
    }
}

3-覆盖您的主题以添加产品限制功能:

将“app/design/frontend/default/default/template/catalog/product/list.phtml”复制到“app/design/frontend” /default/[your_theme]/template/catalog/product/list.phtml"

// Insert between the foreachs and <li> for the list mode and grid mode
<?php if($_iterator < $this->getProductCount()) : ?>
...
// Insert between the foreachs and <li> for the list mode and grid mode
<?php endif; ?>

4- 在主页内容选项卡中,在您想要的位置添加此行:

// category_id = Procucts linked to this category
// product_count = Maximum number of product
{{block type="catalog/product_list" category_id="7" product_count="3" template="catalog/product/list.phtml"}}

希望这对某人有帮助。

No need to create a module. just place this in a block in your layout: It will show all the products linked to the specified category (id=XXX).

<!-- Show all products linked to this category -->
<block type="catalog/product_list" name="best_sellers" template="catalog/product/list.phtml">
    <action method="setCategoryId">
        <category_id>XXX</category_id>
    </action>
</block>

Update:

You can create a module that overide the "Mage_Catalog_Block_Product_List", and add a method to limit a certain number of products.

1- Create "app/code/local/[Namespace]/Catalog/etc/config.xml" and put this in it:

<config>
    <modules>
        <[Namespace]_Catalog>
            <version>0.1.0</version>
        </[Namespace]_Catalog>
    </modules>

    <global>

        <blocks>
            <catalog>
                <rewrite>
                    <product_list>[Namespace]_Catalog_Block_Product_List</product_list>
                </rewrite>
            </catalog>
        </blocks>

    </global>
</config>

2- Override the Block by creating the class: "app/code/local/[Namespace]/Catalog/Block/Product/List.php"

class [Namespace]_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_List
{
    /**
     * Default number of product to show.
     *
     * @var int default = 5
     */
    private $_productCount = 5;

    /**
     * Initialize the number of product to show.
     *
     * @param int $count
     * @return Mage_Catalog_Block_Product_List
     */
    public function setProductCount($count)
    {
        $this->_productCount = intval($count);

        return $this;
    }


    /**
     * Get the number of product to show.
     *
     * @return int
     */
    public function getProductCount()
    {
        return $this->_productCount;
    }
}

3- Overide your theme to add the product limit feature:

copy "app/design/frontend/default/default/template/catalog/product/list.phtml" to "app/design/frontend/default/[your_theme]/template/catalog/product/list.phtml"

// Insert between the foreachs and <li> for the list mode and grid mode
<?php if($_iterator < $this->getProductCount()) : ?>
...
// Insert between the foreachs and <li> for the list mode and grid mode
<?php endif; ?>

4- In the home page content tab, add this line where you want it:

// category_id = Procucts linked to this category
// product_count = Maximum number of product
{{block type="catalog/product_list" category_id="7" product_count="3" template="catalog/product/list.phtml"}}

Hope this help someone.

安静被遗忘 2024-10-09 02:27:57

感谢您提供信息丰富的帖子。对于那些不太精通 PHP 但由于正在寻找显示给定类别的产品名称列表的解决方案而登陆此页面的人,我通过简单地修改其他人的模板文件就找到了解决方案。对于这个解决方案,我发现最适合的扩展是:

http://www.cubewebsites.com/blog/magento/extensions/freebie-magento-featured-products-widget-version-2/

(在 github 上查找最新版本:https://github.com/cubewebsites/Cube-Category-Featured-Products/tags) 。

登录和注销并清除缓存后,我能够将小部件插入静态块并修改用于生成我想要的自定义视图的 .phtml 文件。

插入时,小部件看起来像这样:

{{widget type="categoryfeatured/list" template="categoryfeatured/block.phtml" categories="118" num_products="10" products_per_row="1" product_type="all"}}. 

我只需打开

app/design/frontend/base/default/template/categoryfeatured/block.phtml

复制其内容并创建一个名为 Category_product_listing.phtml 的新 .phtml 文件

,然后将小部件实例指向新的 .phtml 文件如下:

{{widget type="categoryfeatured/list" template="categoryfeatured/category_product_listing.phtml" categories="118" num_products="10" products_per_row="1" product_type="all"}}. 

然后,我根据对 PHP 的基本了解浏览了这个 .phtml 文件,并删除了所有项目,如图像、添加到购物车按钮、评论等,直到只剩下基本的链接产品标题因为类别标题保持不变。

我希望这对某人有帮助,因为我花了几个小时试图解决这个问题。

Thanks for the informative post. For those of you who are not so fluent in PHP but landed on this page because you were looking for a solution to display a product name list from a given category I managed to find a solution by simply modifying someone else's template file. For this solution I found the best suited extension was:

http://www.cubewebsites.com/blog/magento/extensions/freebie-magento-featured-products-widget-version-2/

(find the latest version on github: https://github.com/cubewebsites/Cube-Category-Featured-Products/tags).

After logging in and out and clearing the cache I was able to insert the widget into a static block and modify the .phtml file used to produce the custom view that I wanted.

The widget looked like this when inserted:

{{widget type="categoryfeatured/list" template="categoryfeatured/block.phtml" categories="118" num_products="10" products_per_row="1" product_type="all"}}. 

I simply opened

app/design/frontend/base/default/template/categoryfeatured/block.phtml

copied it's contents and created a new .phtml file called category_product_listing.phtml

and then pointed the widget instance to the new .phtml file as follows:

{{widget type="categoryfeatured/list" template="categoryfeatured/category_product_listing.phtml" categories="118" num_products="10" products_per_row="1" product_type="all"}}. 

I then went through this .phtml file with my basic understanding of PHP and removed all items like images, add to cart buttons, reviews, etc. until I was left with just the basic linked product title as well as the category title left intact.

I hope this helps someone as I spent hours trying to figure this out.

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