Search code examples
phpwoocommerceorders

How to automatically update Order Status for a particular Order ID in WooCommerce


I will take an example to explain my problem.

Based on Order ID, I am storing a value in database having custom table with a column name 'doc_verify'.

So there are three conditions :

1) If doc_verify == 2 then Order Status will set as 'doc-verification'.

2) If doc_verify == 1 then Order Status will set as 'pending'.

3) If doc_verify == 0 then Order Status will set as 'doc-not-verified'.

Now, when doc_verify == 2 then Order Status automatically changed from 'on-hold' to 'doc-verification'. This part is working fine.

Next what I want is :

When I change the doc_verify value to 0 OR 1 in wp-admin/post.php?post={Order ID}&action=edit manually through a custom_meta_box having buttons Verify and Not Verify . Then at the same time it should also change the Order Status automatically.

So, if I click Verify button then Order status must automatically get change from 'doc-verification' to 'pending'.

And if I click Not Verify button then Order status must automatically get change from 'doc-verification' to 'doc-not-verified'.

Currently what happening is :

When I perform anyone of the functionality mentioned above. Then I have to go back to wp-admin/edit.php?post_type=shop_order and then I have to refresh the page again to get the desired Order Status.

Below is the code I am using currently :

//Functionality to show custom data in Order Edit Page

add_action( 'add_meta_boxes', 'wphero_uploaded_documents_for_product' );
function wphero_uploaded_documents_for_product() {
    add_meta_box(
        'woocommerce-order-document-list',
        __( 'Document Verification' ),
        'wphero_uploaded_documents_for_product_content',
        'shop_order',
        'normal',
        'default'
    );
}


function wphero_uploaded_documents_for_product_content( $post ){
    global $wpdb;
    $table_name = $wpdb->prefix . 'users_document';
    $order    = wc_get_order( $post->ID );
    ?>
    <table cellpadding="0" cellspacing="0" class="woocommerce_order_documents_list" width="100%">
        <thead>
            <tr>
                <th class="item_name" width="15%" >Item</th>
                <th class="item_documents sortable" width="55%" >Documents</th>
                <th class="item_documents sortable" width="15%" >Status</th>
                <th class="item_action sortable" width="15%" >Action</th>
            </tr>
        </thead>
        <tbody id="order_line_items">
    <?php
        foreach ($order->get_items() as $item_key => $item ){
        $product = wc_get_product( $item['product_id'] );
        $product_id   = $item->get_product_id(); // the Product id
        $doc_details = $wpdb->get_results("SELECT document_name, document_path FROM $table_name WHERE product_id = $product_id  AND order_id = $post->ID ");
        $doc_name = explode(",",$doc_details[0]->document_name);
        $doc_path = explode(",",$doc_details[0]->document_path);
        ?>
        <tr class="item " data-order_item_id="<?php echo $product_id; ?>">
            <td class="product_name"  ><?php echo $product->get_name(); ?></td>
            <td class="product_documents"  >
                <table cellpadding="0" cellspacing="0" class="product_doc_details" width="100%">
                  <thead>
                    <tr>
                   <?php foreach($doc_name as $name){ ?>
                    <th class="product_doc_name"><?php echo $name; ?></th>
                    <?php } ?>
                      </tr>
                    <thead>
                    <tbody style="text-align: center;">
                    <?php foreach($doc_path as $path){ ?>
                    <td class="product_doc_url">
                       <a href="<?php echo $path; ?>" target="_blank">
                        <img src="<?php echo site_url(); ?>/wp-content/uploads/2019/10/pdf.png">
                        </a>
                    </td>
                    <?php } ?>
                    </tbody>
                </table>
            </td>
            <td class="product_status"  >
            <?php $status = $wpdb->get_results("SELECT doc_verify FROM $table_name WHERE product_id = $product_id  AND order_id = $post->ID "); 
                if($status[0]->doc_verify == 2){
                    echo "<p style='color:blue;'><strong>Under Verification</strong></p>";
                }else if($status[0]->doc_verify == 1){
                    echo "<p style='color:green;'><strong>Verified</strong></p>";
                }else{
                    echo "<p style='color:red;'><strong>Not Verified</strong></p>";
                }
            ?>
            </td>
            <td class="product_action">
            <?php
            $post_id = isset($_GET['post']) ? $_GET['post'] : false;
            if(! $post_id ) return; // Exit
            ?>
            <p><a href="?post=<?php echo $post_id; ?>&action=edit&verify=<?php echo $product_id; ?>" class="button"><?php _e('Verify'); ?></a></p>
            <p><a href="?post=<?php echo $post_id; ?>&action=edit&notverify=<?php echo $product_id; ?>" class="button"><?php _e('Not Verify'); ?></a></p>
            <?php
                if ( isset( $_GET['notverify'] ) && ! empty( $_GET['notverify'] ) ) {
                $wpdb->update($table_name, array('doc_verify'=>0), array('product_id'=>$_GET['notverify'], 'order_id'=>$post_id));
                } 
                if ( isset( $_GET['verify'] ) && ! empty( $_GET['verify'] ) ) {
                $wpdb->update($table_name, array('doc_verify'=>1), array('product_id'=>$_GET['verify'], 'order_id'=>$post_id));
                }
            ?>
           </td>
        </tr>
<?php   } ?>

        </tbody>
    </table>
<?php
}

Solution

  • Update your code from this :

    <?php
        if ( isset( $_GET['notverify'] ) && ! empty( $_GET['notverify'] ) ) {
            $wpdb->update($table_name, array('doc_verify'=>0), array('product_id'=>$_GET['notverify'], 'order_id'=>$post_id));
        } 
        if ( isset( $_GET['verify'] ) && ! empty( $_GET['verify'] ) ) {
            $wpdb->update($table_name, array('doc_verify'=>1), array('product_id'=>$_GET['verify'], 'order_id'=>$post_id));
        }
    ?>
    

    to this:

    <?php
        if ( isset( $_GET['notverify'] ) && ! empty( $_GET['notverify'] ) ) {
            $wpdb->update($table_name, array('doc_verify'=>0), array('product_id'=>$_GET['notverify'], 'order_id'=>$post_id));
            $order->update_status('doc-not-verified');
        } 
        if ( isset( $_GET['verify'] ) && ! empty( $_GET['verify'] ) ) {
            $wpdb->update($table_name, array('doc_verify'=>1), array('product_id'=>$_GET['verify'], 'order_id'=>$post_id));
            $order->update_status('pending');
        }
    ?>