Skip to content

Commit

Permalink
https://github.com/opencart/opencart/issues/11458
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkerr committed Dec 3, 2024
1 parent e82a1ed commit 6f1518d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 26 deletions.
8 changes: 4 additions & 4 deletions upload/admin/controller/catalog/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ public function getList(): string {

$special = '';

$product_specials = $this->model_catalog_product->getDiscounts($result['product_id']);
$product_discounts = $this->model_catalog_product->getDiscounts($result['product_id']);

foreach ($product_specials as $product_special) {
if (($product_special['date_start'] == '0000-00-00' || strtotime($product_special['date_start']) < time()) && ($product_special['date_end'] == '0000-00-00' || strtotime($product_special['date_end']) > time())) {
$special = $this->currency->format($product_special['price'], $this->config->get('config_currency'));
foreach ($product_discounts as $product_discount) {
if (($product_discount['date_start'] == '0000-00-00' || strtotime($product_discount['date_start']) < time()) && ($product_discount['date_end'] == '0000-00-00' || strtotime($product_discount['date_end']) > time())) {
$special = $this->currency->format($product_discount['price'], $this->config->get('config_currency'));

break;
}
Expand Down
9 changes: 7 additions & 2 deletions upload/admin/view/template/catalog/product_form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,12 @@
<option value="F">{{ text_fixed }}</option>
<option value="P">{{ text_percentage }}</option>
</select></td>
<td><div class="form-check form-switch form-switch-lg"><input type="hidden" name="product_discount[{{ discount_row }}][special]" value="0"/><input type="checkbox" name="product_discount[{{ discount_row }}][special]" value="1" class="form-check-input"/></div></td>
<td><div class="form-check form-switch form-switch-lg">
<input type="hidden" name="product_discount[{{ discount_row }}][special]" value="0"/>
<input type="checkbox" name="product_discount[{{ discount_row }}][special]" value="1" class="form-check-input"{% if product_discount.special %} checked{% endif %}/></div>


</td>
<td><input type="date" name="product_discount[{{ discount_row }}][date_start]" value="{{ product_discount.date_start }}" placeholder="{{ entry_date_start }}" class="form-control"/></td>
<td><input type="date" name="product_discount[{{ discount_row }}][date_end]" value="{{ product_discount.date_end }}" placeholder="{{ entry_date_end }}" class="form-control"/></td>
<td class="text-end"><button type="button" onclick="$('#discount-row-{{ discount_row }}').remove();" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa-solid fa-minus-circle"></i></button></td>
Expand Down Expand Up @@ -1850,7 +1855,7 @@ $('#button-discount').on('click', function() {
html += ' <option value="{{ customer_group.customer_group_id }}">{{ customer_group.name|escape('js') }}</option>';
{% endfor %}
html += ' </select><input type="hidden" name="product_discount[' + discount_row + '][product_discount_id]" value=""/></td>';
html += ' <td class="text-end"><input type="text" name="product_discount[' + discount_row + '][quantity]" value="" placeholder="{{ entry_quantity|escape('js') }}" class="form-control"/></td>';
html += ' <td class="text-end"><input type="text" name="product_discount[' + discount_row + '][quantity]" value="1" placeholder="{{ entry_quantity|escape('js') }}" class="form-control"/></td>';
html += ' <td class="text-end"><input type="text" name="product_discount[' + discount_row + '][priority]" value="" placeholder="{{ entry_priority|escape('js') }}" class="form-control"/></td>';
html += ' <td class="text-end"><input type="text" name="product_discount[' + discount_row + '][price]" value="" placeholder="{{ entry_price|escape('js') }}" class="form-control"/></td>';
html += ' <td><select name="product_discount[' + discount_row + '][type]" class="form-select">';
Expand Down
36 changes: 26 additions & 10 deletions upload/catalog/controller/product/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public function index(): ?\Opencart\System\Engine\Action {
$data['price'] = false;
}

if ((float)$product_info['discount'] && ) {
if ((float)$product_info['special']) {
$data['special'] = $this->currency->format($this->tax->calculate($product_info['special'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
} else {
$data['special'] = false;
Expand Down Expand Up @@ -383,20 +383,36 @@ public function index(): ?\Opencart\System\Engine\Action {
}

// Subscriptions
if ($product_info['special']) {
$subscription_price = $product_info['special'];
} else {
$subscription_price = $product_info['price'];
}

$data['subscription_plans'] = [];

$results = $this->model_catalog_product->getSubscriptions($product_id);

foreach ($results as $result) {
$price = $this->currency->format($this->tax->calculate($result['price'], $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
$cycle = $result['cycle'];
$frequency = $this->language->get('text_' . $result['frequency']);
$duration = $result['duration'];

if ($duration) {
$description = sprintf($this->language->get('text_subscription_duration'), $price, $cycle, $frequency, $duration);
} else {
$description = sprintf($this->language->get('text_subscription_cancel'), $price, $cycle, $frequency);
$description = '';

if ($this->customer->isLogged() || !$this->config->get('config_customer_price')) {
if ($result['duration']) {
$price = $subscription_price / $result['duration'];
} else {
$price = $subscription_price;
}

$price = $this->currency->format($this->tax->calculate($price, $product_info['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency']);
$cycle = $result['cycle'];
$frequency = $this->language->get('text_' . $result['frequency']);
$duration = $result['duration'];

if ($duration) {
$description = sprintf($this->language->get('text_subscription_duration'), $price, $cycle, $frequency, $duration);
} else {
$description = sprintf($this->language->get('text_subscription_cancel'), $price, $cycle, $frequency);
}
}

$data['subscription_plans'][] = ['description' => $description] + $result;
Expand Down
90 changes: 85 additions & 5 deletions upload/catalog/model/catalog/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function __construct(\Opencart\System\Engine\Registry $registry) {
$this->registry = $registry;

// Storing some sub queries so that we are not typing them out multiple times.
$this->statement['discount'] = "(SELECT `pd2`.`price` FROM `" . DB_PREFIX . "product_discount` `pd2` WHERE `pd2`.`product_id` = `p`.`product_id` AND `pd2`.`customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "'AND `pd2`.`quantity` = '1' AND ((`pd2`.`date_start` = '0000-00-00' OR `pd2`.`date_start` < NOW()) AND (`pd2`.`date_end` = '0000-00-00' OR `pd2`.`date_end` > NOW())) ORDER BY `pd2`.`priority` ASC, `pd2`.`price` ASC LIMIT 1) AS `discount`";
$this->statement['discount'] = "(SELECT `pd2`.`price` FROM `" . DB_PREFIX . "product_discount` `pd2` WHERE `pd2`.`product_id` = `p`.`product_id` AND `pd2`.`customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "' AND `pd2`.`quantity` = '1' AND `pd2`.`special` = '0' AND ((`pd2`.`date_start` = '0000-00-00' OR `pd2`.`date_start` < NOW()) AND (`pd2`.`date_end` = '0000-00-00' OR `pd2`.`date_end` > NOW())) ORDER BY `pd2`.`priority` ASC, `pd2`.`price` ASC LIMIT 1) AS `discount`";
$this->statement['special'] = "(SELECT `ps`.`price` FROM `" . DB_PREFIX . "product_discount` `ps` WHERE `ps`.`product_id` = `p`.`product_id` AND `ps`.`customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "' AND `ps`.`quantity` = '1' AND `ps`.`special` = '1' 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`";
$this->statement['reward'] = "(SELECT `pr`.`points` FROM `" . DB_PREFIX . "product_reward` `pr` WHERE `pr`.`product_id` = `p`.`product_id` AND `pr`.`customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "') AS `reward`";
$this->statement['review'] = "(SELECT COUNT(*) FROM `" . DB_PREFIX . "review` `r` WHERE `r`.`product_id` = `p`.`product_id` AND `r`.`status` = '1' GROUP BY `r`.`product_id`) AS `reviews`";
}
Expand All @@ -33,13 +34,14 @@ public function __construct(\Opencart\System\Engine\Registry $registry) {
* @return array<string, mixed>
*/
public function getProduct(int $product_id): array {
$query = $this->db->query("SELECT DISTINCT *, `pd`.`name`, `p`.`image`, " . $this->statement['discount'] . ", " . $this->statement['reward'] . ", " . $this->statement['review'] . " FROM `" . DB_PREFIX . "product_to_store` `p2s` LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`p`.`product_id` = `p2s`.`product_id` AND `p`.`status` = '1' AND `p`.`date_available` <= NOW()) LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = `pd`.`product_id`) WHERE `p2s`.`store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `p2s`.`product_id` = '" . (int)$product_id . "' AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
$query = $this->db->query("SELECT DISTINCT *, `pd`.`name`, `p`.`image`, " . $this->statement['discount'] . ", " . $this->statement['special'] . ", " . $this->statement['reward'] . ", " . $this->statement['review'] . " FROM `" . DB_PREFIX . "product_to_store` `p2s` LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`p`.`product_id` = `p2s`.`product_id` AND `p`.`status` = '1' AND `p`.`date_available` <= NOW()) LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = `pd`.`product_id`) WHERE `p2s`.`store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `p2s`.`product_id` = '" . (int)$product_id . "' AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");

if ($query->num_rows) {
$product_data = $query->row;

$product_data['variant'] = (array)json_decode($query->row['variant'], true);
$product_data['override'] = (array)json_decode($query->row['override'], true);
$product_data['price'] = (float)($query->row['discount'] ?: $query->row['price']);
$product_data['rating'] = (int)$query->row['rating'];
$product_data['reviews'] = (int)$query->row['reviews'] ? $query->row['reviews'] : 0;

Expand All @@ -57,7 +59,7 @@ public function getProduct(int $product_id): array {
* @return array<int, array<string, mixed>>
*/
public function getProducts(array $data = []): array {
$sql = "SELECT DISTINCT *, `pd`.`name`, `p`.`image`, " . $this->statement['discount'] . ", " . $this->statement['reward'] . ", " . $this->statement['review'];
$sql = "SELECT DISTINCT *, `pd`.`name`, `p`.`image`, " . $this->statement['discount'] . ", " . $this->statement['special'] . ", " . $this->statement['reward'] . ", " . $this->statement['review'];

if (!empty($data['filter_category_id'])) {
$sql .= " FROM `" . DB_PREFIX . "category_to_store` `c2s`";
Expand Down Expand Up @@ -175,7 +177,7 @@ public function getProducts(array $data = []): array {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN `discount` IS NOT NULL THEN `discount` ELSE `p`.`price` END)";
$sql .= " ORDER BY (CASE WHEN `special` IS NOT NULL THEN `special` CASE WHEN `discount` IS NOT NULL THEN `discount` ELSE `p`.`price` END)";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
Expand Down Expand Up @@ -417,7 +419,7 @@ public function getLayoutId(int $product_id): int {
* @return array<int, array<string, mixed>>
*/
public function getRelated(int $product_id): array {
$sql = "SELECT DISTINCT *, `pd`.`name` AS `name`, `p`.`image`, " . $this->statement['discount'] . ", " . $this->statement['reward'] . ", " . $this->statement['review'] . " FROM `" . DB_PREFIX . "product_related` `pr` LEFT JOIN `" . DB_PREFIX . "product_to_store` `p2s` ON (`p2s`.`product_id` = `pr`.`product_id` AND `p2s`.`store_id` = '" . (int)$this->config->get('config_store_id') . "') LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`p`.`product_id` = `pr`.`related_id` AND `p`.`status` = '1' AND `p`.`date_available` <= NOW()) LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = `pd`.`product_id`) WHERE `pr`.`product_id` = '" . (int)$product_id . "' AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";
$sql = "SELECT DISTINCT *, `pd`.`name` AS `name`, `p`.`image`, " . $this->statement['discount'] . ", " . $this->statement['special'] . ", " . $this->statement['reward'] . ", " . $this->statement['review'] . " FROM `" . DB_PREFIX . "product_related` `pr` LEFT JOIN `" . DB_PREFIX . "product_to_store` `p2s` ON (`p2s`.`product_id` = `pr`.`product_id` AND `p2s`.`store_id` = '" . (int)$this->config->get('config_store_id') . "') LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`p`.`product_id` = `pr`.`related_id` AND `p`.`status` = '1' AND `p`.`date_available` <= NOW()) LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = `pd`.`product_id`) WHERE `pr`.`product_id` = '" . (int)$product_id . "' AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'";

$key = md5($sql);

Expand Down Expand Up @@ -549,6 +551,84 @@ public function getTotalProducts(array $data = []): int {
return (int)$query->row['total'];
}

/**
* Get Specials
*
* @param array<string, mixed> $data
*
* @return array<int, array<string, mixed>>
*/
public function getSpecials(array $data = []): array {
$sql = "SELECT DISTINCT *, `pd`.`name`, `p`.`image`, `p`.`price`, `ps`.price as special, " . $this->statement['discount'] . ", " . $this->statement['reward'] . ", " . $this->statement['review'] . " FROM `" . DB_PREFIX . "product_discount` `ps` LEFT JOIN `" . DB_PREFIX . "product_to_store` `p2s` ON (`ps`.`product_id` = `p2s`.`product_id` AND `p2s`.`store_id` = '" . (int)$this->config->get('config_store_id') . "')LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`p`.`product_id` = `p2s`.`product_id` AND `p`.`status` = '1' AND `p`.`date_available` <= NOW()) LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`pd`.`product_id` = `p`.`product_id`) WHERE `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' AND `ps`.`quantity` = '1' AND `ps`.`special` = '1' AND `ps`.`customer_group_id` = '" . (int)$this->config->get('config_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())) GROUP BY `ps`.`product_id`";

$sort_data = [
'pd.name',
'p.model',
'p.price',
'rating',
'p.sort_order'
];

if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN `special` IS NOT NULL THEN `special` WHEN `discount` IS NOT NULL THEN `discount` ELSE `p`.`price` END)";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY `p`.`sort_order`";
}

if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC, LCASE(`pd`.`name`) DESC";
} else {
$sql .= " ASC, LCASE(`pd`.`name`) ASC";
}

if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}

if ($data['limit'] < 1) {
$data['limit'] = 20;
}

$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}

$key = md5($sql);

$product_data = $this->cache->get('product.' . $key);

if (!$product_data) {
$query = $this->db->query($sql);

$product_data = $query->rows;

$this->cache->set('product.' . $key, $product_data);
}

return (array)$product_data;
}

/**
* Get Total Specials
*
* @return int
*/
public function getTotalSpecials(): int {
$query = $this->db->query("SELECT COUNT(DISTINCT `ps`.`product_id`) AS `total` FROM `" . DB_PREFIX . "product_discount` `ps` LEFT JOIN `" . DB_PREFIX . "product_to_store` `p2s` ON (`p2s`.`product_id` = `ps`.`product_id` AND `p2s`.`store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `ps`.`customer_group_id` = '" . (int)$this->config->get('config_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()))) LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`p2s`.`product_id` = `p`.`product_id` AND `p`.`status` = '1' AND `p`.`date_available` <= NOW())");

if (isset($query->row['total'])) {
return (int)$query->row['total'];
} else {
return 0;
}
}

/**
* Add Report
*
Expand Down
2 changes: 1 addition & 1 deletion upload/catalog/model/checkout/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getProducts(): array {
if ($product['subscription']) {
$subscription_data = [
'frequency_text' => $this->language->get('text_' . $product['subscription']['frequency']),
'price_text' => $this->currency->format($this->tax->calculate($product['subscription']['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'])
'price_text' => $this->currency->format($this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')), $this->session->data['currency'])
] + $product['subscription'];
}

Expand Down
10 changes: 6 additions & 4 deletions upload/system/library/cart/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function getProducts(): array {
}

// Product Discounts
$product_discount_query = $this->db->query("SELECT `price` FROM `" . DB_PREFIX . "product_discount` WHERE `product_id` = '" . (int)$cart['product_id'] . "' AND `customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "' AND `quantity` <= '" . (int)$product_total . "' 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");
$product_discount_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_discount` WHERE `product_id` = '" . (int)$cart['product_id'] . "' AND `customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "' AND `quantity` <= '" . (int)$product_total . "' 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) {
if ($product_discount_query->row['type'] == 'F') {
Expand Down Expand Up @@ -254,12 +254,14 @@ public function getProducts(): array {
$subscription_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product_subscription` `ps` LEFT JOIN `" . DB_PREFIX . "subscription_plan` `sp` ON (`ps`.`subscription_plan_id` = `sp`.`subscription_plan_id`) LEFT JOIN `" . DB_PREFIX . "subscription_plan_description` `spd` ON (`sp`.`subscription_plan_id` = `spd`.`subscription_plan_id`) WHERE `ps`.`product_id` = '" . (int)$cart['product_id'] . "' AND `ps`.`subscription_plan_id` = '" . (int)$cart['subscription_plan_id'] . "' AND `ps`.`customer_group_id` = '" . (int)$this->config->get('config_customer_group_id') . "' AND `spd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' AND `sp`.`status` = '1'");

if ($subscription_query->num_rows) {
$duration = $subscription_query->row['duration'];

// Set the new price if subscription product
if ($subscription_query->row['duration']) {
$price = $price / $subscription_query->row['duration'];
if ($duration) {
$price = $price / $duration;
}

$subscription_data = ['remaining' => $subscription_query->row['duration']] + $subscription_query->row;
$subscription_data = ['remaining' => $duration] + $subscription_query->row;
}

$this->data[$cart['cart_id']] = [
Expand Down

0 comments on commit 6f1518d

Please sign in to comment.