在数据库中保存与 Woocommerce 订单相关的动态全局值

Save a dynamic global value related to Woocommerce orders in database

主要任务是将值保存在通用元数据存储中。然后可以在任何创建的页面上获取它。

代码中的内容:

  1. 我们收到订单金额
  2. 获得订单金额的5%
  3. 从公共元数据中获取保存的数量。
  4. 在新订单的节省金额中增加 5%。
  5. 在元数据中保存总量。

我的实际代码:

// Get daily orders IDs to be checked
function get_order_ids_to_check(){
    global $wpdb;

    return $wpdb->get_col( "
        SELECT p.ID
        FROM {$wpdb->prefix}posts as p
        WHERE p.post_type LIKE 'shop_order'
        AND p.post_status IN ('wc-on-hold','wc-processing')
        AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - 86400)
    " );
}

function send_daily_orders_to_delivery() {

    // Loop through each order Ids
    foreach( get_order_ids_to_check() as $order_id ){
        // Get an instance of the WC_Order object
        $order  = wc_get_order($order_id);
        // Get order total
        $order_total = $order->get_total();
            $secret = ''; // Secret key to be set

        $data   = '&secret='.$secret.'&order_id='.get_post_meta( $order_id, 'delivery_order_id', true );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?get_status");
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);

        $decoded = (array) json_decode($result);

        // Update order status
        if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['status']) && ! empty($decoded['status']) ){
            if( $decoded['status'] == "Completed" )
                $order->update_status( 'wc-completed' );
                // Get $update_total the total amount of percentages from metadata
                $saving_total = // Need code
                // Get 5 percent of the total order amount
                $percent = 5;
                $percent_total = ($percent / 100) * $order_total;
                // Get the sum of the numbers to update the value in the database
                $update_total = $saving_total + $percent_total; // This value must be overwritten in the database
                // Save $update_total the total amount of percentages to metadata (General metadata that can be called on any page created)
                update_post_meta(); // Need code
        }
    }
}

由于保存总计是一个全局动态唯一值,因此应将其作为选项处理并保存在 wp_options Wordpress 数据库 table 中,使用 get_option()update_option()相关专用功能。

We will set this option value with autoload disabled, to avoid caching problems…

在您的主函数中,您应该需要在开始时获取此选项值,然后对每个订单的值进行递归计算,然后在最后更新此值。

因此,对于您的主要功能,请尝试类似的操作:

function send_daily_orders_to_delivery() {
    // Get the actual option value for saving total orders amount
    $option_name = 'wc-orders-saving';
    $orders_saving = $new_orders_saving = (float) get_option( $option_name );
    $percent = 5; // saving percentage
    
    // Loop through each order Ids
    foreach( get_order_ids_to_check() as $order_id ){
        // Get an instance of the WC_Order object
        $order  = wc_get_order($order_id);
        // Get order total
        $order_total = $order->get_total();
            $secret = ''; // Secret key to be set

        $data   = '&secret='.$secret.'&order_id='.get_post_meta( $order_id, 'delivery_order_id', true );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?get_status");
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);

        $decoded = (array) json_decode($result);

        // If order is processable, add the calculation to saving total and update status
        if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['status']) && ! empty($decoded['status']) &&$decoded['status'] == "Completed" ){
            // Set recursively calculation in existing "orders saving" value
            $new_orders_saving += (float) ($percent * $order_total / 100);
            
            // Update order status
            $order->update_status( 'wc-completed' );
        }
    }
    // Updating "Order saving" global value
    if( $orders_saving !== $new_orders_saving ) {
        if ( get_option( $option_name ) !== false ) {
            update_option($option_name, $new_orders_saving );
        } else {
            add_option( $option_name, $new_orders_saving, null, 'no' );
        }
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。应该可以。

If the option already exist, to avoid caching issue, you should need to delete it using:

delete_option('wc-orders-saving');

And adding it in your function.php file. Then browse any page of your web site and remove it.