主要任务是将值保存在通用元数据存储中。然后可以在任何创建的页面上获取它。
代码是什么:
我的实际代码:
// 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
}
}
}
答案 0 :(得分:1)
由于节省总计是一个全局动态唯一值,因此应使用wp_options
和get_option()
相关的专用功能,将其视为{@ 1}} Wordpress数据库表中的一个选项并保存。 >
我们将设置此选项值为禁用自动加载,以避免缓存问题…
在主要功能中,您应该首先获取此选项的值,然后针对每个订单递归地对该值进行计算,然后最后更新此值。
因此,对于您的主要功能,请尝试以下操作:
update_option()
代码进入您的活动子主题(或活动主题)的function.php文件中。应该可以。
如果该选项已存在,为避免出现缓存问题,您需要使用以下命令将其删除:
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文件中。然后浏览您网站的任何页面并将其删除。