【发布时间】:2018-12-15 20:31:45
【问题描述】:
主要任务是将值保存在通用元数据存储中。然后可以在任何创建的页面上获取它。
代码中的内容:
- 我们收到订单金额
- 获得订单金额的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