Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top