限制 magento 中的产品显示

limit product display in magento

此代码用于在主页上显示不同类别的产品。

{{block type="catalog/product_list" template="catalog/product/main_product_list.phtml" category_id="9"}}

{{block type="catalog/product_list" template="catalog/product/main_product_list.phtml" category_id="10"}}

{{block type="catalog/product_list" template="catalog/product/main_product_list.phtml" category_id="11"}}

显示多行产品。但我只想为第一类显示一排产品,为剩余类别显示两行。

我的main_product_list.phtml代码是

   <?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
    $setting = Mage::helper('galabrandstoresettings');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>" <?php if($setting->getProductsList_Gutter()): ?>style="margin-bottom: <?php echo $setting->getProductsList_Gutter() ?>px"<?php endif; ?>>

            <!-- Show Thumbnail -->
            <?php if ($setting->getProductsList_ShowThumbnail()): ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                <?php if ($setting->getProductsList_ShowLabel()):?>
                    <!--show label product - label extension is required-->
                    <?php Mage::helper('productlabels')->display($_product);?>
                <?php endif; ?>
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($setting->getProductsList_Width(285), $setting->getProductsList_Height()); ?>" width="<?php echo $setting->getProductsList_Width(285) ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" <?php if ($alt_img = $setting->getProductsList_AltImg()): ?>
                rel="<?php echo $alt_img ?>"
                data-alt-src="<?php echo $this->helper('catalog/image')->init($_product, $alt_img)->resize($setting->getProductsList_Width(285), $setting->getProductsList_Height()); ?>"
                <?php endif ?>
                alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
            </a>
            <?php else: ?>
                <?php if ($setting->getProductsList_ShowLabel()):?>
                    <!--show label product - label extension is required-->
                    <?php Mage::helper('productlabels')->display($_product);?>
                <?php endif; ?>
            <?php endif ?>

            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>

                     <!--product description-->
                    <?php if ($setting->getProductsList_ShowDesc()): ?>
                        <div class="desc std">
                            <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                        </div>
                    <?php endif ?>

                    <!--show reviews-->
                    <?php if ($setting->getProductsList_ShowReviews()):?>
                        <?php if($_product->getRatingSummary()): ?>
                        <?php echo $this->getReviewsSummaryHtml($_product) ?>
                        <?php endif; ?>
                    <?php endif ?>

                    <!--product price-->
                    <?php if ($setting->getProductsList_ShowPrice()): ?>
                        <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php endif ?>

                    <?php if($_product->isSaleable()): ?>
                        <!--show button add to cart-->
                        <?php if ($setting->getProductsList_ShowAddtocart()): ?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                        <?php endif ?>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

                    <!--show button add to compare-wishlist-->
                    <?php if ($setting->getProductsList_ShowAddto()): ?>
                        <ul class="add-to-links">
                            <?php if ($this->helper('wishlist')->isAllow()) : ?>
                                <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist" title="<?php echo $this->__('Add to Wishlist') ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                            <?php endif; ?>
                            <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                                <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare" title="<?php echo $this->__('Add to Compare') ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                            <?php endif; ?>
                        </ul>
                    <?php endif ?>
                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

    <?php else: ?>

    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php if ($_collectionSize > 0): ?>
    <ul class="products-grid row">
    <?php $i=0; foreach ($_productCollection as $_product): ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?> <?php echo $setting->getProductsGrid_Item('span25'); ?>"style="<?php if($setting->getProductsGrid_Width()): ?>width: <?php echo $setting->getProductsGrid_Width() ?>px;<?php endif; ?> <?php if($setting->getProductsGrid_Gutter()): ?>margin-right: <?php echo $setting->getProductsGrid_Gutter() ?>px;<?php endif; ?>"><div class="product-item">

                <div class="product-info">

                <!-- Show Thumbnail -->
                <?php if ($setting->getProductsGrid_ShowThumbnail()): ?>

                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">

                        <?php if ($setting->getProductsGrid_ShowLabel()):?>
                            <!--show label product - label extension is required-->
                            <?php Mage::helper('productlabels')->display($_product);?>
                        <?php endif;?>

                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize($setting->getProductsGrid_Width(285), $setting->getProductsGrid_Height()); ?>" width="<?php echo $setting->getProductsGrid_Width(285) ?>" <?php if ($alt_img = $setting->getProductsGrid_AltImg()): ?>
                rel="<?php echo $alt_img ?>"
                data-alt-src="<?php echo $this->helper('catalog/image')->init($_product, $alt_img)->resize($setting->getProductsGrid_Width(285), $setting->getProductsGrid_Height()); ?>"
                <?php endif ?> alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" />
                    </a>
                <?php else: ?>
                    <?php if ($setting->getProductsGrid_ShowLabel()):?>
                        <!--show label product - label extension is required-->
                        <?php Mage::helper('productlabels')->display($_product);?>
                    <?php endif;?>
                <?php endif; ?>

                    <div class="name-desc">

<!--                     product name
                    <?php if ($setting->getProductsGrid_ShowProductName()):?>
                        <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                    <?php endif; ?> -->

                    <!--product description-->
                    <?php if ($setting->getProductsGrid_ShowDesc()): ?>
                        <div class="desc std">
                            <?php
                                $shortdes =  $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description');
                                if(strlen($shortdes)>50) { //dem ki tu chuoi $str, 10o la chieu dai muon quy dinh
                                    $strCutTitle = substr($shortdes, 0, 50); //cat 100 ki tu dau
                                    $shortdes = substr($strCutTitle, 0, strrpos($strCutTitle, ' '));
                                }
                                echo $shortdes;
                            ?>
                        </div>
                    <?php endif; ?>

                    </div>
                </div>
            <!--product name-->
                    <?php if ($setting->getProductsList_ShowProductName()):?>
                        <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                    <?php endif ?>

                <!--show reviews-->
                <?php if ($setting->getProductsGrid_ShowReviews()):?>
                    <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                    <?php endif; ?>
                <?php endif; ?>

                <!--product price-->
                <?php if ($setting->getProductsGrid_ShowPrice()): ?>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                <?php endif; ?>

                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <!--show button add to cart-->
                        <?php if ($setting->getProductsGrid_ShowAddtocart()): ?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="  setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                        <?php endif; ?>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

                    <!--show button add to compare-wishlist-->
                    <?php if ($setting->getProductsGrid_ShowAddto()): ?>
                        <ul class="add-to-links">
                            <?php if ($this->helper('wishlist')->isAllow()) : ?>
                                <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist" title="<?php echo $this->__('Add to Wishlist') ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                            <?php endif; ?>
                            <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                                <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare" title="<?php echo $this->__('Add to Compare') ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                            <?php endif; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div></li>
        <?php endforeach ?>
        </ul>
    <?php endif; ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

</div>
<?php endif; ?>

下面我提供了代码片段来展示你应该做的逻辑do.This不是完整的代码但应该提供如何实现你的要求的想法。

  <?php 
$count=0;
$count1=0;
?>
<?php foreach ($_productCollection as $_product): ?>
    <!-- get current category of the product-->
    <?php $currentCategory = $_product->getCategoryIds();  ?>
        <?php if($currentCategory==9):?>
        <!-- If your row contain 3 product it will display upto one row -->
        <?php if($count<3){ 

             //Your code to get product 
         } ?>

        <?php elseif($currentCategory==10||$currentCategory==11):?>
            <!-- If your row contain 3 product it will display upto two row -->
        <?php if($count1<6){ 

         //Your code to get product  
            $count1++;
     } ?>       
        <?php endif;?>
<?php endforeach;?>

上面的代码是自己explanatory.I定义了一个计数器。您只需要获取产品的类别并使用计数器来确定行(我假设您连续有 3 个产品,所以 2 行 6 个)。

希望这会有所帮助。

我做了什么...

在 $_productCollection 的 Foreach 循环中..

<?php $home = Mage::getSingleton('cms/page')->getIdentifier(); ?>

<?php $i=0; foreach ($_productCollection as $_product): ?>      

        <?php if( $i >= 8 && $home == "home" ): break; endif; 
 .... Code .....

  <?php $i++; ?>
<?php endforeach; ?>

在我的例子中,我只想在主页上显示 8 个产品。在您的情况下,您可以使用类别 ID 并在 if 语句中使用。

 <?php $category_id = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId(); ?>