如何在类别的“描述”中显示随机产品

时间:2014-05-20 10:18:16

标签: html magento

我正在尝试在我的网站上制作我的产品的目标网页,但我希望它在页面顶部显示随机产品(在说明框中)。我已经获得了随机产品在random.phtml中的所有代码,它的工作原理是因为我在“测试”CMS页面上测试了它,代码如下:

{{block type="midmedssettings/product_list" num_products="5" category_id="145" template="catalog/product/random.phtml"}}

但是,当我将其放入我的类别的“描述”框中时,它不会将其识别为代码并仅显示代码。我确实试过了

<block type="midmedssettings/product_list" num_products="5" category_id="145" template="catalog/product/random.phtml">

但没有显示任何内容。

random.phtml

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * @category   design_default
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
?>
<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>

<?php
Mage::getSingleton('catalog/layer')->setData("product_collection",NULL);
$this->_productCollection = null;
$_productCollection=$this->getLoadedProductCollection();
?>

<?php $_productCollection=$this->getLoadedProductCollection() ?>
<?php if(!$_productCollection->count()): ?>
<div class="padder">
<div class="note-msg">
    <?php echo $this->__('There are no products matching the selection.') ?>
</div>
</div>
<?php else: ?>

<?php // Grid Mode ?>
<div class="listing-type-grid  catalog-listing padder"> <!-- the class name will change to .listing-type-cell if viewing in list mode -->
<?php $_collectionSize = $_productCollection->count() ?>

    <?php $_items = $_productCollection->getItems(); 
           shuffle($_items); ?>

    <table cellspacing="0" class="generic-product-grid" id="product-list-table">
    <?php $i=0; foreach ($_items as $_product): ?>
    <?php if ($i++%4==0): ?>
    <tr>
    <?php endif ?>
        <td>
            <p class="product-image">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 170); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
                </a>
            </p>
            <h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5>
            <?php if($_product->getRatingSummary()): ?>
                <!--?php echo $this->getReviewsSummaryHtml($_product, 'short') ?-->
                <?php echo $this->getReviewsSummaryHtml($_product) ?>
            <?php endif; ?>

            <?php echo $this->getPriceHtml($_product, true) ?>

            <div class="clear"></div>
            <ul class="add-to">

                <?php if($_product->isSaleable()): ?>
                <li class="add-to-cart-textlink"><a href="#" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span>Add to Cart</span></a></li>
                <?php else: ?>
                <li><?php echo $this->__('Out of stock') ?></li>
                <?php endif; ?>

                <?php if ($this->helper('wishlist')->isAllow()) : ?>
                <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                <?php endif; ?>
                <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                <li><a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                <?php endif; ?>
            </ul>
        </td>
    <?php if ($i%4==0 && $i!=$_collectionSize): ?>
    </tr>
    <?php endif ?>

    <?php if ($i==4) break;  // show 4 products max ?> 

    <?php endforeach ?>
    <?php for($i;$i%4!=0;$i++): ?>
          <td class="empty-product">&nbsp;</td>
    <?php endfor ?>
    <?php if ($i%4==0): ?>
    </tr>
    <?php endif ?>
    </table>
    <script type="text/javascript">decorateTable('product-list-table')</script>
</div>

<?php endif; ?>

有人可以告诉我哪里出错了。

提前谢谢

3 个答案:

答案 0 :(得分:1)

对于随机产品,请按照以下步骤操作:

   goto app/code/core/Mage/Catalog/Block/Product/List/ here you will create a Random.php file and add below code in this file.

 <?php
     class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List
            {
                protected function _getProductCollection()
                {
                    if (is_null($this->_productCollection)) {
                        $categoryID = $this->getCategoryId();
                        if($categoryID)
                        {
                          $category = new Mage_Catalog_Model_Category();
                          $category->load($categoryID); // this is category id
                          $collection = $category->getProductCollection();
                        } else
                        {
                          $collection = Mage::getResourceModel('catalog/product_collection');
                        }
                        Mage::getModel('catalog/layer')->prepareProductCollection($collection);
                        $collection->getSelect()->order('rand()');
                        $collection->addStoreFilter();
                        $numProducts = $this->getNumProducts() ? $this->getNumProducts() : 3;
                        $collection->setPage(1, $numProducts)->load();

                        $this->_productCollection = $collection;
                    }
                    return $this->_productCollection;
                }
            }


  now create static block insert that code or get static block on file for view product.
{{block type="catalog/product_list_random" category_id="YOUR_CATEGORY_ID" template="catalog/product/list.phtml" column_count="4" num_products="12"}}

答案 1 :(得分:0)

从视图中尝试。

  1. 编辑模板/目录/类别/ view.phtml
  2. 将您的块调用代码放在首选行。黑色调用代码如下所示:
  3. $this->getLayout() ->createBlock("midmedssettings/product_list") ->setTemplate("catalog/product/random.phtml") ->toHtml();

答案 2 :(得分:0)

1)使用标识random-products和内容

从管理端创建CMS静态阻止
{{block type="catalog/product_list" template="catalog/product/random.phtml"}}

2)在 template / catalog / category / view.phtml 中,根据您要显示产品的位置,粘贴以下代码

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('random-products')->toHtml();?>

3)在目录/ product / random.phtml 中,粘贴以下代码,

<?php


 $categoryId = Mage::registry('current_category')->getEntityId();
 $_helper = $this->helper('catalog/output');
 $_category = Mage::getModel('catalog/category')->load($categoryId);
 $_productCollection = Mage::getResourceModel('reports/product_collection')
                       ->addAttributeToSelect('*')
                       ->addCategoryFilter($_category)
                       ->setVisibility(array(2,3,4));
 $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));                  
 $_productCollection->setPage(1, 5);

 ?>


 <?php foreach ($_productCollection as $_product): ?>

 <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
            <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>


            <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h3><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h3>

 <?php endforeach; ?>

您可以根据需要更改 random.phtml 的设计,并在每次重新加载页面时,它会显示分配到您的类别的 5个随机产品。

相关问题