Page 1 of 1

Mass delete order

Posted: Sat Dec 09, 2017 8:27 pm
by banarsumboro07
How to make mass delete button for order_list?

I have put this

Code: Select all

<button type="submit" form="form-order" formaction="{{ delete }}" data-toggle="tooltip" title="{{ button_delete }}" class="btn btn-danger" onclick="confirm('{{ text_confirm }}') ? $('#form-order').submit() : false;"><i class="fa fa-trash-o"></i></button>
in admin/view/template/sale/order_list.twig

the result is... delete button appears but not functioning well. So every time I select all orders I want to delete, and click the delete button, it just reload the page without deleting the list

how to solve this?

Re: Mass delete order

Posted: Mon Dec 11, 2017 12:10 am
by straightlight
No OC version posted. However, the reason why that is for deleting one order at a time is due to the enforced use of the API with the default installation of Opencart. Since this request is about deletion rather than updating, there might be another option on doing this by rather capturing an array to delete all required orders at once to speed things up as I don't see how it would create any impact.

There seem to be available extensions on the marketplace but none that involves implicit use of the API.

In admin/view/template/sale/order_list.twig file,

find:

Code: Select all

<button type="submit" id="button-invoice" form="form-order" formaction="{{ invoice }}" formtarget="_blank" data-toggle="tooltip" title="{{ button_invoice_print }}" class="btn btn-info"><i class="fa fa-print"></i></button>
add below:

Code: Select all

<button type="submit" id="button-delete" form="form-order" data-toggle="tooltip" title="{{ button_delete }}" class="btn btn-info"><i class="fa fa-print"></i></button>
Then, find:

Code: Select all

$('#form-order li:last-child a').on('click', function(e) {
add above:

Code: Select all

$('#button-delete').on('click', function(e) {
	e.preventDefault();
	
	var element = this;
	
	if (confirm('{{ text_confirm }}')) {
		var selectedValues = $("input[name='selected[]']").map(function() {
			return $(this).val();
		}).get();
		
		$.ajax({
			url: '{{ catalog }}index.php?route=api/order/delete&api_token={{ api_token }}&store_id={{ store_id }}&order_ids=' + encodeURIComponent(selectedValues),
			dataType: 'json',
			beforeSend: function() {
				$(element).parent().parent().parent().find('button').button('loading');
			},
			complete: function() {
				$(element).parent().parent().parent().find('button').button('reset');
			},
			success: function(json) {
				$('.alert-dismissible').remove();
	
				if (json['error']) {
					$('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
				}
	
				if (json['success']) {
					location = '{{ delete }}';
				}
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	}
});
In catalog/controller/api/order.php file,

find:

Code: Select all

if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}

			$order_info = $this->model_checkout_order->getOrder($order_id);

			if ($order_info) {
				$this->model_checkout_order->deleteOrder($order_id);

				$json['success'] = $this->language->get('text_success');
			} else {
				$json['error'] = $this->language->get('error_not_found');
			}
replace with:

Code: Select all

if (!empty($this->request->get['order_ids'])) {
				$multi_orders_check = array();
				
				$order_ids = explode(',', trim($this->request->get['order_ids']));
				
				foreach ($order_ids as $order_id) {
					$order_info = $this->model_checkout_order->getOrder($order_id);

					if ($order_info) {
						$this->model_checkout_order->deleteOrder($order_id);
						
						$multi_orders_check[] = true;
					}
				}
			}
			
			if (!empty($multi_orders_check)) {
				if (in_array(true, $multi_orders_check)) {
					$json['success'] = $this->language->get('text_success');
				} elseif (!in_array(true, $multi_orders_check)) {
					$json['error'] = $this->language->get('error_not_found');
				}
			}
			
			if (empty($multi_orders_check)) {
				if (isset($this->request->get['order_id'])) {
					$order_id = $this->request->get['order_id'];
				} else {
					$order_id = 0;
				}

				$order_info = $this->model_checkout_order->getOrder($order_id);

				if ($order_info) {
					$this->model_checkout_order->deleteOrder($order_id);

					$json['success'] = $this->language->get('text_success');
				} else {
					$json['error'] = $this->language->get('error_not_found');
				}
			}
This should rectify the issue.

Re: Mass delete order

Posted: Mon Dec 11, 2017 12:41 am
by straightlight
Posted updated above.

Re: Mass delete order

Posted: Sat Mar 30, 2019 10:04 pm
by kg0925
straightlight wrote:
Mon Dec 11, 2017 12:10 am
No OC version posted. However, the reason why that is for deleting one order at a time is due to the enforced use of the API with the default installation of Opencart. Since this request is about deletion rather than updating, there might be another option on doing this by rather capturing an array to delete all required orders at once to speed things up as I don't see how it would create any impact.

There seem to be available extensions on the marketplace but none that involves implicit use of the API.

In admin/view/template/sale/order_list.twig file,

find:

Code: Select all

<button type="submit" id="button-invoice" form="form-order" formaction="{{ invoice }}" formtarget="_blank" data-toggle="tooltip" title="{{ button_invoice_print }}" class="btn btn-info"><i class="fa fa-print"></i></button>
add below:

Code: Select all

<button type="submit" id="button-delete" form="form-order" data-toggle="tooltip" title="{{ button_delete }}" class="btn btn-info"><i class="fa fa-print"></i></button>
Then, find:

Code: Select all

$('#form-order li:last-child a').on('click', function(e) {
add above:

Code: Select all

$('#button-delete').on('click', function(e) {
	e.preventDefault();
	
	var element = this;
	
	if (confirm('{{ text_confirm }}')) {
		var selectedValues = $("input[name='selected[]']").map(function() {
			return $(this).val();
		}).get();
		
		$.ajax({
			url: '{{ catalog }}index.php?route=api/order/delete&api_token={{ api_token }}&store_id={{ store_id }}&order_ids=' + encodeURIComponent(selectedValues),
			dataType: 'json',
			beforeSend: function() {
				$(element).parent().parent().parent().find('button').button('loading');
			},
			complete: function() {
				$(element).parent().parent().parent().find('button').button('reset');
			},
			success: function(json) {
				$('.alert-dismissible').remove();
	
				if (json['error']) {
					$('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
				}
	
				if (json['success']) {
					location = '{{ delete }}';
				}
			},
			error: function(xhr, ajaxOptions, thrownError) {
				alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
			}
		});
	}
});
In catalog/controller/api/order.php file,

find:

Code: Select all

if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}

			$order_info = $this->model_checkout_order->getOrder($order_id);

			if ($order_info) {
				$this->model_checkout_order->deleteOrder($order_id);

				$json['success'] = $this->language->get('text_success');
			} else {
				$json['error'] = $this->language->get('error_not_found');
			}
replace with:

Code: Select all

if (!empty($this->request->get['order_ids'])) {
				$multi_orders_check = array();
				
				$order_ids = explode(',', trim($this->request->get['order_ids']));
				
				foreach ($order_ids as $order_id) {
					$order_info = $this->model_checkout_order->getOrder($order_id);

					if ($order_info) {
						$this->model_checkout_order->deleteOrder($order_id);
						
						$multi_orders_check[] = true;
					}
				}
			}
			
			if (!empty($multi_orders_check)) {
				if (in_array(true, $multi_orders_check)) {
					$json['success'] = $this->language->get('text_success');
				} elseif (!in_array(true, $multi_orders_check)) {
					$json['error'] = $this->language->get('error_not_found');
				}
			}
			
			if (empty($multi_orders_check)) {
				if (isset($this->request->get['order_id'])) {
					$order_id = $this->request->get['order_id'];
				} else {
					$order_id = 0;
				}

				$order_info = $this->model_checkout_order->getOrder($order_id);

				if ($order_info) {
					$this->model_checkout_order->deleteOrder($order_id);

					$json['success'] = $this->language->get('text_success');
				} else {
					$json['error'] = $this->language->get('error_not_found');
				}
			}
This should rectify the issue.
it's not working properly. it's deleting all orders available on page even if it's not selected.

Re: Mass delete order

Posted: Sat Mar 30, 2019 11:25 pm
by straightlight
it's not working properly.
OC version? Forum rules.

Re: Mass delete order

Posted: Sun Apr 07, 2019 5:34 pm
by kg0925
OC version 3.0.3.1

Re: Mass delete order

Posted: Sat Oct 12, 2019 2:51 pm
by hermes23
Not working in 3.0.3.2. Can you update the code if you have time?
Thanks :)