【问题标题】:magento template for block with 2 columns具有 2 列的块的 magento 模板
【发布时间】:2012-09-11 03:52:01
【问题描述】:

我制作了一个小型 magento (1.7) 模板文件,显示一个类别中的所有产品。但是,它们仅显示在单个列中。我想分两列显示。

这会从首页执行块:

{{block type="catalog/product" name="msc.specials" template="mylib/featuredlist.phtml"}}

这是 featureslist.phtml -

<?php
//$_categoryId = $this->getCategoryId();

$productCollection = Mage::getModel('catalog/category')->load(4)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', 1)
    ->addAttributeToFilter('visibility', 4)
    ->setOrder('price', 'ASC'); 
    $cartHelper = Mage::helper('checkout/cart');
?>

<div class='block block-list'> 
<div class='block-title'><strong><span><?php echo $this->__('SPECIALS') ?></span></strong></div>
    <div class='block-content'> 
        <ul>
            <h2><?php echo $this->__( $this->getLabel() ); ?></h2>
            <?php foreach ($productCollection as $product): ?>
                <div class="item">
                    <a class="product-image" href="<?php echo $product->getProductUrl() ?>">                
                        <img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(100); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" />
                    </a>
                    <a class="product-name" href="<?php echo $product->getProductUrl() ?>"><?php echo $this->htmlEscape($product->getName()) ?></a>                     
                   <?php echo $this->getPriceHtml($product, true) ?>                       
                </div>

                <div class="cms-price-box" style=" text-align:center;"></div>
                <div class="button-row" style="text-align:center;">                 
                    <button class="button btn-cart" type="button" onclick="setLocation('<?php echo $this->getUrl('')."checkout/cart/add?product=".$product->getId()."&qty=1"  ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button>
                </div>
                <br/><br/>              
            <?php endforeach ?>
        </ul>
     </div>
</div>

【问题讨论】:

    标签: magento block two-columns


    【解决方案1】:

    这是基于 Nate 的建议和参考的解决方案。会对做同样事情的其他方式感兴趣

    <?php
    $_categoryId = $this->getCategoryId();
    
    $productCollection = Mage::getModel('catalog/category')->load($_categoryId)
        ->getProductCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('visibility', 4)
        ->setOrder('price', 'ASC'); 
        $cartHelper = Mage::helper('checkout/cart');
    
        $x = 1;
    ?>
    
    <div class='block block-list'> 
    <div class='block-title'><strong><span><?php echo $this->__('FEATURED PRODUCTS') ?></span></strong></div>
        <div class='block-content'> 
            <ul>
                <h2><?php echo $this->__( $this->getLabel() ); ?></h2>
                <table>
                <tr>            
                <?php foreach ($productCollection as $product): ?>
                <?php           
                    $x++;
                    if (($x % 2) == 0){
                        echo "</tr><tr>";
                    }
                ?>          
                <td>                
                    <div class="item">
                        <a class="product-image" href="<?php echo $product->getProductUrl() ?>">
                            <img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(180); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($product, 'small_image')) ?>" />
                        </a>
                        <br/>
                        <a class="product-name" href="<?php echo $product->getProductUrl() ?>"><?php echo $this->htmlEscape($product->getName()) ?></a>                     
                        <?php echo $this->getPriceHtml($product, true) ?>                      
                    </div>
    
                    <div class="cms-price-box" style=" text-align:center;"></div>
                    <div class="button-row" style="text-align:center;">                 
                        <button class="button btn-cart" type="button" onclick="setLocation('<?php echo $this->getUrl('')."checkout/cart/add?product=".$product->getId()."&qty=1"  ?>')" class="button"><span><?php echo $this->__('Add to Cart') ?></span></button>
                    </div>
                </td>
                <?php endforeach ?>
                </tr>
                </table>
            </ul>
         </div>
    </div>
    

    【讨论】:

      【解决方案2】:

      这实际上会有点复杂。也许我超出了范围,但你需要做几件事。它使其成为单个项目列,因为它正在执行 foreach 并且只是将每个项目吐出。要使它做两列,您需要计算列表中的项目并除以二。从那里您可以使用一些迭代将其拆分为两个数组,然后对两个结果数组进行 foreach。

      对于每个数组,您都希望它在一个 div 中,左列或右列具有 css 样式。此外,您应该决定是否需要在中间拆分项目,第一部分进入第 1 列,后部分进入第 2 列。或者您可能更喜欢奇数项目进入第 1 列,偶数项目进入第 2 列。

      如果您正在寻找更详细的答案,我可以给您一个,但这不是一个简单的答案。如果您对此感到满意,肯定会进行一些重新编码。

      以下是您可以执行的操作的概述:How to display two table columns per row in php loop

      【讨论】:

      • 我在处理收藏时遇到了一些麻烦,所以我使用了低技术 (($x % 2) == 0)。请参阅下面的答案...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 2018-11-05
      • 2016-01-21
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多