Post by Johnathan » Wed Nov 27, 2013 1:18 am

In a default OpenCart installation, a product's "Special" price is calculated after the product's "Discount" price. This means if you have a product that is on special for $90.00, which has a discount of $80.00 when purchasing 5 or more, then a customer purchasing 5 items will be charged $90.00 x 5 = $450.00, instead of $80.00 x 5 = $400.00.

Some stores may want this behavior; however, if you'd rather have the "Discount" price be calculated after the "Special" price, you just need to switch the order in which the cart library calculates the specials and discounts. To do so, make the following change to your installation:

IN:

Code: Select all

/system/library/cart.php
REPLACE:

Code: Select all

// Product Discounts
$discount_quantity = 0;

foreach ($this->session->data['cart'] as $key_2 => $quantity_2) {
    $product_2 = explode(':', $key_2);
    
    if ($product_2[0] == $product_id) {
        $discount_quantity += $quantity_2;
    }
}

$product_discount_query = $this->db->query("SELECT price FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "' AND quantity <= '" . (int)$discount_quantity . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY quantity DESC, priority ASC, price ASC LIMIT 1");

if ($product_discount_query->num_rows) {
    $price = $product_discount_query->row['price'];
}

// Product Specials
$product_special_query = $this->db->query("SELECT price 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 ($product_special_query->num_rows) {
    $price = $product_special_query->row['price'];
} 
WITH:

Code: Select all

// Product Specials
$product_special_query = $this->db->query("SELECT price 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 ($product_special_query->num_rows) {
    $price = $product_special_query->row['price'];
}                        

// Product Discounts
$discount_quantity = 0;

foreach ($this->session->data['cart'] as $key_2 => $quantity_2) {
    $product_2 = explode(':', $key_2);
    
    if ($product_2[0] == $product_id) {
        $discount_quantity += $quantity_2;
    }
}

$product_discount_query = $this->db->query("SELECT price FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "' AND quantity <= '" . (int)$discount_quantity . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY quantity DESC, priority ASC, price ASC LIMIT 1");

if ($product_discount_query->num_rows) {
    $price = $product_discount_query->row['price'];
} 
This code is based on 1.5.6, but switching those two blocks of code should work in any 1.5.x version.
1.4.x versions already prioritize the "Discount" over the "Special" price, so this edit is not needed.

Image Image Image Image Image


User avatar
Administrator

Posts

Joined
Fri Dec 18, 2009 3:08 am


Post by mirek » Thu May 18, 2023 3:10 pm

Thank you for this.

If anybody is looking to prioritize the lowest price from both "Discounts" and "Specials", just add this code after the "Discounts" and "Specials" sections.

Code: Select all

// Get the lowest price from discounts and specials
if ($product_discount_query->num_rows && $product_special_query->num_rows) {
	$price = min($product_discount_query->row['price'], $product_special_query->row['price']);
}

Newbie

Posts

Joined
Fri May 25, 2012 3:37 pm
Who is online

Users browsing this forum: No registered users and 8 guests