No points added when product is disabled for coupons.
-
hello, we have a function that disables certain products from being eligible for coupon discounts.
when a person buys one of this products, no points are added to their account.is there a way to overcome this?
the code used:
1// Create and display the custom field in product general setting tab 2add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_general_product_fields' ); 3function add_custom_field_general_product_fields(){ 4 global $post; 5? 6 echo '<div class="product_custom_field">'; 7? 8 // Custom Product Checkbox Field 9 woocommerce_wp_checkbox( array( 10 'id' => '_disabled_for_coupons', 11 'label' => __('Disabled for coupons', 'woocommerce'), 12 'description' => __('Disable this products from coupon discounts', 'woocommerce'), 13 'desc_tip' => 'true', 14 ) ); 15? 16 echo '</div>';; 17} 18? 19// Save the custom field and update all excluded product Ids in option WP settings 20add_action( 'woocommerce_process_product_meta', 'save_custom_field_general_product_fields', 10, 1 ); 21function save_custom_field_general_product_fields( $post_id ){ 22? 23 $current_disabled = isset( $_POST['_disabled_for_coupons'] ) ? 'yes' : 'no'; 24? 25 $disabled_products = get_option( '_products_disabled_for_coupons' ); 26 if( empty($disabled_products) ) { 27 if( $current_disabled == 'yes' ) 28 $disabled_products = array( $post_id ); 29 } else { 30 if( $current_disabled == 'yes' ) { 31 $disabled_products[] = $post_id; 32 $disabled_products = array_unique( $disabled_products ); 33 } else { 34 if ( ( $key = array_search( $post_id, $disabled_products ) ) !== false ) 35 unset( $disabled_products[$key] ); 36 } 37 } 38? 39 update_post_meta( $post_id, '_disabled_for_coupons', $current_disabled ); 40 update_option( '_products_disabled_for_coupons', $disabled_products ); 41} 42? 43// Make coupons invalid at product level 44add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4); 45function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values ){ 46 if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $valid; 47? 48 $disabled_products = get_option( '_products_disabled_for_coupons' ); 49 if( in_array( $product->get_id(), $disabled_products ) ) 50 $valid = false; 51? 52 return $valid; 53} 54? 55// Set the product discount amount to zero 56add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 ); 57function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon ){ 58 if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $discount; 59? 60 $disabled_products = get_option( '_products_disabled_for_coupons' ); 61 if( in_array( $cart_item['product_id'], $disabled_products ) ) 62 $discount = 0; 63? 64 return $discount; 65}
- The topic ‘No points added when product is disabled for coupons.’ is closed to new replies.