0

I am using Woocommerce with WCMP (WC Markeplace), however vendors are not allowed to update the order status (i.e.: from 'processing' to 'completed').

There's no option in their backend, so I tryed to add a custom button to handle that!

Any help would be amazing! Here's my code (I found it somewhere around here):

<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>

And to handle the form:

<?php
add_action( 'wp_loaded', 'custom_button_to_update' );
function custom_button_to_update(){
    // 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;
    }  
    else {
        echo "failed";
    } 
}

Thank you for your help in advance!

0

Thank you all, I did found the answer HERE.

I adapt a bit the code to my needs and added a button.

html:

<div>
  <form method="post" name="update_status">             
    <button type="submit" name="marked_as_completed">Mark as Completed</button>
  </form>
</div>

php:

<?php 
        if (isset($_POST["marked_as_completed"])) 
        {
        $completed_status = $order->get_id();
        $order = new WC_Order($completed_status);
        $order->update_status('completed', 'order_note');
        echo '<h2 style="color:green;">Completato &check;</h2>';  
        } 
?> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.