是否可以在自定义模块内显示 magento 产品列表块


Is it possible to display magento products list block inside a custom module

我正在构建一个magento模块,它的某些功能要求我显示一组过滤的产品结果。现在理想情况下,我想为此使用洋红色自己的产品列表块,这样我就不必重建布局/分页等......是否可以将此块合并到我的模块模板中?

或者,是否可以加载 magento 类别但对其应用相同的过滤器?在我的情况下,一组特定的 SKU?

是的,

您可以使用 Defaul 产品集合和工具栏显示在自定义模块中

只需仔细按照以下说明进行操作即可。

在你的中应该看起来像

<?php
class NAMESPACE_YOURMODULE_Block_View extends Mage_Catalog_Block_Product_Abstract
{
    protected $_brand;
    protected $_defaultToolbarBlock = 'catalog/product_list_toolbar';
    protected $_brandCollection;
    public function getBrand()
    {
        $brandId = $this->getRequest()->getParam('brand_id', false);
        if (is_null($this->_brand)) {
            if ($brandId) {
                $this->_brand = Mage::getModel('namespace_yourmodule/yourmodule')
                    ->setStoreId(Mage::app()->getStore()->getId())
                    ->load($brandId);
            } else {
                $this->_brand = Mage::getSingleton('namespace_yourmodule/yourmodule');
            }
        }
        return $this->_brand; 
    }
    protected function _getBrandCollection() 
    {
        if (is_null($this->_brandCollection)) {
           $helper = Mage::helper('namespace_yourmodule');
          $brandId = $this->getBrand()->getBrand();
            $this->_brandCollection = Mage::getResourceModel('catalog/product_collection');
            $this->_brandCollection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
            $this->_brandCollection = $this->_addProductAttributesAndPrices($this->_brandCollection)
            ->addStoreFilter()
            ->addAttributeToFilter($helper->getBrandsAttributeCode(), $brandId);        
        }
        return $this->_brandCollection;
    }
    public function getBrandCollection()
    {
        return $this->_getBrandCollection();
    }
    protected function _prepareLayout()
    {
        $brand = $this->getBrand();
        $helper = Mage::helper('namespace_yourmodule');
        // create breadcrumbs for page
        if ($breadcrumbs = $this->getLayout()->getBlock('breadcrumbs')) {
                $breadcrumbs->addCrumb('home', array('label'=>$helper->__('Home'), 'title'=>$helper->__('Go to Home Page'), 'link'=>Mage::getBaseUrl()));
                $breadcrumbs->addCrumb('brands_list', array('label'=>$helper->__('Brands'), 'title'=>$helper->__('Brands'), 'link'=>Mage::getUrl('brands')));
                $breadcrumbs->addCrumb('brands_view', array('label'=>Mage::getModel('namespace_yourmodule/yourmodule')->getBrandName($brand->getBrand(), Mage::app()->getStore()->getId()), 'title'=>$brand->getIdentifier()));
        }

        return parent::_prepareLayout();
    }

    public function getMode()
    {
        return $this->getChild('toolbar')->getCurrentMode();
    }
    protected function _beforeToHtml()
    {  
        $toolbar = $this->getToolbarBlock();
        $collection = $this->_getBrandCollection();
        if ($orders = $this->getAvailableOrders()) {
            $toolbar->setAvailableOrders($orders);
        }
        if ($sort = $this->getSortBy()) {
            $toolbar->setDefaultOrder($sort);
        }
        if ($dir = $this->getDefaultDirection()) {
            $toolbar->setDefaultDirection($dir);
        }
        if ($modes = $this->getModes()) {
            $toolbar->setModes($modes);
        } 
        $toolbar->setCollection($collection);
        $this->setChild('toolbar', $toolbar);
        Mage::dispatchEvent('catalog_block_product_list_collection', array(
            'collection' => $this->_getBrandCollection()
        )); 
        $this->setProductCollection($collection);
        return parent::_beforeToHtml();
    }
    public function getToolbarBlock()
    {   
        if ($blockName = $this->getToolbarBlockName()) {
            if ($block = $this->getLayout()->getBlock($blockName)) {
                return $block;
            }
        }
        $block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, microtime());
        return $block;
    }
    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }
    public function setCollection($collection)
    {
        $this->_brandCollection = $collection;
        return $this;
    }
}

自定义模块的布局 XML 如下所示

   <brand_index_view translate="label">
           <reference name="root">
                <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
                <action method="setLayoutCode"><name>two_columns_left</name></action>
            </reference>
            <reference name="content">
                <block type="namespace_brand/view" name="brand_info" template="brand/view.phtml" >
                    <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                        <block type="page/html_pager" name="product_list_toolbar_pager"/>
                        <!-- The following code shows how to set your own pager increments -->
                        <!--
                            <action method="setDefaultListPerPage"><limit>4</limit></action>
                            <action method="setDefaultGridPerPage"><limit>9</limit></action>
                            <action method="addPagerLimit"><mode>list</mode><limit>2</limit></action>
                            <action method="addPagerLimit"><mode>list</mode><limit>4</limit></action>
                            <action method="addPagerLimit"><mode>list</mode><limit>6</limit></action>
                            <action method="addPagerLimit"><mode>list</mode><limit>8</limit></action>
                            <action method="addPagerLimit" translate="label"><mode>list</mode><limit>all</limit><label>All</label></action>
                        -->
                    </block>
                        <action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
                        <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
                        <action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
                        <action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
                        <action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
                        <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
                </block>
            </reference>
        </brand_index_view>

和你的 view.phtml 文件看起来像

<?php echo $this->getToolbarHtml() ?>
<div class="category-products">
    <?php if (($_products = $this->getProductCollection()) && $_collectionSize = $_products->getSize()): ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
        <?php foreach ($_products->getItems() as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($_products->getItems()) ): ?> last<?php endif; ?>">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
            <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(94, 120) ?>" width="94" height="120" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
            </a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h2>
                     <?php if($_product->getRatingSummary()): ?>
                        <?php echo $this->getReviewsSummaryHtml($_product) ?>
                     <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="desc std">
                    <?php echo  $_product->getShortDescription() ?>
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                </div>
                        <?php if($_product->isSaleable()): ?>
                            <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span>Add To Cart</span></span></button>
                        <?php else: ?>
                            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                        <?php endif; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-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"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </div>
        </li>
        <?php endforeach; ?>
    </ol>
    <?php else: ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i = 0; foreach ($_products->getItems() as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>
    <ul class="products-grid first odd">
    <?php endif ?>
        <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
            <img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(94, 120) ?>" width="94" height="120" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
            </a>
            <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h2>
            <?php if($_product->getRatingSummary()): ?>
            <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
            <?php endif; ?>
            <?php echo $this->getPriceHtml($_product, true) ?>
            <div class="actions">
                <?php if($_product->isSaleable()): ?>
                    <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span>Add To Cart</span></span></button>
                <?php else: ?>
                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                <?php endif; ?>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-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"><?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
            </div>
        </li>
    <?php if ($i%$_columnCount==0 || $i==count($_products->getItems())): ?>
    </ul>
    <?php endif ?>
    <?php endforeach ?>
<?php endif; ?>
<?php endif;?>
</div>
<?php echo $this->getToolbarHtml() ?>

我知道这是一个非常漫长的过程,但只要尝试一下,我相信你一定会完成这个。另外,请使用自定义模块和类名检查变量名称。

希望这肯定会对您有所帮助。