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

WooCommerce:透過優惠券程式碼銷售

PHP 片段:透過優惠券程式碼顯示總銷售額

   /** * @snippet Get Total Sales by COUPON * @how-to Watch tutorial @ https://businessbloomer.com/?p=19055 * @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; }  


可以在哪裡新增此程式碼?

您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部(如果是 CSS 程式碼,請新增到主題的 style.css 檔案底部),修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。


這段程式碼是否正常可用?

或者是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費影片教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。