skip to Main Content

I tried to translate the “new” label in Magento 2 but now it show Http error 500. For example, I changed <span>new</span> to <span data-bind="i18n: 'new'"></span> in the code below. What did I do wrong?

function newLabel($_product) {
  $output='';
  $now = date("Y-m-d");
  $newsFrom = substr($_product->getNewsFromDate(), 0, 10);
  $newsTo = substr($_product->getNewsToDate(), 0, 10);
  $new = false;

  if (!empty($newsFrom) && !empty($newsTo)) {
    if ($now >= $newsFrom && $now <= $newsTo) $new = true;
  } elseif (!empty($newsFrom) && empty($newsTo)) {
    if ($now >= $newsFrom) $new = true;
  } elseif (empty($newsFrom) && !empty($newsTo)) {
    if ($now <= $newsTo) $new = true;
  }
  // I'm trying to change this line:
  if ($new) $output='<div class="product_holder__label product_holder__label--right product_holder__label--new"> <span>new</span> </div>';
  return $output; 
}

In the error_log, I can see this if I try the translation:

PHP Parse error: syntax error, unexpected 'new' (T_NEW) in /home/.../.../app/code/vendor/module/Helper/Data.php

2

Answers


  1. You are not paying attention to proper quoting. As far as I can tell, you are replacing this line:

    if ($new)$output='... <span>new</span> ...';
    

    Directly with this:

    if ($new)$output='... <span data-bind="i18n: 'new'"></span> ...';
    

    This is a problem because the single quotes around new get misinterpreted as the end of the string '... <span data-bind="i18n: ' and the other as the beginning of the string '"></span> ...'.

    You need to escape the single quotes with a backslash:

    if ($new)$output='... <span data-bind="i18n: 'new'"></span> ...';
    
    Login or Signup to reply.
  2. Not the answer to the OP.

    But
    I was redirected here, for the same error search.

    My case:
    The project was working on server1.
    For R&D i uploaded this same code on server2.
    On server2 i faced the above error.

    Reason:
    "Support for ‘NEW’ was added in PHP 7.0"

    So i checked my PHP version and it was lower.
    Check you PHP version with phpinfo();

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search