【问题标题】:Magento php error, appears only in ChromeMagento php错误,仅出现在Chrome中
【发布时间】:2013-09-21 15:20:07
【问题描述】:

我只在 Chrome 中收到此错误,在 Firefox 中一切正常:

致命错误:在第 55 行的 /home1/idealtil/public_html/app/design/frontend/default/blank/template/catalog/product/view.phtml 中的非对象上调用成员函数 getName()

这是我的 view.phtml 代码:

<?php

$current_category = Mage::registry('current_category');

if ($current_category->getName() == 'Showroom'): ?> //this is line 55

<div class="product-essential">
<form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
    <div class="no-display">

.................................. .....................................

可能是什么问题?

【问题讨论】:

  • 您只在 Chrome 中收到 PHP 错误?这意味着 Magento 中会有一些特定于浏览器的代码 - 或者你错了。或者有一些 Javascript 只能在 Chrome 中执行...
  • 不要将卡住的浏览器缓存误认为是浏览器之间代码执行的差异。

标签: php magento google-chrome


【解决方案1】:

您的“类别”对象在 Chrome 中可能实际上是 null,即,您是否在 current_category 上调用了 Mage::register

检查打开该页面的 URL 和调用,或验证它不是会话问题(例如,您在 Firefox 会话中有数据,但在 Chrome 会话中没有)。

根据您到达该页面的方式,您可能处于current_category 尚未(还?)由CategoryController::_initCategory() 设置的区域,因为例如该类别不存在于URL 中。有关更多详细信息,请参阅this answer。假设现在该类别已保存在会话中,并且您使用 Firefox 进行了几次页面点击。现在 Firefox 会话确实有一个可用的缓存类别,当您在 Firefox 中遇到麻烦的缓存时,您会看到缓存。然后使用没有可用会话或缓存的 Chrome 访问同一页面(还),这会给您一个错误。

您可能希望使用一些后备代码来包装调用,以回避问题并且不使用类别代码,除非确实有可用的类别:

// Here you might get NULL
$current_category = Mage::registry('current_category');

// If category is **not** NULL, *and* is Showroom, then...
if (($current_category) && ($current_category->getName() == 'Showroom')):

根据你在代码中所处的位置,你可以查看更多获取类别的方法;详情请见this answer

【讨论】:

  • 嗨!抱歉,您所说的“您是否在 current_category 上调用 Mage::register ?”是什么意思?这个:$current_category = Mage::registry('current_category');
  • 什么是会话问题?你能解释一下吗?我不知道如何解决它。
  • current_category 在所有页面中不会自动设置。如果不是,您可以自己设置它(如果确实需要),或者它将为 NULL - 给出您遇到的错误。不过,我会扩展我的答案。
  • 太棒了!非常感谢你这么明确的回答。成功了!!
【解决方案2】:

我认为您在这些浏览器中打开了不同的 URL。一个是打开的,另一个是没有类别的,即

  1. http://example.com/cool-category/coolest-product

  2. http://example.com/coolest-product

在第二个 url 上 $current_category 将为 null 。

也可能是您从搜索结果页面来到产品,并且在 URL 中缺少类别参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2015-10-15
    • 2016-12-25
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多