如何通过 WooCommerce 中的管理员检查已付款订单是否已被修改

时间:2021-02-23 15:39:05

标签: php wordpress woocommerce

我想知道是否可以检查付款订单是否已被管理员或商店经理修改。 我有一个 WooCommerce 图书网站,有多个管理员。其中一些是图书供应商,如果他们无法提供客户订单中的任何书籍,他们可以修改订单并删除一些项目,致电客户并退还相关项目。

如何找到这些订单并查看哪些图书已被移除和退款?

我写了一些应该处理这个问题的代码,但我想知道是否可以在后端使用 woocommerce 功能。

任何帮助将不胜感激。

这是我在主题函数中写的代码:

// my idea is to add any successful order's id and it's products to a
// table(name it tmp) in database and then compare this table and wp_posts table 
// per orders and if products for a order id is not in wp-posts table
// but it is in my table it means this order is modified and the code
// compare products and print out the deleted product. 
add_action('woocommerce_payment_complete','add_orders_to_tmp_table');


function add_orders_to_tmp_table($order_id){
    // check after successful order if the order number is not in table it adds the order to table
    global $wpdb;
    $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM order_tmp WHERE order_id = '$order_id'");
    if($rowcount < 1):
        $order = wc_get_order($order_id);
        $order_id = $order->get_id();
        $items = $order->get_items();
        $products = [];
        foreach ($items as $item) {
            array_push($products,$item->get_name());
        }
        $wpdb->insert('order_tmp',array('order_id'=>$order_id,'products'=>serialize($products)));
        $wpdb->close();
    endif;

// i wrote the below code because some of the orders was not stored 
//in the tmp table after successful payment i didn't find the problem 
//and because of that wrote the below code i even change the hooked 
//function to woocommerce_thankyou but still some orders were not in tmp table

    //  here we get all order from X date to N date that is successful 
    // but not in our table and if any, we add it to table 
    // it will make the list up to date by checking after any 
    //successful order for any missed successful order and then add it to the tmp table. 
    $date_from = date("Y-m-d",strtotime("-1 day"));
    $date_to = date("Y-m-d");
    $post_status = implode("','", array('wc-processing', 'wc-completed') );
    $order_ids = [];

    $result = $wpdb->get_results( "SELECT * FROM $wpdb->posts 
            WHERE post_type = 'shop_order'
            AND post_status IN ('{$post_status}')
            AND post_date BETWEEN '{$date_from}  00:00:00' AND '{$date_to} 23:59:59'
        ");


    foreach ($result as $i) {
        array_push($order_ids,$i->ID);
    }

    foreach($order_ids as $i){
        $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM order_tmp WHERE order_id = '$i'");
        if($rowcount == 0):
            $order = wc_get_order($i);
            $items = $order->get_items();
            $products_new = [];
            foreach ($items as $item) {
                array_push($products_new,$item->get_name());
            }
            // print_r($products);
            // $serialized_products = serialize($products_new);
            $serialized_products = serialize($products_new);
            // print($serialized_products);
            $result = $wpdb->insert('order_tmp',array('order_id'=>$i,'products'=>$serialized_products,));
        endif;
    }

}

然后我将在这里检查已付款的订单及其产品的差异:

global $wpdb;

$date_from = date("Y-m-d",strtotime("-30 day"));;
$date_to = date("Y-m-d");
$post_status = implode("','", array('wc-processing', 'wc-completed') );

$result = $wpdb->get_results( "SELECT * FROM $wpdb->posts 
            WHERE post_type = 'shop_order'
            AND post_status IN ('{$post_status}')
            AND post_date BETWEEN '{$date_from}  00:00:00' AND '{$date_to} 23:59:59'
        ");

$order_ids = [];

foreach ($result as $i) {
    array_push($order_ids,$i->ID);
}
// print_r($order_ids);
$count = 0;

foreach($order_ids as $i){
    $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM order_tmp WHERE order_id = '$i'");
    if($rowcount >= 1):
        $count++;
        $products = [];
        $order = wc_get_order($i);
        $items = $order->get_items();
        foreach ($items as $item) {
            array_push($products,$item->get_name());
        }
        // print_r($products);
        // Date from database
        $results = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT products from order_tmp where order_id=%d",$i
            )
        );
        foreach ($results as $result) {
            $array = json_decode( json_encode($result), true);
            $order_current_status = unserialize($array["products"]);
            // echo "***********************************";
        }
        $order_diff = array_diff($products,$order_current_status); 
        if($order_diff):
            echo $i;
            echo '<br>';
            foreach ($order_diff as $key) {
                echo '<br>';
                echo 'yes' . '-------' . $key;
                echo '<br>';
            }
        else:
            echo 'No product deleted from this order' . '---' . $i;
            echo '<br>';
            echo 'Created date: ' . $order->get_date_paid();
            echo '<br>';
            echo 'Modified Date: ' . $order->get_date_modified();
        endif;
        echo '<br>';
        echo '------------------------------------------------------------------------------------------';
        echo '<br>';
    elseif($rowcount == 0):
       
        
        echo "<h1>was not in db-tmp $i</h1>";    
    endif;



}

0 个答案:

没有答案