如何顯示給定優惠券程式碼產生的總銷售量?而且,由於粉絲是 VIP,所以我可以建立這個程式碼片段,讓這個部落格跳過我在長列表中的內容排隊,所以您去!

PHP 片段:透過優惠券程式碼顯示總銷售額
/** * @snippet Get Total Sales by COUPON * @sourcecode https://businessbloomer.com/?p=72576 * @author Rodolfo Melogli * @testedwith WooCommerce 3.0.7 */ // ------------------------- // 1. Create function that calculates sales based on coupon code function bbloomer_get_sales_by_coupon($coupon_id) { $args = [ 'post_type' => 'shop_order', 'posts_per_page' => '-1', 'post_status' => ['wc-processing', 'wc-completed', 'wc-on-hold'] ]; $my_query = new WP_Query($args); $orders = $my_query->posts; $total = 0; foreach ($orders as $key => $value) { $order_id = $value->ID; $order = wc_get_order($order_id); $items = $order->get_items('coupon'); foreach ( $items as $item ) { if( $item['code'] == $coupon_id ) { $total += $order->get_total(); } } } return 'Total sales for coupon "' . $coupon_id . '": ' . wc_price($total); } // ------------------------- // 2. Add new tab to WooCommerce "Reports", and print the coupon total sales add_filter( 'woocommerce_admin_reports', 'bbloomer_add_report_tab' ); function bbloomer_add_report_tab( $reports ) { $reports['coupons'] = array( 'title' => __( 'Coupons', 'woocommerce' ), 'reports' => array( "sales_by_code" => array( 'title' => __( 'Sales by code', 'woocommerce' ), 'description' => bbloomer_get_sales_by_coupon('barmada'), //change coupon code here 'hide_title' => false, 'callback' => '', ), ), ); return $reports; }
如何新增此程式碼?
1 、您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部 (如果是 CSS 程式碼,請新增到子主題的 style.css 檔案底部) 修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。
2 、 WordPress 4.9 後改進了主題編輯器,對於 CSS 程式碼也可開啟網站前臺編輯器的 【自定義】,複製程式碼新增到自定義 css 中。
此程式碼是否可用?
如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。