代码之家  ›  专栏  ›  技术社区  ›  Martijn Heemels

在Magento中设置全局变量,GUI方式?

  •  9
  • Martijn Heemels  · 技术社区  · 16 年前

    我最近开始将Magento用于一个客户的网店,但仍然需要掌握它的系统。

    webshop应该有几个链接,也可以从公司网站所在的另一个域获取信息。我不希望硬编码域名或URL,而是在某个地方定义它,并在整个webshop的phtml模板中使用该值。这使得我们在开发人员、登台人员和生产URL之间移动站点时可以轻松地进行调整。

    有人能推荐Magento的方法吗?我们最好能在后端的商店配置GUI中添加一个字段,类似于{{base_url}}的设置方式。或者我想的不对?

    5 回复  |  直到 16 年前
        1
  •  36
  •   Alana Storm    14 年前

    Magento为自定义配置值提供(相对)简单的支持。我发现实现这一点的最佳方法是创建一个单独的magento模块,该模块保存所有自定义配置值。

    像任何Magento一样,有很多步骤,任何一个错误都可能绊倒你(或我!)。

    首先,您需要设置一个magento模块来保存所有自定义配置值。创建magento模块需要:

    1. 在app/etc/modules中创建xml文件
    2. 在app/code/local/Companyname中创建文件夹结构

    Companyname是用作名称空间的唯一字符串,大多数magento教程建议您在此处使用公司名称。在本教程中,我将使用“Stackoverflow”。无论您在何处看到字符串Stackoverflow,请使用您自己的唯一字符串替换它。

    因此,对于步骤1,在app/etc/modules中创建一个名为“Stackoverflow_Customconfig.xml”的文件,并将以下内容放入

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

    随机MaGeto Tip:MaGeto系统中有部分考虑了空白区的重要性,因此最好不要用属性值包含空白(L& Active & Gt;Trand & Lt//Active & Gt;Vs. & Lt;Active & Gt;Trand & Lt/ /Active & Gt;

    接下来,创建以下文件夹

    mkdir app/code/local/Stackoverflow/Customconfig
    mkdir app/code/local/Stackoverflow/Customconfig/etc
    

    并在以下位置创建一个文件:

    app/code/local/Stackoverflow/Customconfig/etc/config.xml
    

    包括以下内容

    <?xml version="1.0"?>
    <config>
        <modules>
            <Stackoverflow_Customconfig>
                <version>0.1.0</version>
            </Stackoverflow_Customconfig>
        </modules>
    </config>
    

    恭喜,您刚刚安装了一个新的Magento模块。如果清除magento缓存并转到

    System -> Configuration -> Advanced -> Disable Modules Output
    

    您应该会看到列出的模块。

    将System.xml文件添加到模块中

    接下来,我们将添加一个system.xml文件。此文件用于向magento添加自定义配置值,您可以在magento请求周期中获取该值。在以下位置添加文件:

    app/code/local/Stackoverflow/Customconfig/etc/system.xml
    

    它包含以下内容

    <config>
        <sections>
            <design>
                <groups>
                    <my_or_their_group translate="label">
                        <label>A grouping of config values.  Make your own, or us an existing group.</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>50</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>0</show_in_website>
                        <show_in_store>0</show_in_store>
                        <fields>
                            <my_config translate="label">
                                <label>This will be my config's label</label>
                                <frontend_type>text</frontend_type>
                                <sort_order>50</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>0</show_in_website>
                                <show_in_store>0</show_in_store>
                            </my_config>
                        </fields>
                    </my_or_their_group>
                </groups>
            </design>
        </sections>
    </config>   
    

    <设计>

    app/code/core/Mage/Directory/etc/system.xml
    <currency translate="label" module="directory">
        <label>Currency Setup</label>
    

    所以您要使用标签<货币/<,而不是更直观的<货币设置/>

    <

    您还会注意到 此处,以及相应的标签标签。这允许您在HTML界面中使用想要的任何字符串作为组标题,但在内部将名称保留为有效的XML标记名。我们的标签名为

    <my_or_their_group />
    

    但在界面中,组将具有标题

    最后,<my_config translate=“label”>是yoru conifg值的名称。再次,请注意 翻译属性 . 上述规则同样适用。

    另一个xml结构是必需的,并且(主要)用于控制配置将使用何种HTML输入。如果需要特定的接口元素,请在核心模块中找到一个示例并复制XML结构。

    这将允许您在Magento GUI界面中设置和查找配置值。您可以使用全局Mage对象的静态getStoreConfig方法并指定配置值的URI来检索值。URI是使用配置文件的节/组/名称创建的。

    Mage::getStoreConfig('design/my_or_their_group/my_config');     
    
        2
  •  17
  •   scrowler    10 年前

    登录到管理员端,系统->自定义变量->使用代码“my_variable”创建新的自定义变量。

    输入此变量的HTML内容和纯文本

    您可以在CMS页面中显示自定义变量,方法是 {{customVar code=my_variable}}

    或者 .phtml 页:

    $variableHtml = Mage::getModel('core/variable')->loadByCode('my_variable')->getValue('html');
    $variablePlain = Mage::getModel('core/variable')->loadByCode('my_variable')->getValue('plain');
    
        3
  •  1
  •   Rick J    16 年前

    最简单的方法是在magento的核心配置xml文件中添加一个节点。但不建议这样做,因为这会导致升级问题。要设置自定义值而不编辑核心。。。。查看此链接

    How to override config values

        4
  •  1
  •   Edison Software    14 年前

    我太新了,无法对Alan的回答添加评论,但这里有一些来自Magento的更多信息:

    -埃德。

        5
  •  0
  •   Krista K    12 年前

    艾伦,谢谢你的回答!正是这把钥匙为我解开了谜团。即使在我 reading your excellent guide . 由于我正在尽力不修改核心文件,我已经开始为我的电子商务业务做扩展。我得到了一个我认为足够好的版本,可以发布给人们,但是我想能够在管理员中配置它,这样就不需要编辑文件了。

    我从上面的代码开始,看到添加的“菜单”不是“一般”,而是“一般”,或“一般Web、一般设计”,等等。我希望我的东西一般地显示出来,但我不想像其他人一样,为我的扩展添加整个菜单组。

    如果读者是从谷歌来的,只是想把我的选项放在简单的管理中,那么继续读下去(这就是为什么我要添加另一个答案)。 照艾伦上面说的做。让您的菜单以常规方式显示->一般->你的菜单。注意:您需要清除缓存并注销,因为会话中存储了一些信息。

    为了让您自己的菜单显示在“常规”下,您必须执行相同的操作,就像您获得自己的组,甚至顶部菜单栏的选项卡一样,您必须在中为自己授予ACL权限 config.xml :

    <!-- file: config.xml -->
    <config>
        <adminhtml>
            <acl>
                <resources>
                    <admin>
                        <children>
                            <system>
                                <children>
                                    <config>
                                        <children>
                                            <ytf translate="title">
                                                <title>Youtube Feed</title>
                                            </ytf>
                                        </children>
                                    </config>
                                </children>
                            </system>
                        </children>
                    </admin>
                </resources>
            </acl>
        </adminhtml>
    </config>
    

    system.xml 与配置相对应的。注意 ytf ytf ytfeed ytf 条目是当您转到“管理”时显示的内容->配置并查看常规组中的左侧。 ytfeed 是单击“常规->Youtube提要”时在页面中心打开的“栏”

    <!-- file: system.xml -->
    <config>
        <sections>
            <ytf translate="label" module="ytfeed">
                <label>Youtube Feed</label>
                <tab>general</tab>
                <frontend_type>text</frontend_type>
                <sort_order>700</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>
                <groups>
                    <ytfeed translate="label" module="ytfeed">
                        <label>Youtube Feed</label>
                        <sort_order>50</sort_order>
                        <expanded>1</expanded>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <fields>
                            <username translate="label">
                            <!-- Mage::getStoreConfig('ytf/ytfeed/username');  -->  
                                <label>YouTube Username:</label>
                                <comment>(or YouTube channel name)</comment>
                                <frontend_type>text</frontend_type>
                                <sort_order>10</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>1</show_in_website>
                                <show_in_store>1</show_in_store>
                            </username>
                        </fields>
                    </ytfeed>
                </groups>
            </ytf>
        </sections>
    </config>
    

    另一个对我帮助很大的链接:
    http://www.scorgit.com/blog/custom-options-in-a-magento-back-end-dropdown-menu/

    更新: 我做了 an extension out of this answer .