亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Woocommerce 管理優惠券編輯頁面上顯示自定義數據

在 Woocommerce 管理優惠券編輯頁面上顯示自定義數據

PHP
墨色風雨 2022-01-24 09:38:09
對于我在系統中定義的每張優惠券,我想顯示使用統計信息:它使用了多少銷售額,提供了多少折扣等等......我想在管理中的該優惠券的編輯頁面上添加該數據(作為新標簽或元框)所以我有代碼來計算使用該優惠券的所有銷售額。但是如何將其添加到 woocommerce 管理中的優惠券編輯頁面function get_sales_by_coupon($coupon_id) {    $args = [        'post_type' => 'shop_order',        'posts_per_page' => '-1',        'post_status' => ['wc-processing', 'wc-completed']    ];    $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);}
查看完整描述

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 文件中。測試和工作。

http://img1.sycdn.imooc.com//61ee031b0001ae9902870091.jpg

查看完整回答
反對 回復 2022-01-24
  • 1 回答
  • 0 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號