我想向在我的网站上至少购买过一次的每个人自动应用折扣券。这是我尝试过的代码,但我在页面上遇到了致命错误...
function has_bought( $customer_email ){
$orders = get_posts(array(
'numberposts' => -1,
'post_type' => 'shop_order',
'post_status' => array('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;
}
}
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
global $woocommerce;
$coupon_code = '10fidelity'; // coupon code
if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;
if ( has bought() {
$woocommerce->cart->add_discount( $coupon_code );
$woocommerce->show_messages();
}
}