php 通过允许的用户ID添加限制到WooCommerce优惠券

nqwrtyyt  于 12个月前  发布在  PHP
关注(0)|答案(3)|浏览(102)

假设我正在使用此代码创建一个优惠券:

$coupon_code = 'CODEOFF';
$amount = '10';
$discount_type = 'percent';
                    
$coupon = array(
    'post_title' => $coupon_code,
    'post_status' => 'publish',
    'post_author' => 1,
    'post_type'     => 'shop_coupon'
);
                    
$new_coupon_id = wp_insert_post( $coupon );
                    
update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
update_post_meta( $new_coupon_id, 'product_ids', $some_id );
update_post_meta( $new_coupon_id, 'usage_limit', '1' );
update_post_meta( $new_coupon_id, 'expiry_date', $some_date );

字符串
现在我想限制这个代码只用于一个用户。
有一个使用update_post_meta( $new_coupon_id, 'customer_email', '' );的选项,但我的用户没有电子邮件。
如何限制使用该用户ID的用户使用此优惠券?

8iwquhpp

8iwquhpp1#

要保持其动态性,可以使用以下代码向使用限制选项卡添加新字段:

// Add new field - usage restriction tab
function action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) {
    woocommerce_wp_text_input( array( 
        'id' => 'customer_user_id',  
        'label' => __( 'User ID restrictions', 'woocommerce' ),  
        'placeholder' => __( 'No restrictions', 'woocommerce' ),  
        'description' => __( 'List of allowed user IDs. Separate user IDs with commas.', 'woocommerce' ),  
        'desc_tip' => true,  
        'type' => 'text',  
    )); 
}
add_action( 'woocommerce_coupon_options_usage_restriction', 'action_woocommerce_coupon_options_usage_restriction', 10, 2 );

// Save
function action_woocommerce_coupon_options_save( $post_id, $coupon ) {
    // Isset
    if ( isset ( $_POST['customer_user_id'] ) ) {
        $coupon->update_meta_data( 'customer_user_id', sanitize_text_field( $_POST['customer_user_id'] ) );
        $coupon->save();
    }
}
add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );

字符串
您可以在其中输入用户ID(s),用逗号分隔。(见所附图像)


的数据
沿着此代码用于优惠券验证:

// Valid
function filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) {
    // Get meta
    $customer_user_id = $coupon->get_meta( 'customer_user_id' );

    // NOT empty
    if ( ! empty( $customer_user_id ) ) {
        // Convert string to array
        $customer_user_id = explode( ', ', $customer_user_id );
    
        // Get current user id
        $user_id = get_current_user_id();
        
        if ( ! in_array( $user_id, $customer_user_id ) ) {
            $valid = false;
            
            if ( ! $valid ) {
                throw new Exception( __( 'My error message', 'woocommerce' ), 109 );
            }
        }
    }

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

3hvapo4f

3hvapo4f2#

没有直接的方法通过用户ID来限制优惠券。但是有一个变通方法,通过添加用户ID和优惠券元数据。

$user_id = get_current_user_id();
update_post_meta( $new_coupon_id, 'couponuserid', $user_id );

字符串
并检查使用优惠券的母鸡。

add_filter( 'woocommerce_coupon_is_valid', function( $is_valid, $coupon ) {

    /**
     * Selected coupons allowed only for selected User IDs
     */
    $is_userid_coupon = get_post_meta( $coupon->get_id(), 'couponuserid', true );
    if ( isset( $is_userid_coupon ) ) {
        $user = wp_get_current_user();
        if ( $user->ID && $user->ID == get_post_meta( $coupon->get_id(), 'couponuserid', true ) ) {
            return true;
        }
    }

    return $is_valid;
}, 100, 2 );

oxalkeyp

oxalkeyp3#

您不需要添加任何自定义字段来按ID过滤,因为WooCommerce已经支持电子邮件限制。所以.

function filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) {
    // Get meta

    $coupon_id            = $coupon->get_id();

    $customer_user_emails = get_post_meta($coupon_id, 'customer_email', true);

    $customer_user_ids    = [];
    foreach ( $customer_user_emails as $customer_user_email ) {
        $customer_user_ids[]       = Users::getUserIdByEmail($customer_user_email);
    }

    // NOT empty
    if ( ! empty( $customer_user_ids ) ) {
                
        // Get current user id
        $user_id = get_current_user_id();
        
        if ( ! in_array( $user_id, $customer_user_ids ) ) {
            $valid = false;
        }
    }

    return $valid;
}

add_filter( 'woocommerce_coupon_is_valid', filter_woocommerce_coupon_is_valid', 10, 3 );

字符串
备注:
基于@7uc1f3r答案
你需要这个

static function getEmailById($id){
     if (!is_numeric($id)){
         throw new \InvalidArgumentException("UID unexpected format");
     }

     $user_data = get_userdata($id);

     if (!$user_data) {                
         throw new \RuntimeException("v");
     }

     $email = $user_data->user_email;

     if (empty($email)) {
         throw new \RuntimeException("User not found");
     }

    return $email;
}

相关问题