Davide Tubia Davide Tubia - 3 months ago 5
PHP Question

Add "starting at" if products have options in Opencart category page

I need to show in the category page of opencart

Starting at € xx.xx if this product have options.

Something like this:

if (have options) {
print starting at $price
} else {
print $price }


category.tpl contains:

<p class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
<?php } ?>
<?php if ($product['tax']) { ?>
<span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
<?php } ?>




I need to insert another controller that, if we have options show "starting at €..." instead the price.

Thank you all,
Davide

Answer

I needed something similar for a client in Opencart (v1.5.5.1). The following code checks to see if there is an option with a price increase and if so puts 'From price' on the category page:

Add the following function to \catalog\model\catalog\product.php

public function hasOptionPriceIncrease($product_id) {
  $option_data = $this->getProductOptions($product_id);
  if (is_array($option_data)) {
    foreach ($option_data as $option) {
      if (is_array($option['option_value'])) {
        foreach ($option['option_value'] as $value) {
          if ($value['price'] > 1) {
            return true;
          }
        }  
      }
    }
  }
  return false;
}

in opencart > 2.0 use product_option_value instead of option_value:

public function hasOptionPriceIncrease($product_id) {
  $option_data = $this->getProductOptions($product_id);
    if (is_array($product_option_value)) {
      foreach ($product_option_value as $option) {
      if (is_array($option['product_option_value'])) {
        foreach ($option['product_option_value'] as $value) {
          if ($value['price'] > 1) {
            return true;
          }
        }
      }
    }    
  }
  return false;
}

Then add the following line to the $this->data['products'][] array in the category controller: \controller\product\category.php

      'has_option_price_increase' =>$this->model_catalog_product->hasOptionPriceIncrease($result['product_id'])

This is at around line 250 on mine but yours maybe a bit different it goes in here:

$this->data['products'][] = array(
  'product_id'  => $result['product_id'],
  'thumb'       => $image,
  'name'        => $result['name'],
  'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 100) . '..',
  'price'       => $price,
  'special'     => $special,
  'tax'         => $tax,
  'rating'      => $result['rating'],
  'reviews'     => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
  'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url),
  'has_option_price_increase' =>$this->model_catalog_product->hasOptionPriceIncrease($result['product_id'])
);

Don't forget to add the comma at the end of the preceding line.

Then you can add something similar to the following in the view: \catalog\view\theme\duvalay\template\product\category.tpl

<p class="price">
 <?php if ($product['has_option_price_increase']) { echo 'From'; } ?>