【问题标题】:Unable to multiply two variables in Smarty on Prestashop无法在 Prestashop 上的 Smarty 中将两个变量相乘
【发布时间】:2020-02-19 21:49:19
【问题描述】:

在我的 prestashop 商店中,我有两个变量 per_sqft_priceper_box_price。 per_sqft_price 之前有货币 ($) 签名。我正在尝试打印这两个变量的乘积。但它总是打印 0。

{assign var = per_sqft_price value = $product.price}
{assign var = per_box_price value = $product.features[0].value}

<meta itemprop="per_sqft_price" content="{$per_sqft_price|replace:'$':''}"/>
{math equation = "x * y" x = $per_sqft_price  y = $per_box_price }

请帮助我如何解决这个问题。在此先感谢

【问题讨论】:

  • 你能不能 var_dump per_sqft_priceper_box_price (在assing 之后)

标签: php prestashop smarty


【解决方案1】:

使用$product.price_amount 代替$product.price,它给出没有货币符号的数字价格输出。并确保$product.features[0].value 也是数字,因为它可能是一个字符串并且可能只包含一个文本。此外,您可以将intval 用于您的变量,例如:

{math equation = "x * y" x = $per_sqft_price|intval  y = $per_box_price|intval }

【讨论】:

  • 它的工作。如何将结果保留到小数点后 2 位?
  • 使用 floatval 而不是 intval 或者如果您需要在句点之后有 2 个数字,则将 number_format:2 添加到结果中
  • {math assign='result' equation = "x * y" x = $per_sqft_price|floatval y = $per_box_price|floatval } {$result|number_format:2}
【解决方案2】:

试试这个代码:

{$result = ($per_sqft_price|intval) * ($per_box_price|intval)}

这不是标准方法,但它可能有效

【讨论】:

  • 我编辑了这个问题。你能重新检查一下吗?实际上数字格式是限制做数学运算的。
  • 用你的方式试过了。数学运算后仍显示空或 0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多