如何在Magento首页中创建自己的自定义块

自定义块将分类信息显示在首页。

效果图

步骤1:全局模块配置

 app / etc / modules /  目录中创建自定义块配置xml文件。我将其命名为  Lollicupstore_Example.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Lollicupstore_Example>
            <active>true</active>
            <codePool>local</codePool>
        </Lollicupstore_Example>
    </modules>
</config>

步骤2:创建目录

为您的模块创建正确的目录:

app/code/local/Lollicupstore
app/code/local/Lollicupstore/Example
app/code/local/Lollicupstore/Example/Block
app/code/local/Lollicupstore/Example/etc

步骤3:模块配置

添加您的模块配置文件。这应该命名为  config.xml,  并放置在  app / code / local / Lollicupstore / Example / etc内

<?xml version="1.0"?>
<config>
    <modules>
        <Lollicupstore_Example>
            <version>0.1.0</version>
        </Lollicupstore_Example>
    </modules>
    <global>
        <blocks>
            <lollicupstoreexample>
                <class>Lollicupstore_Example_Block</class>
            </lollicupstoreexample>
        </blocks>
    </global>
</config>

步骤4:创建块类

现在,您可以创建您的块类。在此示例中,我们将其简称为  Menu.php  并将其放置在  app / code / local / Lollicupstore / Example / Block内

<?php 
class Lollicupstore_Example_Block_Menu extends Mage_Core_Block_Template 
{    
    
}

menu.php类文件中写出商品分类信息的方法。


<?php class Lollicupstore_Example_Block_Menu extends Mage_Core_Block_Template
{
    public function getProductListHtml()
    {
        return $this->getChildHtml('product_list');
    }
    /**
    * Retrieve current category model object
    *
    * @return Mage_Catalog_Model_Category
    */
    public function getCurrentCategory()
    {
        if (!$this->hasData('current_category')) {
            $category = Mage::getModel('catalog/category')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load('3');
            $this->setData('current_category', $category);
        }
        return $this->getData('current_category');
    }
    public function getCmsBlockHtml()
    {
        if (!$this->getData('cms_block_html')) {
            $html = $this->getLayout()->createBlock('cms/block')
            ->setBlockId($this->getCurrentCategory()->getLandingPage())
            ->toHtml();
            $this->setData('cms_block_html', $html);
        }
        return $this->getData('cms_block_html');
    }

    /**
    * Check if category display mode is "Products Only"
    * @return bool
    */
    public function isProductMode()
    {
         return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_PRODUCT;
    }

    /**
    * Check if category display mode is "Static Block and Products"
    * @return bool
    */
    public function isMixedMode()
    {
        return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_MIXED;
    }

    /**
    * Check if category display mode is "Static Block Only"
    * For anchor category with applied filter Static Block Only mode not allowed
    *
    * @return bool
    */
    public function isContentMode()
    {
        $category = $this->getCurrentCategory();
        $res = false;
        if ($category->getDisplayMode()==Mage_Catalog_Model_Category::DM_PAGE) {
         $res = true;
         if ($category->getIsAnchor()) {
             $state = Mage::getSingleton('catalog/layer')->getState();
            if ($state && $state->getFilters()) {
                $res = false;
            } 
         }
       }
    return $res;
    }
}
?>

步骤5:创建视图脚本

创建一个视图脚本模板文件以用于您的块。我只是将其放置为  /app/design/frontend/lollicupstore/default/template/example/ 文件夹中的menu.phtml文件 。

menu.phtml文件中我们调用名为Menu 的Block的方法。

<?php if($this->isContentMode()): ?>
    <?php echo $this->getCmsBlockHtml() ?>

<?php elseif($this->isMixedMode()): ?>
    <?php echo $this->getCmsBlockHtml() ?>
    <?php echo $this->getProductListHtml() ?>

<?php else: ?>
    <?php echo $this->getProductListHtml() ?>
<?php endif; ?>

步骤6:将代码块嵌入您的layout.xml或以编程方式。

我们以编程的方式,将代码块放到main.phtml文件中

echo $this->getLayout()->createBlock('lollicupstoreexample/menu')->setTemplate('example/menu.phtml')->toHtml();

总结:

注意这段代码 createBlock(‘lollicupstoreexample/menu’) 的
menu 一定要写设置新建的Block,并且这个配置“
lollicupstoreexample ” 名称一定要与config.xmll中配置的名字一致,不然不会生效。

名称可以自己取,但是在使用的时候,要保持一致。比如:

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用*标注