如何在 WooCommerce 中自动更新特定订单 ID 的订单状态

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

我举个例子来说明我的问题

根据订单 ID,我将一个值存储在具有自定义 table 的数据库中,列名称为 'doc_verify'。

所以有三个条件:

1) 如果 doc_verify == 2 那么订单状态将设置为 'doc-verification'.

2) 如果 doc_verify == 1 那么订单状态将设置为 'pending'.

3) 如果 doc_verify == 0 那么订单状态将设置为 'doc-not-verified'.

现在,当 doc_verify == 2 时,订单状态会自动从 'on-hold' 更改为 'doc-verification'。这部分工作正常。

接下来我要的是:

当我在 wp-admin/post.php?post={Order ID}&action=edit 中将 doc_verify 值更改为 0 或 1 时 手动通过 custom_meta_box 按钮 Verify 和 Not Verify 。然后同时它也应该自动更改订单状态。

因此,如果我单击 Verify 按钮,那么订单状态必须自动从“doc-verification”更改为“[=39” =]待定'.

如果我点击不验证按钮,那么订单状态必须自动从“doc-verification”更改为“[=39” =]文档未验证'.

目前发生的事情是:

当我执行上述任何功能时。然后我必须回到 wp-admin/edit.php?post_type=shop_order 然后我必须再次刷新页面才能获得所需的订单状态。

下面是我目前使用的代码:

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

从此更新您的代码:

<?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));
    }
?>

对此:

<?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');
    }
?>