【问题标题】:Save a dynamic global value related to Woocommerce orders in database在数据库中保存与 Woocommerce 订单相关的动态全局值
【发布时间】:2018-12-15 20:31:45
【问题描述】:

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

代码中的内容:

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

【问题讨论】:

    标签: php wordpress curl woocommerce orders


    【解决方案1】:

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

    我们将设置此选项值禁用自动加载,以避免缓存问题...

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

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

    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 文件中。它应该可以工作。

    如果该选项已经存在,为避免缓存问题,您应该使用以下命令将其删除:

    delete_option('wc-orders-saving');
    

    并将其添加到您的 function.php 文件中。然后浏览您网站的任何页面并将其删除。

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      相关资源
      最近更新 更多