Я ищу легкий способ подсчета всех заказов клиентов со статусом «Завершено» и на основе количества заказов, для отображения различных сообщений с помощью wc_print_notice
.
Подсчет работает нормально, но я надеюсь, что у кого-то есть более легкий способ сделать это.
Отображение первого сообщения работает, но оно не показывает 2-е сообщение (когда количество выполненных заказов равно 2 или более).
Идея состоит в том, чтобы распространить это в общей сложности на 10 различных сообщений и предоставить покупателю различные коды скидок во время покупок на сайте.
Надеюсь, посмотрев код, вы поймете, чего я пытаюсь достичь. Итак, что я прошу, так это сочетание ДВУХ вещей;
как мне заставить elseif работать так, чтобы он отображал разные сообщения, а не все сообщения одновременно?
Есть ли более легкий способ подсчета суммы заказа?
Вот мой код:
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' );
}
}