ограничить отображение товара в 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; ?>

person maddy    schedule 21.02.2015    source источник


Ответы (2)


ниже я привожу фрагмент кода, чтобы показать логику того, что вы должны делать. Это не полный код, но он должен дать представление о том, как выполнить ваше требование.

  <?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;?>

Приведенный выше код не требует пояснений. Я определил счетчик. Вам просто нужно получить категорию продукта и использовать счетчик для определения строки (я предполагаю, что у вас есть 3 продукта в строке, поэтому 6 для 2 строки).

Надеюсь, это поможет.

person aton1004    schedule 21.02.2015

Что я сделал...

в цикле Foreach для $_productCollection..

<?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 продуктов, когда это была домашняя страница. В вашем случае вы можете использовать идентификатор категории и поиграть с оператором if.

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