Change order status with AJAX in plugin
-
First of all, massive thanks for your plugin. I was going to code something similar, but yours saved me quite a bunch of time. I have modified the new-order-notification-admin.php file of your plugin to include an ajax call registered on a button.
This is my method so far:
Ajax call// ajax function in plugin file <?php function my_action_javascript($order_id) { $order = wc_get_order( $order_id ); // returns correct order id in every button ?> <script type="text/javascript"> // initiate script jQuery(function($){ $('pluginchangebutton').click( function(e){ e.preventdefault(); $.ajax({ type: 'POST', dataType: 'json', url: "<?php echo admin_url('admin-ajax.php'); ?>", data: { 'action': 'my_action', // send php function ? 'order_id': <?php echo $order_id; ?>, // Here we send the order Id }, success: function(response) { // no response at all alert('ok'); }, error: function(response) { // no response at all alert('error'); } }); }); }); </script> <?php } add_action( 'wp_ajax_my_action', 'my_action' );// register my_action to wp ajax function my_action() { if ( isset($_POST['order_id']) && $_POST['order_id'] > 0 ) { $order = wc_get_order($_POST['order_id']); $order->update_status('completed'); // function to update status die(); } } // ajax end
// row 3 start $content .= "<td>" . esc_html($order_javena) ."<br>"; // custom variable $content .= my_action_javascript($_order) . "<button class=pluginchangebutton type=submit>Потврди</button>"; // button is here $content .="</td>"; // row 3 end
In inspecting, the $.ajax gets the correct order id for every button, however i do not get a response at all. I’ve tried with console.log as well but still, no response. I have tried inserting type, datatype and url with ‘ ‘, as well as ” “, still nothing. This code is in the plugin file. I have tried adding it to functions.php, it does not work.
Whole new-order-notification.php file
<?php add_action('woocommerce_checkout_order_processed', 'detect_new_order_on_checkout'); function detect_new_order_on_checkout($order_id) { $options = get_option('_new_order_id_for_notification'); if (!$options) { add_option('_new_order_id_for_notification', array( 'order_id' => $order_id )); } else { update_option('_new_order_id_for_notification', array( 'order_id' => $order_id )); } } function my_action_javascript($order_id) { $order = wc_get_order( $order_id ); // returns correct order id in every button ?> <script type="text/javascript"> jQuery(function($){ $('pluginchangebutton').click( function(e){ e.preventdefault(); $.ajax({ type: "POST", dataType: "json", url: "<?php echo admin_url('admin-ajax.php'); ?>", data: { 'action': 'my_action', 'order_id': <?php echo $order_id; ?>, // Here we send the order Id }, success: function(response) { alert('ok'); }, error: function(response) { alert('error'); } }); }); }); </script> <?php } add_action( 'wp_ajax_my_action', 'my_action' ); function my_action() { if ( isset($_POST['order_id']) && $_POST['order_id'] > 0 ) { $order = wc_get_order($_POST['order_id']); $order->update_status('completed'); die(); } } add_action('detect_new_order_on_checkout', 'customer_completes_order_thankyou'); add_action('admin_menu', 'new_order_notification'); function new_order_notification() { add_menu_page('Порачки', 'Порачки', 'delete_posts', 'new_order_notification', 'new_order_notification_menu', 'dashicons-warning', '54'); add_submenu_page('new_order_notification', 'Небитно', 'Небитно', 'manage_options', 'new_order_notification_settings', 'new_order_notification_settings'); } function new_order_notification_menu() { $isNew = false; $all_product_ids = get_posts(array( 'posts_per_page' => -1, 'post_type' => array('product', 'product_variation'), 'fields' => 'ids', )); global $wp_roles; $roles = $wp_roles->roles; $roleValues = array_keys($roles); $all_user_roles = $roleValues; $order_status_map = wc_get_order_statuses(); $order_status_keys = array_keys($order_status_map); $options = get_option('__new_order_option'); if ($options) { if ($options['mp3_url']) { $musicUrlMp3 = $options['mp3_url']; } else { $musicUrlMp3 = plugins_url('assets/order-music.mp3', __FILE__); } if ($options['refresh_time']) { $refreshTime = $options['refresh_time']; } else { $refreshTime = 30; } if ($options['order_header']) { $order_header = $options['order_header']; } else { $order_header = "Order Notification - New Order"; } if ($options['order_text']) { $order_text = $options['order_text']; } else { $order_text = "Check Order Details: "; } if ($options['confirm']) { $confirm = $options['confirm']; } else { $confirm = "ACKNOWLEDGE THIS NOTIFICATION"; } if ($options['statuses']) { $order_statuses = $options['statuses']; } else { $order_statuses = $order_status_keys; } if ($options['product_ids']) { $product_ids = $options['product_ids']; } else { $product_ids = $all_product_ids; } if ($options['user_roles']) { $user_roles = $options['user_roles']; } else { $user_roles = $all_user_roles; } if ($options['show_order_num']) { $show_order_num = $options['show_order_num']; } else { $show_order_num = 20; } if ($options['show_order_statuses']) { $show_order_statuses = $options['show_order_statuses']; } else { $show_order_statuses = $order_status_keys; } update_option('__new_order_option', array( 'refresh_time' => $refreshTime, 'mp3_url' => $musicUrlMp3, 'order_header' => $order_header, 'order_text' => $order_text, 'confirm' => $confirm, 'statuses' => $order_statuses, 'product_ids' => $product_ids, 'user_roles' => $user_roles, 'show_order_num' => $show_order_num, 'show_order_statuses' => $show_order_statuses )); $user = wp_get_current_user(); $isRestrictedUserRole = true; if (is_array($user_roles) && count($user_roles)) { foreach ($user_roles as $user_role) { if (in_array($user_role, $user->roles)) { $isRestrictedUserRole = false; } } } else { $isRestrictedUserRole = false; } if ($isRestrictedUserRole) { echo "<br><br><h2>You don't have permission to see New Order Notification page.</h2>"; return; } } else { $musicUrlMp3 = plugins_url('assets/order-music.mp3', __FILE__); $refreshTime = 30; $order_header = "Order Notification - New Order"; $order_text = "Check Order Details: "; $confirm = "ACKNOWLEDGE THIS NOTIFICATION"; $order_statuses = $order_status_keys; $product_ids = $all_product_ids; $user_roles = $all_user_roles; $show_order_num = 20; $show_order_statuses = $order_status_keys; add_option('__new_order_option', array( 'refresh_time' => $refreshTime, 'mp3_url' => $musicUrlMp3, 'order_header' => $order_header, 'order_text' => $order_text, 'confirm' => $confirm, 'statuses' => $order_statuses, 'product_ids' => $product_ids, 'user_roles' => $user_roles, 'show_order_num' => $show_order_num, 'show_order_statuses' => $show_order_statuses )); } $checkOrders = wc_get_orders(array('status' => $order_status_keys)); $numberOfOrders = 0; if (is_array($checkOrders)) { $numberOfOrders = count($checkOrders); } if ($numberOfOrders == 0) { echo "<h1>You have not received any orders yet.<br><br>This page will be refreshed for every 5 seconds to check if your first order is received.</h1>"; header("Refresh: 5"); return; } else { $any_new_order = get_option('_new_order_id_for_notification'); if ($any_new_order) { $alertForThisProduct = false; $isAllProducts = true; if (is_array($product_ids) && count($product_ids) != 0) { $isAllProducts = false; } $lastOrderId = $any_new_order['order_id']; $lastOrder = wc_get_order($lastOrderId); if (!$isAllProducts) { foreach ($lastOrder->get_items() as $item_id => $item) { $product_id = $item->get_product_id(); $variation_id = $item->get_variation_id(); if (in_array($product_id, $product_ids) || in_array($variation_id, $product_ids)) { $alertForThisProduct = true; } } } $statusPrefix = "wc-"; $lastOrderStatus = $lastOrder->get_status(); $lastOrderStatus = $statusPrefix . $lastOrderStatus; if (in_array($lastOrderStatus, $order_statuses) && ($isAllProducts || $alertForThisProduct)) { $isNew = true; } $websiteUrl = get_site_url(); $websiteUrl .= "/wp-admin/post.php?post="; $websiteUrl .= $lastOrderId; $websiteUrl .= "&action=edit"; if ($isNew) { ?> <script type='text/javascript'> window.focus(); jQuery(function ($) { var overlay = $('<div id="overlay"></div>'); overlay.show(); overlay.appendTo(document.body); $('.popup').show(); $('.close').click(function () { $('.popup').hide(); overlay.appendTo(document.body).remove(); location.reload(); return false; }); $('.x').click(function () { $('.popup').hide(); overlay.appendTo(document.body).remove(); return false; }); }); </script> <?php $audiocontent = "<audio controls autoplay ><source src='" . esc_html($musicUrlMp3) . "' type='audio/mpeg'>Пиши на бе?о.</audio>"; echo $audiocontent; $popupcontent = "<div class='popup'><div class='cnt223'><h1>" . esc_html($order_header) . "</h1><p>" . esc_html($order_text) . " <a href='" . esc_html($websiteUrl) . "' target='_blank'>" . esc_html($lastOrderId) . "</a><br/><br/><a href='' class='close'>" . esc_html($confirm) . "</a></p></div></div>"; echo $popupcontent; delete_option('_new_order_id_for_notification'); } } } $recent_orders = wc_get_orders(array( 'limit' => $show_order_num, 'orderby' => 'date', 'order' => 'DESC', 'status' => $show_order_statuses, )); $content = "<h1>Нови порачки</h1>"; $content .= "<table id='customers-new-order-notification'>"; $content .= "<tr><th>Последни порачки</th></tr>"; $content .= "<tr><th>Порачка бр.</th><th>Дата на порачка</th><th>Статус на порачка</th><th>Детали за порачка</th></tr>"; foreach ($recent_orders as $recent_order) { $order_id = $recent_order->get_id(); $_order = wc_get_order($order_id); $order_date = $_order->get_date_created(); $order_status = $recent_order->get_status(); $order_link = get_site_url(); $order_link .= "/wp-admin/post.php?post="; $order_link .= $order_id; $order_link .= "&action=edit"; $billing_first_name_plugin = $recent_order->get_billing_first_name(); $billing_address_1_plugin = $recent_order->get_billing_address_1(); $billing_city_plugin = $recent_order->get_billing_city(); $billing_postalcode_plugin = $recent_order->get_billing_postcode(); $billing_email_plugin = $recent_order->get_billing_email(); $billing_phone_plugin = $recent_order->get_billing_phone(); $billing_country_plugin = $recent_order->get_billing_country(); $order_shipping_method_plugin = $recent_order->get_shipping_method() . ' - ' . $recent_order->get_shipping_total() . ' rsd'; $order_get_subtotal_plugin = $recent_order->get_subtotal() . ' rsd'; $order_get_total_plugin = $recent_order->get_total() . ' rsd'; $order_javena='Непотврдена'; $customer_note_plugin = $recent_order->get_customer_note(); $statusPrefix = "wc-"; $_orderStatus = $statusPrefix . $order_status; $_order_status = $order_status_map[$_orderStatus]; $date_format = get_option('date_format'); $time_format = get_option('time_format'); $format_order_date = $time_format . " - " . $date_format; $items = $recent_order->get_items(); $content .= "<tr><td><p id=bigtextplugindate>" .esc_html($order_date->date($format_order_date))."</p><br><p id=bigtextplugin>Име: </p>" . esc_html($billing_first_name_plugin) ."<br><p id=bigtextplugin>Адреса: </p>". esc_html($billing_address_1_plugin)."<br><p id=bigtextplugin>Град: </p>".esc_html($billing_city_plugin). // row 1 "<br><p id=bigtextplugin>Поштенски код: </p>" .esc_html($billing_postalcode_plugin) ."<br><p id=bigtextplugin>Е-маил: </p>".esc_html($billing_email_plugin). "<br><p id=bigtextplugin>Телефон: </p>".esc_html($billing_phone_plugin)."<br><p id=bigtextplugin>Држава: </p>".esc_html($billing_country_plugin)."</td><td>"; // row 1 foreach ( $items as $item ) { $product_name = $item['name']." x ".$item['qty']." - ".$item['subtotal'] ." rsd"; // row 2 $content .= esc_html($product_name) ."<br>"; } $content .= esc_html($order_get_subtotal_plugin). "<br>".esc_html($order_shipping_method_plugin). "<br>". esc_html($order_get_total_plugin). "</td>"; // row 2 $content .= "<td>" . esc_html($order_javena) ."<br>"; $content .= my_action_javascript($_order) . "<button class=pluginchangebutton type=submit>Потврди</button>"; // button $content .="</td>"; $content .="<td><a href='" . esc_html($order_link) . "' target='_blank'>ПОГЛЕДНИ ДЕТАЛИ</a> "; } $content .= "</table><br><hr>"; // енд табле if (!$isNew) { delete_option('_new_order_id_for_notification'); $time = $refreshTime; header("Refresh:" . esc_html($time) . ""); add_action('wp_head', 'wpb_hook_javascript'); } global $post; $content .= "<br><br><div class='main-info-bottom'><p>1 - За да работе звуко, таа страна треба да е секо?пат отворана.</p>"; $content .= "<p>3 - Проби го звуко: </p><audio controls style='display: block'><source src='" . esc_html($options['mp3_url']) . "' type='audio/mpeg'>Пиши на бе?о.</audio></div>"; echo $content; }
Can i get some guidance?
- The topic ‘Change order status with AJAX in plugin’ is closed to new replies.