Support » Plugin: Code Snippets » Possible to execute filter for all products except a few?

  • Resolved Tom van M

    (@tom-van-m)


    Hello team,

    A question: Is it possible to execute filter for all products except a few?

    I use this on my website

    <?php
    add_filter( 'woocommerce_bundled_item_is_optional_checked', 'wc_pb_is_optional_item_checked', 10, 2 );
    function wc_pb_is_optional_item_checked( $checked, $bundled_item ) {
    	if ( ! isset( $_GET[ 'update-bundle' ] ) ) {
    		$checked = true;
    	}
    	return $checked;
    }

    Some products need to have unchecked bundle products. Can I add for example page id’s to this code to exclude them from being executed?

    Thanks in advance,

    Tom

    • This topic was modified 12 months ago by Tom van M.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi Tom,

    If you have a list of product IDs that you want to exclude from the filter, you can do so like this:

    add_filter( 'woocommerce_bundled_item_is_optional_checked', function ( $checked, $bundled_item ) {
    
    	$exclude = array( 142, 73, 29 );
    
    	if ( in_array( $bundled_item->product_id, $exclude ) ) {
    		return $checked;
    	}
    
    	if ( ! isset( $_GET[ 'update-bundle' ] ) ) {
    		$checked = true;
    	}
    
    	return $checked;
    }, 10, 2 );

    Alternately, if you would prefer to exclude by product slug, you can do so like this:

    
    add_filter( 'woocommerce_bundled_item_is_optional_checked', function ( $checked, $bundled_item ) {
    
    	$exclude = array( 'something', 'another-thing' );
    
    	if ( in_array( $bundled_item->product->get_slug(), $exclude ) ) {
    		return $checked;
    	}
    
    	if ( ! isset( $_GET[ 'update-bundle' ] ) ) {
    		$checked = true;
    	}
    
    	return $checked;
    }, 10, 2 );

    Let me know if you have any other questions!

    Thank you very much for this! I will try it!

    Too bad, that didn’t work

    Plugin Author Shea Bunge

    (@bungeshea)

    I’m sorry to hear that. I’m no expert on WooCommerce myself, so you might have more luck asking this question in a forum specific to that plugin. Generally, any PHP snippet that someone gives you to perform what you’re asking should work if you add it to this plugin.

    The maker of WooCommerce Bundle Products came up with the solution. Thanks anyway!!

    add_filter( 'woocommerce_bundled_item_is_optional_checked', 'wc_pb_is_optional_item_checked', 10, 2 );
    
    function wc_pb_is_optional_item_checked( $checked, $bundled_item ) {
    	
    	global $product;
    	$bundle_id = $product->get_id();
    
    	if ( ! isset( $_GET[ 'update-bundle' ] ) ) {
    		
    		if ( $bundle_id === HERE_ID_OF_PAGE|| $bundle_id === HERE_ID_OF_PAGE) {
    			$checked = false;
    		} else {
    			$checked = true;
    		}
    	}
    
    	return $checked;
    }
    Plugin Author Shea Bunge

    (@bungeshea)

    That’s great to hear you could get it working! Sorry that I couldn’t be much help.

    Not much help? Your plugin makes the implement very easy! 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Possible to execute filter for all products except a few?’ is closed to new replies.