OpenCart 主题开发:价格是多少?

发布于 2024-10-11 15:27:51 字数 321 浏览 4 评论 0原文

我需要根据某些产品的价格以不同的方式展示它们。我希望可以简单地从相关主题文件中检查 $price 变量的值,但 $price 包含货币格式的字符串。由于 OpenCart 支持多种货币格式,因此没有简单、可靠的方法将价格字符串转换回数字。

我查看了产品控制器类 ControllerProductProduct。据我所知,OpenCart 不会向视图公开数字价格值。我可以修改控制器类,但我不想这样做,因为这会使更新变得复杂。

我是不是忽略了什么?是否没有简单的方法可以在 OpenCart 主题内对价格进行数字比较?

I need to display some products differently depending on their price. I hoped that I could simply check the value of the $price variable from within the relevant theme file(s), but $price contains a currency formatted string. And because OpenCart supports a variety of currency formats, there's no simple, robust way of converting price strings back into numbers.

I've looked in the product controller class, ControllerProductProduct. So far as I can tell, OpenCart does not expose a numeric price value to views. I could modify the controller class, but I'd rather not because it would complicate updates.

Have I overlooked something? Is there no easy way to perform a numeric comparison on a price from within an OpenCart theme?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

番薯 2024-10-18 15:27:51

查看product.php (ControllerProductProduct) 中的 v1.4.9.4,我可以看到以下代码设置您正在讨论的 $price 的格式化值:

if ($discount) {
    $price = $this->currency->format($this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax')));
} else {
    $price = $this->currency->format($this->tax->calculate($result['price'],$result['tax_class_id'], $this->config->get('config_tax')));

为什么不将其更改为如下...

if ($discount) {
    $price_num = $this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax'));
    $price = $this->currency->format($price_num);
} else {
    $price_num = $this->tax->calculate($result['price'],$result['tax_class_id'], $this->config->get('config_tax'));
    $price = $this->currency->format($price_num);

然后再往下几行,您可以通过添加以下内容将此 $price_num 值传递给模板:

$this->data['products'][] = array(
    'product_id'    => $result['product_id'],
    ...
    'price'         => $price,
    'price_num'     => $price_num,
    ...

应该执行您需要的操作

Looking at v1.4.9.4 in product.php (ControllerProductProduct) I can see the following code that sets the formatted value of $price that you're talking about:

if ($discount) {
    $price = $this->currency->format($this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax')));
} else {
    $price = $this->currency->format($this->tax->calculate($result['price'],$result['tax_class_id'], $this->config->get('config_tax')));

Why don't you change this to be the following...

if ($discount) {
    $price_num = $this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax'));
    $price = $this->currency->format($price_num);
} else {
    $price_num = $this->tax->calculate($result['price'],$result['tax_class_id'], $this->config->get('config_tax'));
    $price = $this->currency->format($price_num);

And then a few lines down from this, you can then pass on this $price_num value to the template by adding the following:

$this->data['products'][] = array(
    'product_id'    => $result['product_id'],
    ...
    'price'         => $price,
    'price_num'     => $price_num,
    ...

Should do what you need

守望孤独 2024-10-18 15:27:51

不幸的是,答案是否定的,OpenCart 不会向主题公开数字价格值。您必须修改核心文件,Brad 解释了如何操作。

Unfortunately the answer is no, OpenCart does not expose numeric price values to themes. You will have to modify core files, which Brad explains how to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文