• Resolved OC WordPress Web Designer

    (@oc-wordpress-web-designer)


    How can I exclude a specific product from any coupon code discount applying? I know you can exclude product from each coupon code, but I have a ton and want to exclude from the product side, not the coupon side.

Viewing 15 replies - 1 through 15 (of 20 total)
  • Hi @oc-wordpress-web-designer

    Here https://stackoverflow.com/questions/47598528/exclude-product-from-all-coupons-in-woocommerce do you have the code to do this.

    Tell me if this works for you ok?

    Regards..

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    Yes I tried that, but get the error below:

    Warning: count(): Parameter must be an array or an object that implements Countable in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 511

    Warning: in_array() expects parameter 2 to be array, bool given in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 514

    Warning: count(): Parameter must be an array or an object that implements Countable in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 511

    Warning: in_array() expects parameter 2 to be array, bool given in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 514

    Warning: count(): Parameter must be an array or an object that implements Countable in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 511

    Warning: in_array() expects parameter 2 to be array, bool given in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 514

    Warning: count(): Parameter must be an array or an object that implements Countable in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 523

    Warning: in_array() expects parameter 2 to be array, bool given in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 526

    Warning: count(): Parameter must be an array or an object that implements Countable in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 523

    Warning: in_array() expects parameter 2 to be array, bool given in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 526

    Warning: count(): Parameter must be an array or an object that implements Countable in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 511

    Warning: in_array() expects parameter 2 to be array, bool given in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 514

    Warning: count(): Parameter must be an array or an object that implements Countable in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 511

    Warning: in_array() expects parameter 2 to be array, bool given in /home/ivivisio/public_html/wp-content/themes/x-child/functions.php on line 514

    —–
    Line 511 in my site is:

    if( ! count(get_option( ‘_products_disabled_for_coupons’ )) > 0 ) return $valid;

    Line 514 in my site is:

    if( in_array( $product->get_id(), $disabled_products ) )

    Line 523 in my site is:

    if( ! count(get_option( ‘_products_disabled_for_coupons’ )) > 0 ) return $discount;

    Line 526 in my site is:

    if( in_array( $cart_item[‘product_id’], $disabled_products ) )

    Hi @oc-wordpress-web-designer

    I have tested on a demo installation and it works fine for me.

    Do you have the last versión of WordPress and WooCommerce?

    Which version of PHP do you have?

    It seems that the problem is in the PHP version but check all of this, please.

    Regards.

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    PHP 7.4.4
    Wordpress 5.4.1
    Woocommerce 4.0.1

    Ok, I think that the problem is in the PHP versión.

    I have updated to PHP 7.4.1 and it works fine for me.

    One moment…..

    Revise the ‘ it seems are different. Revise, please…

    Here is the code:

    // Create and display the custom field in product general setting tab
    add_action( ‘woocommerce_product_options_general_product_data’, ‘add_custom_field_general_product_fields’ );
    function add_custom_field_general_product_fields(){
    global $post;

    echo ‘<div class=”product_custom_field”>’;

    // Custom Product Checkbox Field
    woocommerce_wp_checkbox( array(
    ‘id’ => ‘_disabled_for_coupons’,
    ‘label’ => __(‘Disabled for coupons’, ‘woocommerce’),
    ‘description’ => __(‘Disable this products from coupon discounts’, ‘woocommerce’),
    ‘desc_tip’ => ‘true’,
    ) );

    echo ‘</div>’;;
    }

    // Save the custom field and update all excluded product Ids in option WP settings
    add_action( 'woocommerce_process_product_meta', 'save_custom_field_general_product_fields', 10, 1 );
    function save_custom_field_general_product_fields( $post_id ){
    
        $current_disabled = isset( $_POST['_disabled_for_coupons'] ) ? 'yes' : 'no';
    
        $disabled_products = get_option( '_products_disabled_for_coupons' );
        if( empty($disabled_products) ) {
            if( $current_disabled == 'yes' )
                $disabled_products = array( $post_id );
        } else {
            if( $current_disabled == 'yes' ) {
                $disabled_products[] = $post_id;
                $disabled_products = array_unique( $disabled_products );
            } else {
                if ( ( $key = array_search( $post_id, $disabled_products ) ) !== false )
                    unset( $disabled_products[$key] );
            }
        }
    
        update_post_meta( $post_id, '_disabled_for_coupons', $current_disabled );
        update_option( '_products_disabled_for_coupons', $disabled_products );
    }
    
    // Make coupons invalid at product level
    add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4);
    function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values ){
        if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $valid;
    
        $disabled_products = get_option( '_products_disabled_for_coupons' );
        if( in_array( $product->get_id(), $disabled_products ) )
            $valid = false;
    
        return $valid;
    }
    
    // Set the product discount amount to zero
    add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 );
    function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon ){
        if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $discount;
    
        $disabled_products = get_option( '_products_disabled_for_coupons' );
        if( in_array( $cart_item['product_id'], $disabled_products ) )
            $discount = 0;
    
        return $discount;
    }

    Tell me, please.

    Regards.

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    Same error….

    Hmmmmmmm…..

    Can you paste the code here, please?

    And please, review the ‘ becouse it seems that you have ′

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    
    // Create and display the custom field in product general setting tab
    add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_general_product_fields' );
    function add_custom_field_general_product_fields(){
        global $post;
    
        echo '<div class="product_custom_field">';
    
        // Custom Product Checkbox Field
        woocommerce_wp_checkbox( array(
            'id'        => '_disabled_for_coupons',
            'label'     => __('Disabled for coupons', 'woocommerce'),
            'description' => __('Disable this products from coupon discounts', 'woocommerce'),
            'desc_tip'  => 'true',
        ) );
    
        echo '</div>';;
    }
    
    // Save the custom field and update all excluded product Ids in option WP settings
    add_action( 'woocommerce_process_product_meta', 'save_custom_field_general_product_fields', 10, 1 );
    function save_custom_field_general_product_fields( $post_id ){
    
        $current_disabled = isset( $_POST['_disabled_for_coupons'] ) ? 'yes' : 'no';
    
        $disabled_products = get_option( '_products_disabled_for_coupons' );
        if( empty($disabled_products) ) {
            if( $current_disabled == 'yes' )
                $disabled_products = array( $post_id );
        } else {
            if( $current_disabled == 'yes' ) {
                $disabled_products[] = $post_id;
                $disabled_products = array_unique( $disabled_products );
            } else {
                if ( ( $key = array_search( $post_id, $disabled_products ) ) !== false )
                    unset( $disabled_products[$key] );
            }
        }
    
        update_post_meta( $post_id, '_disabled_for_coupons', $current_disabled );
        update_option( '_products_disabled_for_coupons', $disabled_products );
    }
    
    // Make coupons invalid at product level
    add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4);
    function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values ){
        if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $valid;
    
        $disabled_products = get_option( '_products_disabled_for_coupons' );
        if( in_array( $product->get_id(), $disabled_products ) )
            $valid = false;
    
        return $valid;
    }
    
    // Set the product discount amount to zero
    add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 );
    function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon ){
        if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $discount;
    
        $disabled_products = get_option( '_products_disabled_for_coupons' );
        if( in_array( $cart_item['product_id'], $disabled_products ) )
            $discount = 0;
    
        return $discount;
    }
    
    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    I have Theme X (https://themeforest.net/item/x-the-theme/5871901) active and when I deactivated it to a default theme, the issue was removed.

    Appears I need to talk to theme support.

    Yes but you need to add this code to the default theme functions.php to ensure.

    Can you do that?

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    Oh duh, yes the issue still appears after the code is added to the default theme function.php so its not a theme conflict.

    Do you review the ””” and the ′′′′′′ ?

    It seems that when you paste the code these ”” are trasnformed to ′′′′′′′

    Thread Starter OC WordPress Web Designer

    (@oc-wordpress-web-designer)

    I’m copying to a text editor in plain text and pasting in the function.php in code editor mode. The code is being validated with the correct ‘

    I’m sorry but I can’t reproduce your issue.

    For me it works fine and I have the last version of WordPress and the last version of WooCommerce.

    Regards.

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Exclude Product From All Coupons’ is closed to new replies.