0
votes

So I set up a store for the company I work for. People pay, but it doesn't get shipped to them, because I'll bulk order the items and have them sent to the company and then they'll come see me (at work) to pick their items up.

So what I want to do it have a tablet when they come to pick it up where they put in their phone # to search for their order and then mark it complete. I found a plugin that does most of that (Woocommerce Order Search) so now I am trying to set up a button that they can just click to change their order status to complete.

I deleted the "Shipping Address" and put in the button, but with this code it makes the order completely disappear, not mark it as complete.

  <td>
  <form action="<?php  add_action('woocommerce_order_status_completed') ?>" method="get">
<input type="hidden" value="http://www.store.bandwidthcorp.com/?page_id=13400">
<input type="submit" value="I Got It!">
</form></td>

Then I tried something like this:

<td>
<form action="<?php function order_complete {$order = new WC_Order($order_id);                                     $order->update_status('completed'); }
add_action('order-complete') ?>" method="get">
<input type="hidden" value="http://www.store.bandwidthcorp.com/?page_id=13400">
<input type="submit" value="I Got It!">
</form></td>

But that didn't do anything at all. It seems like this should be pretty simple, but I can't seem to figure it out. Thanks in advance!

1
I believe you need to separate the form "action" parameter from what you're actually trying to do. The form action is more like the URL where you are doing the processing and not the actual processing.helgatheviking

1 Answers

1
votes

Like I mentioned in my comment, I think we want to separate things. First, we'll remove the action from the form. This will cause the current page to reload on form submission

<form method="post">
<input type="hidden" name="mark_as_received" value="true">
<input type="hidden" name="order_id" value="<?php echo esc_attr($order_id);?>">
<?php wp_nonce_field( 'so_38792085_nonce_action', '_so_38792085_nonce_field' ); ?> 
<input type="submit" value="I Got It!">
</form>

Then we will listen on an early WordPress action hook for the posted form inputs.

add_action( 'wp_loaded', 'so_38792085_form_handler' );
function so_38792085_form_handler(){

    // not a "mark as received" form submission
    if ( ! isset( $_POST['mark_as_received'] ) ){
        return;
    }

    // basic security check
    if ( ! isset( $_POST['_so_38792085_nonce_field'] ) || ! wp_verify_nonce( $_POST['_so_38792085_nonce_field'], 'so_38792085_nonce_action' ) ) {   
        return;
    } 

    // make sure order id is submitted
    if ( ! isset( $_POST['order_id'] ) ){
        $order_id = intval( $_POST['order_id'] );
        $order = wc_get_order( $order_id );
        $order->update_status( "completed" );
        return;
    }   
}

You may want to add more security checks (like user permissions), error messages, and I'd probably suggest doing this via AJAX, but that's a basic start.