php - 如果客户在之前的 WooCommerce 订单中使用过相关优惠券,则限制使用优惠券

标签 php wordpress woocommerce orders coupon

我正在寻找一种方法来限制优惠券的使用,并在客户之前在 WooCommerce 中的先前订单中使用过相关优惠券时显示错误消息。

通过相关优惠券,我的意思是:出现在预定义数组中的优惠券代码以及购物车/结帐页面上当前插入的优惠券。在此基础上进行比较。

我的代码尝试:

add_filter( 'woocommerce_coupon_is_valid', 'specific_coupons_valid', 10,
3 );
function specific_coupons_valid( $is_valid, $coupon, $discount ){
     $coupon_codes = array( 'free10', 'free20', 'free30' );

     $args = array(
         'posts_per_page'   => -1,
         'orderby'          => 'title',
         'order'            => 'asc',
         'post_type'        => 'shop_coupon',
         'post_status'      => 'publish',
     );

     $coupons = get_posts( $args );

     if( in_array( $coupons, $coupon_codes ) ) {
         $is_valid = false;
     }

     return $is_valid;
}

感谢任何帮助

最佳答案

如果客户之前在 WooCommerce 的先前订单中使用过相关优惠券,要限制优惠券的使用,您可以使用:

// Compare & return true OR false
function compare_related_coupon_codes( $current_coupon_code ) {
    // Add multiple coupon codes to compare, seperated by a comma
    $compare_coupon_codes = array( 'coupon1', 'coupon2', 'coupon3' );
    
    // Default
    $valid = true;
    
    // When the current coupon code has to be compared with other coupon codes
    if ( in_array( $current_coupon_code, $compare_coupon_codes ) ) {        
        // Get user ID
        $user_id = get_current_user_id();
        
        // Get the WC_Customer instance Object
        $customer = New WC_Customer( $user_id );
        
        // Billing email
        $email = $customer->get_billing_email();

        // Loop through         
        foreach ( $compare_coupon_codes as $coupon_code ) {
            // Get the WC_Coupon instance Object
            $coupon_obj = New WC_Coupon( $coupon_code );

            // If one of the coupons has already been used by the customer
            if ( array_intersect( array( $user_id, $email ), $coupon_obj->get_used_by() ) ) {
                $valid = false;
                break;
            }   
        }
    }
    
    // Return
    return $valid;
}

// Valid
function filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) {
    if ( ! is_user_logged_in() ) return $valid;
    
    // Get current applied coupon code
    $current_coupon_code = strtolower( $coupon->get_code() );

    // Call function, true OR false
    $valid = compare_related_coupon_codes( $current_coupon_code ); 

    // NOT valid
    if ( ! $valid ) {
        throw new Exception( __( 'My error', 'woocommerce' ), 109 );
    }

    return $valid;
}
add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );

通过我的答案中添加的评论标签进行解释

相关:Coupon daily time range in WooCommerce

关于php - 如果客户在之前的 WooCommerce 订单中使用过相关优惠券,则限制使用优惠券,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67868421/

相关文章:

php - Guzzle (35) gnutls_handshake() 失败 : An unexpected TLS packet was received over HTTP

html - 页脚不会靠边栏下降

wordpress - 短代码输出在新行后添加 <br/>

wordpress - 动态添加自定义产品数据作为订单上的项目元数据

php - 管理允许在 Woocommerce 中购买的客户

php - 使用 JS slider 显示来自电子商店 API 的产品的可嵌入小部件

php - Twilio 在连接到设备时获取 session SID

php - 比较 mysql 表上的行

ssl - WordPress 正在禁用 HTTPS。它是怎么做到的,我该如何阻止它?

php - 使用 IF 语句映射 Woocommerce 订单状态