Для проверки клиента (гость или нет) заказы от пользователя "Billing email" требуют прямого ввода Javascript и Ajax для включения (применения) скидки на корзину (отрицательный взнос) вкл.Страница оформления заказа.
Полный код:
// Conditionally apply a percentage discount if customer has already make a purchase
add_action( 'woocommerce_cart_calculate_fees', 'first_time_purchase_percentage_discount', 10, 1 );
function first_time_purchase_percentage_discount( $cart ) {
$percent = 5; // HERE set the discount percentage (float)
if ( is_admin() && ! defined('DOING_AJAX') )
return;
if ( WC()->session->get( 'first_purchase_discount' ) && is_checkout() ) {
// The First order discount percentage calulation
$first_order_discount = $cart->get_subtotal() * $percent / 100;
// Apply the discount
$cart->add_fee( __( 'First Order Discount', 'woocommerce')." ($percent%)", -$first_order_discount );
}
}
// The Wordpress Ajax PHP receiver
add_action( 'wp_ajax_checkout_billing_email', 'get_ajax_checkout_billing_email' );
add_action( 'wp_ajax_nopriv_checkout_billing_email', 'get_ajax_checkout_billing_email' );
function get_ajax_checkout_billing_email() {
// Checking that the posted email is valid
if ( isset($_POST['cb_email']) && filter_var($_POST['cb_email'], FILTER_VALIDATE_EMAIL) ) {
// Get one of customer orders from billing email
$orders = get_posts( array(
'numberposts' => 1, // Just one is enough
'meta_key' => '_billing_email',
'meta_value' => sanitize_email( $_POST['cb_email'] ),
'post_type' => 'shop_order',
'post_status' => array('wc-processing', 'wc-completed')
) );
// If the inputed billing email is not set on a customer order we set the value to TRUE
$value = sizeof($orders) == 0 && ! empty($_POST['cb_email']) ? true : false;
// Set the value in custom Woocommerce session identifier
WC()->session->set('first_purchase_discount', $value );
// Return the session value to jQuery
echo WC()->session->get('first_purchase_discount');
}
die();
}
add_action('wp_footer', 'checkout_billing_email_js_ajax' );
function checkout_billing_email_js_ajax() {
// Only on Checkout
if( is_checkout() && ! is_wc_endpoint_url() ) :
// Initializing
WC()->session->set('first_purchase_discount', false);
?>
<script type="text/javascript">
jQuery(function($){
if (typeof wc_checkout_params === 'undefined')
return false;
$( 'input#billing_email' ).on('change blur', function() {
var value = $(this).val();
console.log(value);
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'checkout_billing_email',
'cb_email': value,
},
success: function (result) {
if( result == 1 ) {
// Update checkout
$(document.body).trigger('update_checkout');
}
console.log(result); // For testing (to be removed)
}
});
});
});
</script>
<?php
endif;
}
Код находится в файле function.php вашей активной дочерней темы (или активной темы).Протестировано и работает.
Дополнение:
Для обработки статуса "В ожидании" также замените следующую строку:
'post_status' => array('wc-processing', 'wc-completed')
этим:
'post_status' => array('wc-on-hold', 'wc-processing', 'wc-completed')