在Magento中设置全局变量,GUI方式? [英] Setting a global variable in Magento, the GUI way?

查看:156
本文介绍了在Magento中设置全局变量,GUI方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Magento作为客户的网上商店,仍然需要掌握其系统。

I've recently started using Magento for a client's webshop, and still need to get to grips with its systems.

网上商店应该有几个链接,从公司网站所在的其他域名获取信息。我不想硬编码域名或URL,而是在某个地方定义它,并在整个网上商店的phtml模板中使用该值。这使得当我们在开发,分段和生产URL之间移动网站时,很容易调整它。

The webshop should have several links to and also grab info from another domain, where the corporate website is located. I would prefer not to hardcode the domain name or URL but instead define it at some place and use that value in the phtml templates throughout the webshop. This makes it easy to adjust it when we move the site between dev, staging and production URL's.

任何人都可以建议Magento这样做?优选地,我们可以向后端中的Store的配置GUI添加字段,类似于设置{{base_url}}的方式。

Can anyone suggest the Magento way of doing this? Preferably we could add a field to the Store's Config GUI in the backend, similar to the way the {{base_url}} is set. Or maybe I'm thinking the wrong way?

推荐答案

Magento提供了相对容易的自定义配置值支持。我找到的最好的方法是创建一个包含所有自定义配置值的magento模块。

Magento offers (relatively) easy support for custom configuration values. The best way I've found to accomplish this is to create a single magento module that holds all your custom configuration values.

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

Like anything Magento, there are a lot of steps, and any one being wrong could trip you (or me!) up.

首先,您需要设置一个magento模块,自定义配置值。创建magento模块涉及

First, you'll want to setup a magento module to hold all your custom configuration values. Creating a magento module involves


  1. 在应用程序中创建一个xml文件/ etc / modules

  2. 创建一个app / code / local / Companyname中的文件夹结构

公司名是一个用作命名空间的唯一字符串,大多数magento教程在这里使用您的公司名称。为了本教程的目的,我将使用Stackoverflow。

Companyname is a unique string that serves as a namespace, and most magento tutorials recommend you use your company name here. For the purposes of this tutorial, I'll use "Stackoverflow". Wherever you see the string Stackoverflow, replace it with your own unique string.

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

So, for step 1, create a file at app/etc/modules named "Stackoverflow_Customconfig.xml", and place the following inside

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

随机Magento提示:magento系统的一些部分认为空格有意义,所以最好不包含具有属性值的空格(< active> true< / active>与< active> true< / active>

Random Magento Tip: There are parts of the magento system that consider whitespace significant, so it's always best to include no whitespace with your attribute values (<active>true</active> vs. <active> true </active>

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 Module。如果你清除你的magento缓存,并转到

Congratulations, you've just setup a new Magento Module. If you clear your magento cache and go to

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

您应该会看到您的模块。

you should see your module listed.

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

Next, we're going to add a system.xml file. This file is used to add a custom configuration value to magento, which you'll be able to grab anywhere you want during the magento request cycle. Add a file at

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>   

< design> 是您的配置部分的名称显示。通用,Web,设计,货币设置等大体上,这将是标题的小写版本,即一般变为一般,设计变为设计。如果你不确定这个外部标签应该是什么,搜索核心magento模块。即,货币设置的greping显示在

<design> is the name of the section your config will be displayed in. "General, Web, Design, Currency Setup, etc." By and large, this will be a lower-case version of the title, i.e. "General" becomes "general", "Design" becomes "design". If you're not sure what this outer tag should be, search through the core magento modules. i.e., grepping for "Currency Setup" reveals a mention in

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

因此,您可以使用标记< currency /<,而不是更直观的< currency_setup />

So you'd use the tag <currency /<, and not the more intuitive <currency_setup />

< my_or_their_group translate =label>是您的配​​置变量将显示在其中的组的名称。组是包含配置字段的Ajax下拉列表。例如,常规部分有国家/地区选项组和本地选项组。如果您不确定如何在现有组中添加值,请再次检查现有的核心模块。

<my_or_their_group translate="label"> is the name of the group your config variable will show up in. Groups are the Ajax drop downs that contain config fields. For example, the General section has a "Country options" group and a Local options" group. Again, check existing core modules if you're unsure how to place a value in an existing group.

您会在这里看到一个 translate属性以及相应的标签标签,这样您可以在HTML界面中使用任何您想要的字符串作为群组标题,我们的标签命名为

You'll also notice a translate attribute here, along with a corresponding label tag. This allows you to use any string you want in the HTML interface as a group title, but internally keep the name a valid XML tag name. Our tag is named

<my_or_their_group />

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

but in the interface, the group will have the title


一组配置值。创建自己的或者我们现有的组。

A grouping of config values. Make your own, or us an existing group.

最后,< my_config translate =label>是yoru conifg值的名称,请再次注意 translate属性,与上述规则相同。

Finally, <my_config translate="label"> is the name of yoru conifg value. Again, notice the translate attribute. The same rules as above apply.

其他的xml结构是需要的,并且(主要)用于控制什么样的HTML输入将用于您的配置。如果你想要一个特定的接口元素,在核心模块中找到一个例子并复制XML结构。

The other xml structure is needed, and is (mostly) used to control what kind of HTML inputs will be used for your config. If you want a particular interface element, find an example in the core module and copy the XML structure.

这将允许你设置和查找配置值在Magento GUI接口。您可以使用全局Mage对象的静态getStoreConfig方法检索您的值,并指定配置值的URI。使用配置的部分/组/名称创建URI。

This will allow you to set and lookup config values in the Magento GUI interface. You can retrieve your values using the static getStoreConfig method of the global Mage object and specifying the URI of your config value. The URI is created using the section/group/name of your config.

Mage::getStoreConfig('design/my_or_their_group/my_config');     

这篇关于在Magento中设置全局变量,GUI方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆