2

magento バックエンドを介して magento の静的ブロックからカテゴリ名と画像を参照するにはどうすればよいですか? 私は1.7を実行しています。

4

2 に答える 2

3

静的ブロック内からこれらの値を簡単に参照できる方法を知りません。

代わりに、これを実現するためのよりクリーンで拡張可能な方法を提供するウィジェット (私の意見では、Magento の最も活用されていない機能の 1 つ) を作成して使用することをお勧めします。

静的ブロックから要求したことを正確に実行する Magento ウィジェットの完全な (簡略化された) 例については、以下のコードを参照してください。

app/etc/modules/YourCompany_Categorywidget.xml

<config>
    <modules>
        <MyCompany_Categorywidget>
            <active>true</active>
            <codePool>community</codePool>
        </MyCompany_Categorywidget>
    </modules>
</config>

アプリ/コード/コミュニティ/MyCompany/Categorywidget/etc/config.xml

<?xml version="1.0"?>

<config>
    <modules>
        <MyCompany_Categorywidget>
            <version>1.0.0</version>
        </MyCompany_Categorywidget>
    </modules>
    <global>
        <blocks>
            <categorywidget>
                <class>MyCompany_Categorywidget_Block</class>
            </categorywidget>
        </blocks>
        <helpers>
            <categorywidget>
                <class>MyCompany_Categorywidget_Helper</class>
            </categorywidget>
        </helpers>
    </global>
</config>

アプリ/コード/コミュニティ/MyCompany/Categorywidget/etc/widget.xml

<?xml version="1.0"?>

<widgets>
    <category_widget type="categorywidget/catalog_category_widget_info" translate="name description" module="categorywidget">
        <name>Category Info Block</name>
        <description>Category Info Block showing name, image etc</description>
        <parameters>
            <block_title translate="label">
                <required>1</required>
                <visible>1</visible>
                <label>Block Title</label>
                <type>text</type>
            </block_title>
            <template>
                <required>1</required>
                <visible>1</visible>
                <label>Template</label>
                <type>select</type>
                <value>categorywidget/info.phtml</value>
                <values>
                    <default translate="label">
                        <value>categorywidget/info.phtml</value>
                        <label>Category Widget Info Block - Default Template</label>
                    </default>
                    <!-- Add different temmplates here for different block positions -->
                </values>
            </template>
            <category translate="label">
                <visible>1</visible>
                <required>1</required>
                <label>Category</label>
                <type>label</type>
                <helper_block>
                    <type>adminhtml/catalog_category_widget_chooser</type>
                    <data>
                        <button translate="open">
                            <open>Select Category...</open>
                        </button>
                    </data>
                </helper_block>
                <sort_order>10</sort_order>
            </category>
        </parameters>
    </category_widget>
</widgets>

アプリ/コード/コミュニティ/MyCompany/Categorywidget/Helper/Data.php

<?php

class MyCompany_Categorywidget_Helper_Data extends Mage_Core_Helper_Abstract
{}

アプリ/コード/コミュニティ/マイカンパニー/カテゴリーウィジェット/ブロック/カタログ/カテゴリー/ウィジェット/Info.php

<?php

class MyCompany_Categorywidget_Block_Catalog_Category_Widget_Info
    extends MyCompany_Categorywidget_Block_Catalog_Category_Info
        implements Mage_Widget_Block_Interface
{
    protected function _prepareCategory()
    {
        $this->_validateCategory();

        $category = $this->_getData('category');
        if (false !== strpos($category, '/')) {
            $category = explode('/', $category);
            $this->setData('category', (int)end($category));
        }
        return parent::_prepareCategory();
    }
}

アプリ/コード/コミュニティ/マイカンパニー/カテゴリーウィジェット/ブロック/カタログ/カテゴリー/Info.php

<?php

class MyCompany_Categorywidget_Block_Catalog_Category_Info extends Mage_Core_Block_Template
{
    protected $_category;

    protected function _beforeToHtml()
    {
        $this->_category = $this->_prepareCategory();
        return parent::_beforeToHtml();
    }

    protected function _prepareCategory()
    {
        $this->_validateCategory();
        return Mage::getModel('catalog/category')->load($this->_getData('category'));
    }

    protected function _validateCategory()
    {
        if (! $this->hasData('category')) {
            throw new Exception('Category must be set for info block');
        }
    }

    public function getCategoryName()
    {
        return $this->_category->getName();
    }

    public function getCategoryImage()
    {
        return $this->_category->getImageUrl();
    }
}

アプリ/デザイン/フロントエンド/ベース/デフォルト/テンプレート/カテゴリウィジェット/info.phtml

<?php
    $_categoryName = $this->getCategoryName();
    $_categoryImage = $this->getCategoryImage();
 ?>

 <div class="categoryinfo_block block">
    <p><strong><?php echo $_categoryName ?></strong></p>
    <img src="<?php echo $_categoryImage ?>" alt="<?php echo $_categoryName  ?>" />
 </div>
于 2012-06-05T08:34:38.693 に答える