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

时间:2019-01-05 09:33:28

标签: php wordpress woocommerce count notice

我正在寻找一种轻量级的方法来对所有状态为“已完成”的客户订单进行计数,并根据订单数使用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' );
        }
    }

1 个答案:

答案 0 :(得分:0)

以下重新访问的代码应该可以按预期的方式运行(对于订单计数,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文件中。经过测试和工作。

enter image description here


enter image description here