Eric
Eric

Reputation: 9

How to check if products have a special price in Opencart

I want to disable coupons for sale products in Opencart.

I found coupon code and here is a list of "if-s" when the coupon is not working... I have to add here the condition that when the product is on discount, $status = false;, but I don't know how to check if the products have a special price.

if ($coupon_query->row['total'] > $this->cart->getSubTotal()) {
    $status = false;
}
$coupon_history_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "coupon_history` ch WHERE ch.coupon_id = '" . (int)$coupon_query->row['coupon_id'] . "'");

if ($coupon_query->row['uses_total'] > 0 && ($coupon_history_query->row['total'] >= $coupon_query->row['uses_total'])) {
    $status = false;
}

if ($coupon_query->row['logged'] && !$this->customer->getId()) {
    $status = false;
}

Upvotes: 0

Views: 1639

Answers (3)

Charles Chang
Charles Chang

Reputation: 11

if you look for foreach ($this->cart->getProducts() as $product) { , you nedd to add the same codes above after if (in_array($product['product_id'], $coupon_info['product'])) { again.

Upvotes: 0

Freshman2578
Freshman2578

Reputation: 1

Thanks for the code, it works pretty well, but when I've add this I noticed another bug that appeared. When I add products that are not on sale and products that are on sale, the discount value is not correct. The type of coupon value is set to fixed price. Example : Discount coupon - 20USD Product 1 - 10USD - On Sale Product 2 - 20USD - On Sale Product 3 - 100USD - Not on sale

Discount coupon value appear in checkout like : -17,65 or something. This is the check that I made in coupon.php

if ($status) {
  if ($coupon_info['type'] == 'F') {
    $discount = $coupon_info['discount'] * ($product['total'] / $sub_total);
  } elseif ($coupon_info['type'] == 'P') {
    $discount = $product['total'] / 100 * $coupon_info['discount'];
}

Upvotes: 0

Mehravish Temkar
Mehravish Temkar

Reputation: 4365

This is for version 2.3.0.2

In your file catalog/model/extension/total/coupon.php look for the function public function getTotal($total) :

next to the line

$this->load->language('extension/total/coupon');

Add

$this->load->model('catalog/product');

Next search $discount = 0; in same function. Before $discount = 0; add

$product_details = $this->model_catalog_product->getProduct($product['product_id']);
if($product_details['special']) {
    continue;
}

This will skip applying discount to products that have special price and will apply for other products in the cart.

Upvotes: 4

Related Questions