【问题标题】:magento set globally available variable - optimal method to work in separate filesmagento 设置全局可用变量 - 在单独文件中工作的最佳方法
【发布时间】:2015-09-30 11:00:07
【问题描述】:

在结帐/购物车中的物品清单中,我声明了以下内容:

Mage::helper('checkout/cart')->group_rate_shipping_info=$group_rate['shipping_info'];

在另一个.phtml 文件中使用。这是在../checkout/cart.phtml中完成的@

然后该文件调用 this->getChildHtml('totals'),后者又调用两个(+更多).phtml 文件:

  1. /frontend/base/default/template/tax/checkout/subtotal.phtml - 它在这里工作,值已设置并打印
  2. /frontend/mycompany/default/template/tax/checkout/grandtotal.phtml - 在这里不起作用。

我有几个问题:

  1. 显然首先,为什么不呢?我怀疑在 mycompany 文件中声明 Mage::helper(..) 的路径有所不同,但我不知道
  2. 我可以将属性附加到哪个工作类以在两个 phtml 文件中工作?
  3. 我应该改用 Mage:getSingleton(..) 吗?
  4. 有更好的“Magento 方式”吗?

显然我想避免使用全局变量... 谢谢

【问题讨论】:

  • 可能在有效和无效的模板之间存在不同的块层次结构。要在它们之间共享,您需要以某种方式存储它,无论您是使用 Mage::register()registry()、会话还是其他方式都取决于您
  • 不确定这是否是您要执行的操作:stackoverflow.com/questions/3340982/…

标签: oop magento global


【解决方案1】:

这里有 Magento 注册表:

Mage::register('group_rate_shipping_info', $group_rate['shipping_info']);

确保在显示总数之前先注册。 Magento 注册表是一种仅针对当前请求的会话。通常在控制器中使用注册表来注册对象或变量,以便在调度周期中稍后访问。例如,在目录控制器中,注册表用于存储当前产品或类别以供以后在块或模板之一中访问:

//in controller:
Mage::register('current_product', $product);
...
//later on in block:
$_product = Mage::registry('current_product');

注意:

Mage::helper('checkout/cart')->group_rate_shipping_info = $group_rate['shipping_info'];

这不起作用,因为大多数 Magento 助手类没有公共属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 2017-04-06
    • 2021-09-24
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2010-10-01
    相关资源
    最近更新 更多