1 回答

TA貢獻1851條經驗 獲得超4個贊
您將獲得的訂單越多,您的功能就越繁重……相反,最好進行一個直接的更輕量級的 SQL 查詢,該查詢將獲取已定義優惠券代碼的訂單總數。
然后在優惠券編輯頁面上使用自定義側元框,您將能夠顯示:
// Get totals orders sum for a coupon code
function get_total_sales_by_coupon( $coupon_code ) {
global $wpdb;
return (float) $wpdb->get_var( $wpdb->prepare("
SELECT SUM( pm.meta_value )
FROM {$wpdb->prefix}postmeta pm
INNER JOIN {$wpdb->prefix}posts p
ON pm.post_id = p.ID
INNER JOIN {$wpdb->prefix}woocommerce_order_items woi
ON woi.order_id = pm.post_id
WHERE pm.meta_key = '_order_total'
AND p.post_status IN ('wc-processing','wc-completed')
AND woi.order_item_name LIKE '%s'
AND woi.order_item_type = 'coupon'
", $coupon_code ) );
}
// Adding Meta container to admin shop_coupon pages
add_action( 'add_meta_boxes', 'add_custom_coupon_meta_box' );
if ( ! function_exists( 'add_custom_coupon_meta_box' ) )
{
function add_custom_coupon_meta_box()
{
add_meta_box( 'coupon_usage_data', __('Usage data','woocommerce'), 'custom_coupon_meta_box_content', 'shop_coupon', 'side', 'core' );
}
}
// Displaying content in the meta container on admin shop_coupon pages
if ( ! function_exists( 'custom_coupon_meta_box_content' ) )
{
function custom_coupon_meta_box_content() {
global $post;
$total = get_total_sales_by_coupon( $post->post_title );
printf( __("Total sales: %s"), wc_price( $total ) );
}
}
代碼在您的活動子主題(或活動主題)的functions.php 文件中。測試和工作。
- 1 回答
- 0 關注
- 161 瀏覽
添加回答
舉報