如何在在当前分类下随机的显示若干个产品呢?请往下看:
1.在
app\design\frontend\yourinterface\yourtheme\template\catalog\category
文件夹下建立文件current_category_products.phtml。
2.打开网站后台CMS>Static Block建立个新的block
Block Title:Random? Product
Identifier :random_current_product
content内容如下
{{block type="catalog/category_view" template="catalog/category/current_category_products.phtml"}}
3.编辑我们刚刚建立的current_category_products.phtml文件。添加如下内容:
<?php /* ################################################################# header of the Get random products at the current category */ ?> <div class="jewelry_product"> <h2>Hot sale</h2> <div class="jewelry_productlist"> <?php $_helper = $this->helper('catalog/output');?> <?php $_currentCategoryId = Mage::registry('current_category')->getId();//Get the current category;?> <?php $showCount = 6;//Random number of custom display ?> <?php if($_currentCategoryId):?> <?php //echo get_class($this).'<br>';?> <?php $products = Mage::getModel('catalog/category')->load($_currentCategoryId)->getProductCollection();?> <?php foreach($products as $_products ):?> <?php $_productArrayId[] = $_products->getId();?> <?php //echo $_products->getId();?> <?php endforeach;?> <ul class="goodlist_1"> <?php if(count($_productArrayId)>$showCount):?> <?php foreach(array_rand($_productArrayId,$showCount) as $_productsId):?> <?php if(in_array($_productsId,$_productArrayId)):?> <?php //echo 'this product id is:'.$_productsId.'<br>';?> <?php $_product = Mage::getModel('catalog/product')->load($_productsId);?> <?php //var_dump($_product);?> <li> <span class="img"><a class="product-image" href="<?php echo $_product->getProductUrl();?>"><img src="<?php echo $_product->getImageUrl();?>" alt="<?php echo $_product->getName()?>"></a></span> <h3 class="title"><span><?php echo $_product->getName();?></span></h3> <span class="price"><?php $priceContent = ($_product->getSpecialPrice())?'<div class="price-box"><span id="old-price-'.$_product->getId().'" class="old-price">$'.sprintf("%.2f", $_product->getPrice()).'</span><span id="product-price-'.$_product->getId().'" class="special-price"> $'.sprintf("%.2f", $_product->getSpecialPrice()).'</span></div>': '<div class="price-box"><span id="product-price-'.$_product->getId().'" class="regular-price"><span class="price">$'.sprintf("%.2f", $_product->getPrice()).'</span></span></div>' ?> <?php echo $priceContent; ?> </span> </li> <?php endif?> <?php endforeach;?> <?php else:?> <?php foreach($_productArrayId as $_productsId):?> <?php if(in_array($_productsId,$_productArrayId)):?> <?php //echo 'this product id is:'.$_productsId.'<br>';?> <?php $_product = Mage::getModel('catalog/product')->load($_productsId);?> <?php //var_dump($_product);?> <li> <span class="img"><a class="product-image" href="<?php echo $_product->getProductUrl();?>"><img src="<?php echo $_product->getImageUrl();?>" alt="<?php echo $_product->getName()?>"></a></span> <h3 class="title"><span><?php echo $_product->getName();?></span></h3> <span class="price"><?php $priceContent = ($_product->getSpecialPrice())?'<div class="price-box"><span id="old-price-'.$_product->getId().'" class="old-price">$'.sprintf("%.2f", $_product->getPrice()).'</span><span id="product-price-'.$_product->getId().'" class="special-price"> $'.sprintf("%.2f", $_product->getSpecialPrice()).'</span></div>': '<div class="price-box"><span id="product-price-'.$_product->getId().'" class="regular-price"><span class="price">$'.sprintf("%.2f", $_product->getPrice()).'</span></span></div>' ?> <?php echo $priceContent; ?> </span> </li> <?php endif?> <?php endforeach;?> <?php endif ?> </ul> <?php endif ?> </div> </div> <?php /* Bottom of the Get random products at the current category */ ################################################################# ?>
4.在需要的地方引用这个块
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('random_current_product')->toHtml(); ?>
原创文章,版权所有。