汪汪一只猫
以下代码将一个新字段添加到使用限制选项卡,您可以在其中添加允许的用户角色。// Add new field - usage restriction tabfunction action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) { woocommerce_wp_text_input( array( 'id' => 'customer_user_role', 'label' => __( 'User role restrictions', 'woocommerce' ), 'placeholder' => __( 'No restrictions', 'woocommerce' ), 'description' => __( 'List of allowed user roles. Separate user roles with commas.', 'woocommerce' ), 'desc_tip' => true, 'type' => 'text', )); }add_action( 'woocommerce_coupon_options_usage_restriction', 'action_woocommerce_coupon_options_usage_restriction', 10, 2 );// Savefunction action_woocommerce_coupon_options_save( $post_id, $coupon ) { // Isset if ( isset ( $_POST['customer_user_role'] ) ) { $coupon->update_meta_data( 'customer_user_role', sanitize_text_field( $_POST['customer_user_role'] ) ); $coupon->save(); }}add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );// Validfunction filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) { // Get meta $customer_user_role = $coupon->get_meta('customer_user_role'); // NOT empty if( ! empty( $customer_user_role ) ) { // Convert string to array $customer_user_role = explode( ', ', $customer_user_role ); // Get current user role $user = wp_get_current_user(); $roles = ( array ) $user->roles; // Compare $compare = array_diff( $roles, $customer_user_role ); // NOT empty if ( ! empty ( $compare ) ) { $valid = false; if ( ! $valid ) { throw new Exception( __( 'My custom error message', 'woocommerce' ), 109 ); } } } return $valid;}add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );编辑:在CART页面自动应用优惠券并隐藏删除优惠券链接(基于用户角色)function action_woocommerce_before_calculate_totals( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return; // Only cart if( ! is_cart() ) return; /* SETTINGS */ // Coupon code $coupon_code = 'test'; // Allowed user role $allowed_user_role = 'administrator'; /* END SETTINGS */ // check current user role $user = wp_get_current_user(); $user_roles = ( array ) $user->roles; // ADD js $add_js = false; // In array user roles if ( in_array( $allowed_user_role, $user_roles ) ) { // Format $coupon_code = wc_format_coupon_code( $coupon_code ); // Applied coupons $applied_coupons = $cart->get_applied_coupons(); // Is applied $is_applied = in_array( $coupon_code, $applied_coupons ); // NOT applied if ( ! $is_applied ) { // Apply $cart->apply_coupon( $coupon_code ); // True $add_js = true; } elseif ( $is_applied ) { // True $add_js = true; } // True if ( $add_js ) { ?> <script type="text/javascript"> jQuery( function($) { // Hide remove link $( '.woocommerce-remove-coupon' ).hide(); }); </script> <?php } }}add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );