Skip to content

Commit

Permalink
https://github.com/opencart/opencart/issues/12661
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkerr committed Apr 6, 2024
1 parent 8157b21 commit 41879c4
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 25 deletions.
80 changes: 68 additions & 12 deletions upload/admin/view/template/sale/order_info.twig
Expand Up @@ -1650,7 +1650,7 @@ $('#order-products').on('submit', 'form', function(e) {
var element = this;
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/cart.remove',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/cart.remove',
type: 'post',
data: $(element).serialize(),
dataType: 'json',
Expand Down Expand Up @@ -1689,7 +1689,7 @@ $('#form-voucher-add').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/voucher.add',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/voucher.add',
type: 'post',
data: $('#form-voucher-add').serialize(),
dataType: 'json',
Expand Down Expand Up @@ -1747,7 +1747,7 @@ $('#order-vouchers').on('submit', 'form', function(e) {
var element = this;
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/voucher.remove',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/voucher.remove',
type: 'post',
data: $(element).serialize(),
dataType: 'json',
Expand Down Expand Up @@ -1784,20 +1784,76 @@ $('#order-vouchers').on('submit', 'form', function(e) {
$('#input-store').on('change', function(e) {
e.preventDefault();
$('#button-refresh').trigger('click');
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=localisation/store',
type: 'post',
data: $('#form-store').serialize(),
dataType: 'json',
beforeSend: function() {
$('#input-store').prop('disabled', true);
},
complete: function() {
$('#input-store').prop('disabled', false);
},
success: function(json) {
$('.alert-dismissible').remove();
if (json['error']) {
$('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
}
if (json['success']) {
$('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
$('#button-refresh').trigger('click');
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
$('#input-language').on('change', function(e) {
e.preventDefault();
$('#button-refresh').trigger('click');
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=localisation/language',
type: 'post',
data: $('#form-language').serialize(),
dataType: 'json',
beforeSend: function() {
$('#input-language').prop('disabled', true);
},
complete: function() {
$('#input-language').prop('disabled', false);
},
success: function(json) {
$('.alert-dismissible').remove();
if (json['error']) {
$('#alert').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa-solid fa-circle-exclamation"></i> ' + json['error'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
}
if (json['success']) {
$('#alert').prepend('<div class="alert alert-success alert-dismissible"><i class="fa-solid fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>');
$('#button-refresh').trigger('click');
}
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
$('#input-currency').on('change', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=localisation/currency',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=localisation/currency',
type: 'post',
data: $('#form-currency').serialize(),
dataType: 'json',
Expand Down Expand Up @@ -1831,7 +1887,7 @@ $('#form-coupon').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/coupon',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/coupon',
type: 'post',
data: $('#form-coupon').serialize(),
dataType: 'json',
Expand Down Expand Up @@ -1866,7 +1922,7 @@ $('#form-voucher').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/voucher',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/voucher',
type: 'post',
data: $('#form-voucher').serialize(),
dataType: 'json',
Expand Down Expand Up @@ -1901,7 +1957,7 @@ $('#form-reward').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/reward',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/reward',
type: 'post',
data: $('#form-reward').serialize(),
dataType: 'json',
Expand Down Expand Up @@ -2009,7 +2065,7 @@ $('#form-affiliate').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/affiliate',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/affiliate',
type: 'post',
data: $('#form-affiliate').serialize(),
dataType: 'json',
Expand Down Expand Up @@ -2145,7 +2201,7 @@ $('#form-payment-address').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&action=sale/payment_address',
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/payment_address',
type: 'post',
data: $('#form-payment-address').serialize(),
dataType: 'json',
Expand Down Expand Up @@ -2854,7 +2910,7 @@ $('#form-history').on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/order.addHistory&store_id=' + $('#input-store').val() + '&language=' + $('#input-language').val() + '&order_id=' + $('#input-order-id').val(),
url: 'index.php?route=sale/order.call&user_token={{ user_token }}&action=sale/order.addHistory&order_id=' + $('#input-order-id').val(),
type: 'post',
dataType: 'json',
data: $('#form-history').serialize(),
Expand Down
40 changes: 40 additions & 0 deletions upload/catalog/controller/api/localisation/language.php
@@ -0,0 +1,40 @@
<?php
namespace Opencart\Catalog\Controller\Api\Localisation;
/**
* Class Language
*
* @package Opencart\Catalog\Controller\Api\Localisation
*/
class Language extends \Opencart\System\Engine\Controller {
/**
* @return void
*/
public function index(): void {
$this->load->language('api/localisation/language');

$json = [];

if (isset($this->request->post['language'])) {
$language = (string)$this->request->post['language'];
} else {
$language = '';
}

$this->load->model('localisation/language');

$language_info = $this->model_localisation_currency->getCurrencyByCode($language);

if (!$language_info) {
$json['error'] = $this->language->get('error_language');
}

if (!$json) {
$this->session->data['language'] = $language;

$json['success'] = $this->language->get('text_success');
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
}
40 changes: 40 additions & 0 deletions upload/catalog/controller/api/localisation/store.php
@@ -0,0 +1,40 @@
<?php
namespace Opencart\Catalog\Controller\Api\Localisation;
/**
* Class Store
*
* @package Opencart\Catalog\Controller\Api\Localisation
*/
class Store extends \Opencart\System\Engine\Controller {
/**
* @return void
*/
public function index(): void {
$this->load->language('api/localisation/store');

$json = [];

if (isset($this->request->post['store'])) {
$store = (string)$this->request->post['store'];
} else {
$store = '';
}

$this->load->model('setting/store');

$language_info = $this->model_localisation_store->getStoreByCode($store);

if (!$language_info) {
$json['error'] = $this->language->get('error_store');
}

if (!$json) {
$this->session->data['store'] = $store;

$json['success'] = $this->language->get('text_success');
}

$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
}
2 changes: 1 addition & 1 deletion upload/catalog/controller/api/sale/cart.php
Expand Up @@ -178,7 +178,7 @@ public function add(): void {
if (!$json) {
$this->cart->add($product_id, $quantity, $option, $subscription_plan_id);

print_r($this->cart->getProducts());
//print_r($this->cart->getProducts());


$json['success'] = $this->language->get('text_success');
Expand Down
6 changes: 6 additions & 0 deletions upload/catalog/language/en-gb/api/localisation/language.php
@@ -0,0 +1,6 @@
<?php
// Text
$_['text_success'] = 'Success: Your language has been changed!';

// Error
$_['error_currency'] = 'Warning: Language could not be found!';
10 changes: 6 additions & 4 deletions upload/catalog/model/checkout/order.php
Expand Up @@ -209,7 +209,7 @@ public function getOrder(int $order_id): array {
}

$order_data['products'] = $this->getProducts($order_id);
$order_data['vouchers'] = $this->getProducts($order_id);
$order_data['vouchers'] = $this->getVouchers($order_id);
$order_data['totals'] = $this->getTotals($order_id);

return $order_data;
Expand All @@ -231,12 +231,14 @@ public function addProduct(int $order_id, array $data): int {

$order_product_id = $this->db->getLastId();

foreach ($data['option'] as $option) {
$this->model_checkout_order->addOption($order_id, $order_product_id, $option);
if (isset($data['option'])) {
foreach ($data['option'] as $option) {
$this->model_checkout_order->addOption($order_id, $order_product_id, $option);
}
}

// If subscription add details
if ($data['subscription']) {
if (isset($data['subscription'])) {
$this->model_checkout_order->addSubscription($order_id, $order_product_id, $data['subscription']);
}

Expand Down
16 changes: 8 additions & 8 deletions upload/system/library/cart/cart.php
Expand Up @@ -53,10 +53,10 @@ public function __construct(\Opencart\System\Engine\Registry $registry) {

if ($this->customer->isLogged()) {
// We want to change the session ID on all the old items in the customers cart
$this->db->query("UPDATE `" . DB_PREFIX . "cart` SET `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "' WHERE `api_id` = '0' AND `customer_id` = '" . (int)$this->customer->getId() . "'");
$this->db->query("UPDATE `" . DB_PREFIX . "cart` SET `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "' WHERE `customer_id` = '" . (int)$this->customer->getId() . "'");

// Once the customer is logged in we want to update the customers cart
$cart_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cart` WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '0' AND `customer_id` = '0' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");
$cart_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cart` WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `customer_id` = '0' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");

foreach ($cart_query->rows as $cart) {
$this->db->query("DELETE FROM `" . DB_PREFIX . "cart` WHERE `cart_id` = '" . (int)$cart['cart_id'] . "' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "'");
Expand All @@ -77,7 +77,7 @@ public function __construct(\Opencart\System\Engine\Registry $registry) {
*/
public function getProducts(): array {
if (!$this->data) {
$cart_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cart` WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");
$cart_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cart` WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");

foreach ($cart_query->rows as $cart) {
$stock = true;
Expand Down Expand Up @@ -374,9 +374,9 @@ public function add(int $product_id, int $quantity = 1, array $option = [], int
$query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "cart` WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "' AND `product_id` = '" . (int)$product_id . "' AND `subscription_plan_id` = '" . (int)$subscription_plan_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");

if (!$query->row['total']) {
$this->db->query("INSERT INTO `" . DB_PREFIX . "cart` SET `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', `customer_id` = '" . (int)$this->customer->getId() . "', `session_id` = '" . $this->db->escape($this->session->getId()) . "', `product_id` = '" . (int)$product_id . "', `subscription_plan_id` = '" . (int)$subscription_plan_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', `quantity` = '" . (int)$quantity . "', `override` = '" . (bool)$override . "', `price` = '" . (float)($override ? $price : 0) . "', `date_added` = NOW()");
$this->db->query("INSERT INTO `" . DB_PREFIX . "cart` SET `store_id` = '" . (int)$this->config->get('config_store_id') . "', `customer_id` = '" . (int)$this->customer->getId() . "', `session_id` = '" . $this->db->escape($this->session->getId()) . "', `product_id` = '" . (int)$product_id . "', `subscription_plan_id` = '" . (int)$subscription_plan_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', `quantity` = '" . (int)$quantity . "', `override` = '" . (bool)$override . "', `price` = '" . (float)($override ? $price : 0) . "', `date_added` = NOW()");
} else {
$this->db->query("UPDATE `" . DB_PREFIX . "cart` SET `quantity` = (`quantity` + " . (int)$quantity . ") WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "' AND `product_id` = '" . (int)$product_id . "' AND `subscription_plan_id` = '" . (int)$subscription_plan_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
$this->db->query("UPDATE `" . DB_PREFIX . "cart` SET `quantity` = (`quantity` + " . (int)$quantity . ") WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "' AND `product_id` = '" . (int)$product_id . "' AND `subscription_plan_id` = '" . (int)$subscription_plan_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
}

// Clear cart data
Expand All @@ -395,7 +395,7 @@ public function add(int $product_id, int $quantity = 1, array $option = [], int
* @return void
*/
public function update(int $cart_id, int $quantity): void {
$this->db->query("UPDATE `" . DB_PREFIX . "cart` SET `quantity` = '" . (int)$quantity . "' WHERE `cart_id` = '" . (int)$cart_id . "' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");
$this->db->query("UPDATE `" . DB_PREFIX . "cart` SET `quantity` = '" . (int)$quantity . "' WHERE `cart_id` = '" . (int)$cart_id . "' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");

// Clear cart data
$this->data = [];
Expand Down Expand Up @@ -423,7 +423,7 @@ public function has(int $cart_id): bool {
* @return void
*/
public function remove(int $cart_id): void {
$this->db->query("DELETE FROM `" . DB_PREFIX . "cart` WHERE `cart_id` = '" . (int)$cart_id . "' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "cart` WHERE `cart_id` = '" . (int)$cart_id . "' AND `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");

unset($this->data[$cart_id]);
}
Expand All @@ -434,7 +434,7 @@ public function remove(int $cart_id): void {
* @return void
*/
public function clear(): void {
$this->db->query("DELETE FROM `" . DB_PREFIX . "cart` WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `api_id` = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");
$this->db->query("DELETE FROM `" . DB_PREFIX . "cart` WHERE `store_id` = '" . (int)$this->config->get('config_store_id') . "' AND `customer_id` = '" . (int)$this->customer->getId() . "' AND `session_id` = '" . $this->db->escape($this->session->getId()) . "'");

$this->data = [];
}
Expand Down

0 comments on commit 41879c4

Please sign in to comment.