根据 Woocommerce 中的客户订单数量显示不同的通知

Display different notices based on Customer order count in Woocommerce

我正在寻找一种轻量级的方法来计算状态为 "Completed" 的所有客户订单,并根据订单计数,使用 wc_print_notice.

显示不同的消息

计数工作正常,但我希望有人有更轻量级的方法。

显示第一条消息有效,但不显示第二条消息(当完整订单为 2 个或更多时)。

我们的想法是将其扩展为总共 10 条不同的消息,并在客户在网站上购物的整个过程中为客户提供各种折扣代码。

希望您通过查看代码能够理解我想要实现的目标。所以,我要的是两件事的结合;

  1. 如何使 elseif 工作,以便它显示不同的消息而不是同时显示所有消息?

  2. 是否有更轻量级的订单总数计算方法?

这是我的代码:

add_action( 'woocommerce_before_my_account', 'shop_loyalty_program' );
add_action( 'woocommerce_before_shop_loop', 'shop_loyalty_program' );
add_action( 'woocommerce_before_single_product_summary', 'shop_loyalty_program' );

function shop_loyalty_program() {

    $customer = wp_get_current_user();

    // count how many orders by the customer with "completed" status
    // we only count the completed status since completed = paid
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order',
        'post_status' => 'wc-completed' // only "completed" as completed = paid
    ) );


    // discount counter for our loyalty system
    $first_order = 0;
    $third_order = 2;

    // messages to be shown depending on how many completed orders

    // FIRST ORDER message when 0 orders exist
    $first_order_message = sprintf( 'Hey %1$s 😀 For your first order, we\'ll give you a 10 percent discount and with that we say - WELCOME to our store!', $customer->display_name, $first_order );


    // THIRD ORDER message when 2 orders exist
    $third_order_message = sprintf( 'Hey %1$s 😀 We noticed you\'ve placed more than %2$s orders with us – thanks for being a loyal customer!', $customer->display_name, $third_order );


    // discount control
    if ( count( $customer_orders ) >= $first_order ) {
        wc_print_notice( $first_order_message, 'notice' );
    }

    elseif ( count( $customer_orders ) >= $third_order ) {
        wc_print_notice( $third_order_message, 'notice' );
        }
    }

以下重新访问的代码应该按预期工作(订单计数查询更轻SQL)

add_action( 'woocommerce_before_my_account', 'shop_loyalty_program' );
add_action( 'woocommerce_before_shop_loop', 'shop_loyalty_program' );
add_action( 'woocommerce_before_single_product_summary', 'shop_loyalty_program' );
function shop_loyalty_program() {
    global $wpdb;

    // Get current WP User Object instance
    $user = wp_get_current_user();

    // Only for logged in users
    if( $user->ID == 0 ) return false;

    // Count customer orders with "Completed" status (Paid)
    $orders_count = $wpdb->get_var( "
        SELECT COUNT(ID) FROM {$wpdb->prefix}posts as p
        INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
        WHERE p.post_status LIKE 'wc-completed' AND p.post_type LIKE 'shop_order'
        AND pm.meta_key LIKE '_customer_user' AND pm.meta_value = {$user->ID}
    " );

     // FIRST ORDER Message (0 orders)
    if ( $orders_count == 0 ) {
        $message = sprintf( __("Hey %s 😀 For your first order, we'll give you a 10 %% discount and with that we say - WELCOME to our store!"), $user->display_name );
    }
    // TWO ORDERS AT LEAST Message (when 2 orders or more exist)
    elseif ( $orders_count >= 2 ) {
        $message = sprintf( __("Hey %s 😀 We noticed you've placed at least 2 orders with us – Thanks for being a loyal customer!"), $user->display_name );
    }

    // Display message
    if ( isset($message) ) {
        wc_print_notice( $message, 'notice' );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。