Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.7 kB
6
Indexable
Never

add_filter('salesking_show_default_button_my_orders_page','__return_false');

add_action('salesking_my_orders_actions', function($order){
	if (isset($order->ID)){
		$order_id = $order->ID;
	} else {
		$order_id = $order->get_id();
	}
	?>
    <td class="nk-tb-col">
        <div class="salesking_manage_order_container">
            <button class="btn btn-sm btn-primary salesking_delete_order" type="button" value="<?php echo esc_attr($order_id);?>"><em class="icon ni ni-trash"></em><span><?php esc_html_e('Delete Order','salesking');?></span></button>
        </div>
    </td>
	<?php
}, 10, 1);

add_action('salesking_dashboard_head', function(){
	?>
	<script>
		jQuery(document).ready(function(){
			jQuery('.salesking_delete_order').on('click', function(){
				// Run ajax request
				if (confirm('Are you sure you want to delete this order?')){
					var datavar = {
			            action: 'saleskingdeleteorder',
			            orderid: jQuery(this).val(),
			        };
			        jQuery.post(salesking_display_settings.ajaxurl, datavar, function(response){
			        	location.reload();
			        });
			    }
			});
		});
	</script>
	<?php
});


function saleskingdeleteorder(){

	$order_id = sanitize_text_field($_POST['orderid']);

	// verify that author has this order
	$order_author = get_post_meta($order_id,'salesking_assigned_agent', true);
	if (intval($order_author) === get_current_user_id()){
		$order = wc_get_order($order_id);
		$order->update_status('cancelled');
		$order->save();
		wp_delete_post($order_id);
	}

	echo 'success';
	exit();

}

add_action( 'wp_ajax_saleskingdeleteorder', 'saleskingdeleteorder');
add_action( 'wp_ajax_nopriv_saleskingdeleteorder', 'saleskingdeleteorder');