OpenCart theme development: price as number?

I need to display some products differently depending on their price. I was hoping I could just check the value of the $price variable from the corresponding theme files, but $price contains a string with formatted currency. And since OpenCart supports various currency formats, there is no easy and reliable way to convert price strings to numbers.

I looked at the product controller class, ControllerProductProduct . As far as I can tell, OpenCart does not display numerical price values ​​for views. I could change the controller class, but I would prefer not because it would complicate the updates.

Did I miss something? Is there an easy way to do numerical price comparisons within an OpenCart theme?

+4
source share
2 answers

Unfortunately, the answer is no, OpenCart does not provide numerical price values ​​for topics. You will have to modify the core files that Brad explains how to do this .

0
source

Looking at v1.4.9.4 in product.php ( ControllerProductProduct ), I see the following code that sets the formatted value of the $ price you are 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 next ...

 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, you can pass this value to $ price_num to the template by adding the following:

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

Do what you need

+7
source

Source: https://habr.com/ru/post/1335138/


All Articles