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

时间:2019-10-24 12:47:54

标签: php woocommerce orders

我将举例说明我的问题。

基于订单ID,我正在将值存储在具有自定义表且其列名为“ doc_verify”的数据库中。

所以有三个条件:

1)如果doc_verify == 2,则订单状态将设置为“ doc-verification”。

2)如果doc_verify == 1,则订单状态将设置为“待处理”。

3)如果doc_verify == 0,则订单状态将设置为“未验证文档”。

现在,当doc_verify == 2时,订单状态将自动从“保留”更改为“文档验证”。这部分工作正常。

接下来我要的是:

通过 custom_meta_box wp-admin / post.php?post = {Order ID}&action = edit 中手动将doc_verify值更改为0或1时,按钮验证和不验证。然后,同时它还应该自动更改“订单状态”。

因此,如果我单击验证按钮,则订单状态必须自动从“ 文档验证”更改为“ 待审核”。< / p>

如果我单击不验证按钮,则订单状态必须自动从“ 文档验证”更改为“ 未经文档验证” >”。

目前正在发生的事情:

当我执行上述任何功能时。然后,我必须返回到 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
}

1 个答案:

答案 0 :(得分:0)

由此更新您的代码:

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