通过检查GUEST客户的电子邮件地址是否有已处理和已完成的订单,如果电子邮件没有订单,我想给GUEST一个“第一订单折扣”。如果在访客输入电子邮件时发生这种情况,那会很好。
我认为我已经设法制作了折扣代码,现在我要求合并这两个代码以使其全部正常工作。
这是折扣代码:
add_action( 'woocommerce_cart_calculate_fees', 'first_time_order_discount', 10, 1 );
function first_time_order_discount( $wc_cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
// discount percentage, change this into whatever you like
// currently set to 5%
$percent = 5;
// calculate first order discount based on the percentage above
$first_order_discount = $wc_cart->cart_contents_total * $percent / 100;
$wc_cart->add_fee( __( 'First Order Discount', 'woocommerce')." ($percent%)", -$first_order_discount );
}
这是控制代码:
function has_bought( $customer_email ){
$orders = get_posts( array(
'numberposts' => -1,
'post_type' => 'shop_order',
'post_status' => array('wc-processing', 'wc-completed'),
) );
$email_array = array();
foreach($orders as $order) {
$order_obj = wc_get_order($order->ID);
$order_obj_data = $order_obj->get_data();
array_push($email_array, $order_obj_data['billing']['email']);
}
if (in_array($customer_email, $email_array)) {
return true;
} else {
return false;
}
}
不确定如何将两者合并为一个。
答案 0 :(得分:0)
要检查来自“结算电子邮件”用户实时输入的客户(来宾)订单,需要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')