title: Change order status with AJAX in plugin wordpress
body: <p>I am customizing a plugin, which gives recent orders and plays a sound notification. However i want to add an ajax call to a custom button that shows in every row:</p>
  <p>This is my method so far:</p>
  <pre><code>    // ajax function in plugin file
      &lt;?php 
  function my_action_javascript($order_id) {
      $order = wc_get_order( $order_id ); // returns correct order id in every button
      ?&gt;
          &lt;script type=&quot;text/javascript&quot;&gt;             // initiate script
          jQuery(function($){ 
  
              $('pluginchangebutton').click( function(e){
              e.preventdefault();
  
  
                  $.ajax({
                      type: 'POST',
                      dataType: 'json',
                      url: &quot;&lt;?php echo admin_url('admin-ajax.php'); ?&gt;&quot;,
                      data: {
                          'action': 'my_action', // send php function ? 
                          'order_id': &lt;?php echo $order_id; ?&gt;, // Here we send the order Id
                      },
  
                       success: function(response) { // no response at all
                      alert('ok');
              },
              error: function(response) { // no response at all
                  alert('error');
              }
                  });
              });
          });
          &lt;/script&gt; &lt;?php
      }
  
      add_action( 'wp_ajax_my_action', 'my_action' );// register my_action to wp ajax
          function my_action() {
          if ( isset($_POST['order_id']) &amp;&amp; $_POST['order_id'] &gt; 0 ) {       
              $order = wc_get_order($_POST['order_id']); 
              $order-&gt;update_status('completed');      // function to update status
              die();
          }
      }
      // ajax end
  </code></pre>
  <p>This is the ajax function which i place on top of the plugin file.</p>
  <p>And this is how i display my table:</p>
  <pre><code>// get recent orders
   $recent_orders = wc_get_orders(array(
          'limit' =&gt; $show_order_num,
          'orderby' =&gt; 'date',
          'order' =&gt; 'DESC',
          'status' =&gt; $show_order_statuses,
      ));
  
      $content = &quot;&lt;h1&gt;Нови порачки&lt;/h1&gt;&quot;;
      $content .= &quot;&lt;table id='customers-new-order-notification'&gt;&quot;;
      $content .= &quot;&lt;tr&gt;&lt;th&gt;Последни порачки&lt;/th&gt;&lt;/tr&gt;&quot;;
      $content .= &quot;&lt;tr&gt;&lt;th&gt;Порачка бр.&lt;/th&gt;&lt;th&gt;Дата на порачка&lt;/th&gt;&lt;th&gt;Статус на порачка&lt;/th&gt;&lt;th&gt;Детали за порачка&lt;/th&gt;&lt;/tr&gt;&quot;;
      
  
      foreach ($recent_orders as $recent_order) {
          // get order details .. ... .. .. .. .. ..
          $order_id = $recent_order-&gt;get_id();
          $_order = wc_get_order($order_id);
          $order_date = $_order-&gt;get_date_created();
          $order_status = $recent_order-&gt;get_status();
          $order_link = get_site_url();
          $order_link .= &quot;/wp-admin/post.php?post=&quot;;
          $order_link .= $order_id;
          $order_link .= &quot;&amp;action=edit&quot;;
          $billing_first_name_plugin =  $recent_order-&gt;get_billing_first_name();
          $billing_address_1_plugin =  $recent_order-&gt;get_billing_address_1();
          $billing_city_plugin       = $recent_order-&gt;get_billing_city();
          $billing_postalcode_plugin = $recent_order-&gt;get_billing_postcode();
          $billing_email_plugin  = $recent_order-&gt;get_billing_email();
          $billing_phone_plugin  = $recent_order-&gt;get_billing_phone();
          $billing_country_plugin    = $recent_order-&gt;get_billing_country();
          $order_shipping_method_plugin = $recent_order-&gt;get_shipping_method() . ' - ' . $recent_order-&gt;get_shipping_total() . ' rsd';
          $order_get_subtotal_plugin = $recent_order-&gt;get_subtotal() . ' rsd';
          $order_get_total_plugin =  $recent_order-&gt;get_total() . ' rsd';
          $order_javena='Непотврдена';
          $customer_note_plugin = $recent_order-&gt;get_customer_note();
          $statusPrefix = &quot;wc-&quot;;
          $_orderStatus = $statusPrefix . $order_status;
          $_order_status = $order_status_map[$_orderStatus];
          
          $date_format = get_option('date_format'); // format 
          $time_format = get_option('time_format'); // format 
  
          $format_order_date = $time_format . &quot; - &quot; . $date_format;
          $items = $recent_order-&gt;get_items(); // get product items from order 
          
          // add content to $content .. table .. .. . .. row 1
  
          // .... .... ... ... ... ... ... ... row 2
  
          // row 3 start
          $content .= &quot;&lt;td&gt;&quot; . esc_html($order_javena) .&quot;&lt;br&gt;&quot;;
          $content .= my_action_javascript($_order) . &quot;&lt;button class=pluginchangebutton type=submit&gt;Потврди&lt;/button&gt;&quot;;
          $content .=&quot;&lt;/td&gt;&quot;; // row 3 end
          // row 4 add .. .. .. 
          // row 4 end
          if (!$isNew) {
          delete_option('_new_order_id_for_notification');
          $time = $refreshTime;
          header(&quot;Refresh:&quot; . esc_html($time) . &quot;&quot;);
          add_action('wp_head', 'wpb_hook_javascript');
          
      }
      echo $content;
  }
  </code></pre>
  <p>In inspecting, the <strong>$.ajax</strong> gets the <strong>correct order id</strong> 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 &quot; &quot;, still nothing.
  This code is in the plugin file.
  I have tried adding it to functions.php, it does not work.
  Can i get some guidance?</p>