Radu Cristian
Radu Cristian

Reputation: 79

display special price's end date to my product page in opencart

Is there any way I could display the special price's end date on my product page in opencart 1.5.5.1 if an end date is set?

I added this to my catalog/controller/product/product.php:

$special_info = $this->db->query("SELECT date_end FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "'");
         if ($special_info->num_rows) {
            $date_end = $special_info->row['date_end'];   
              $this->data['date_end'] = date($this->language->get('date_format_short'), strtotime($date_end));
         }else{
               $this->data['date_end'] = '';
         }

and to my catalog/view/theme/default/template/product/product.tpl this:

Special Ends: <?php echo $date_end; ?>

but it doesn't seem to work very well. I see the date if I set a date to a special product, but if I don't, it still shows this: 30.11.-0001

How can I make it display nothing if an end date is not set?

Upvotes: 0

Views: 1956

Answers (1)

Jay Gilford
Jay Gilford

Reputation: 15151

The problem is you are just getting a special for a product, regardless of if it's actually in date or for that customer group. The full query used to get a special is in /catalog/model/catalog/product.php

(SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$customer_group_id . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special

So you need to add that to your query as well and supply the customer group id. You also need to check that the date isn't 0000-00-00 so your full code should look as follows

$this->data['date_end'] = '';

$customer_group_id = $this->customer->isLogged() ? $this->customer->getCustomerGroupId() : $this->config->get('config_customer_group_id');
$special_info = $this->db->query("SELECT date_end FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "' AND customer_group_id ='" . (int) $customer_group_id . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY priority ASC, price ASC LIMIT 1");

if ($special_info->num_rows && $special_info->row['date_end'] != '0000-00-00') {
    $this->data['date_end'] = date($this->language->get('date_format_short'), strtotime($special_info->row['date_end']));
}

Upvotes: 1

Related Questions