[Magento] 创建子分类列表代替产品列表页

由于最近有个Magento项目,客户产品分类较多,需求是将Magento默认产品列表替换成产品子分类展示,如果你也有同样的需求,可以跟着下面的步骤,一步步的实现:

功能:

  1. 调用产品分类描述
  2. 调用子分类的分类图片作为特色图片
  3. 如果子分类未设置分类图片,则调用子分类产品中第一个产品图片作为特色图片

你可能需要调整部分HTML代码匹配你的网站的风格,我这里只是举例说明,默认分类下是直接显示产品列表的,如图:

sub-category-listing1

  1. 创建一个名为sub_navigation.phtml的文件
  2. 粘贴在下面的代码(适用1.7.2.0+)
<?php

/* File Location

* /new/app/design/frontend/base/default/template/catalog/navigation

* Compatible with: Magento Community 1.7.2.0

*/

?>

<div id="categories">

<div class="col_full">

<div class="listing" >

<?php

	function getImageUrl($category)

    {

    $cur_category=Mage::getModel('catalog/category')->load($category->getId());

    $layer = Mage::getSingleton('catalog/layer');

    $layer->setCurrentCategory($cur_category);

            $url = $this->getCurrentCategory()->getImageUrl();

        return $url;

    };

    //Get the Current Category

    $_maincategorylisting=$this->getCurrentCategory();    

    // Iterate all categories in store

   foreach ($this->getCurrentChildCategories() as $_category):

        // If category is Active

        if($_category->getIsActive()):

            // Load the actual category object for this category

            $cur_category = Mage::getModel('catalog/category')->load($_category->getId());

            // Load a random product from this category

            $products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category);

            $products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(100);

            $products->load();

            // This a bit of a fudge - there's only one element in the collection

            $_product = null;

            foreach ( $products as $_product ) {}

			if(isset($_product)):

            ?>

            <div class="sub-category-listing" style="float: left; margin-left: 20px; margin-right: 10px; padding: 0px; text-align: center; width: 155px; ">

                <div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_category)?>" class="product-image">

            <?php

            $layer = Mage::getSingleton('catalog/layer');

    		$layer->setCurrentCategory($cur_category);

            ?>      

            <?

            // If there is an image set for the category - Display it

            if($_imgUrl=$this->getCurrentCategory()->getImageUrl()):?>

            	<img src="<?php echo $_imgUrl ?>" style="padding: 10px;" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />

			<?php endif; ?>

            <?

            // If there is not a image set for that Category - Display a random product Image

            if(!$_imgUrl): 

			// Let's load the category Model and grab the product collection of that category

			$product_collection = Mage::getModel('catalog/category')->load($_category->getId())->getProductCollection();

			$product_collection->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);

			// Now let's loop through the product collection and print the ID of every product

			foreach($product_collection as $product) {

			// Get the product ID

			$product_id = $product->getId();

			// Load the full product model based on the product ID

			$full_product = Mage::getModel('catalog/product')->load($product_id);

  			// Now that we loaded the full product model, let's access all of it's data

  			// Let's get the Product Name

  			$product_name = $full_product->getName();

  			// Let's get the Product URL path

  			$product_url = $full_product->getProductUrl();

  			// Let's get the Product Image URL

  			$product_image_url = $full_product->getImageUrl();

  			// Let's print the product information we gathered and continue onto the next one

			} //End For Each

			?>

            	<img src="<?php echo $product_image_url; ?>" width="135" height="135"  style="padding: 10px;" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />

            <?php endif; ?>

            </a>

           		</div>

           	<a href="<?php echo $this->getCategoryUrl($_category)?>">

           	<h2 class="product-name" style="margin-top: 10px;"><?php echo $_category->getName()?></a></h2>

            <? if($_description=$this->getCurrentCategory()->getDescription()):?>

				<p class="category-description">

					<?php echo $_description ?></p>

			<?php endif; ?>

            </div>

            <?php

     		endif;

		endif;

    endforeach;

?>

</div>

<br clear=all>

</div>

</div>
  1. 上传 sub_navigation.phtml文件到Magent目录:/app/design/frontend/base/default/template/catalog/navigation
  2. 管理员登陆到后台点击 CMS > Static Blocks
  3. 点击添加新区块:Add new Block
  4. 设置几个参数 block title:Sub Category Listing ; Identifier:sub_category_listing;  Status:Enabled;
  5. Content :
    {{block type="catalog/navigation" template="catalog/navigation/sub_navigation.phtml"}}

     

sub-category-listing

 

  1. 点击保存: Save block.
  2. 点击 Catalogue > Manage Categories
  3. 点选一个分类:Category ( 前提是这个分类要有子分类)
  4. 选择 Display Settings 标签
  5. 改一下 Display Mode: Static block only(重要,默认则直接显示产品列表,先择只显示静态区块);下面的 CMS Block 选择之前创前的区块: Sub Category Listing; Is Anchor:Yes

sub-category-listing2

 

  1. 保存分类 点击:Save Category
  2. 现在到前台看一下分类列表,就能看到子分类列表了
  3. 重复创建区块,可以创建更多不同功能的区块

 

624 total views, 2 views today

Revisions

There are no revisions for this post.

Tags:

No comments yet.

发表评论