• Resolved jamesjohnsonw

    (@jamesjohnsonw)


    Hi there,

    Is there any way of creating an exclusive product that is only available in a bundle and is not accessible any other way?

    I have made a product and added it to a ‘Bundle Exclusives’ category.

    I have hidden the category with:

    // DISABLE PURCHASE
    
    add_filter( 'woocommerce_add_to_cart_validation', 'woosb_prevent_exclusive_products', 10, 2 );
    function woosb_prevent_exclusive_products( $passed, $product_id ) {
    	$exclusive_products = array( 6845 ); // fill your exclusive products ID here
    
    	if ( in_array( $product_id, $exclusive_products, false ) ) {
    		wc_add_notice( esc_html__( 'This is an exclusive product only available in bundles!', 'woo-product-bundle' ), 'error' );
    
    		return false;
    	}
    
    	return $passed;
    }
    
    // DISABLE ADD TO CART BUTTON, PRICE, SOCIAL SHARE AND CATEGORY LIST ON BUNDLE EXCLUSIVE CATEGORY
    
    add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
    function remove_product_description_add_cart_button() { // function for deleting ...
        // Set category ID
        $categories = array('bundle-exclusives');
    
        //Feature removal
        if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
        }
    }
    
    // HIDE BUNDLE EXCLUSIVE CATEGORY
    
    add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );
    function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
    	$new_terms = array();
    
    		if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() &&is_shop() ) {
    		foreach( $terms as $key => $term ) {
    		if ( !in_array( $term->slug, array( 'bundle-exclusives', 'uncategorized' ) ) ) { //pass the slug name here
    	
    	$new_terms[] = $term;
    	}}
    	$terms = $new_terms;
    	}
    
    return $terms;
    }

    and, have changed the display from £0 to Free! with:

    function wpglorify_price_free_zero_empty( $price, $product ) {
    
    	if ( $product->get_price() == 0 ) {
    
    		if ( $product->is_on_sale() && $product->get_regular_price() ) {
    
    			$regular_price = wc_get_price_to_display( $product, array( 'qty' => 1, 'price' => $product->get_regular_price() ) );
    
    			$price = wc_format_price_range( $regular_price, __( 'Free!', 'woocommerce' ) );
    
    		} else {
    
    			$price = '<span class="amount">' . __( 'Free!', 'woocommerce' ) . '</span>';
    
    		}
    
    	}
    
    	return $price;
    
    }
    
    add_filter( 'woocommerce_get_price_html', 'wpglorify_price_free_zero_empty', 10, 2 );

    Is there any way of hiding the exclusive product from the store? It appears on the search and shop pages. You cannot change the visibility to ‘hidden’ on the product because then, it doesn’t allow users to purchase the product.

    /JJ

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter jamesjohnsonw

    (@jamesjohnsonw)

    Ticket moved to WooCommerce.

    I’ll reply with resolution here.

    Thread Starter jamesjohnsonw

    (@jamesjohnsonw)

    Resolved

    If you are looking to create exclusive products you can use the following code to create hidden products which are purchasable through bundles:

    // DISABLE PURCHASE
    
    add_filter( 'woocommerce_add_to_cart_validation', 'woosb_prevent_exclusive_products', 10, 2 );
    function woosb_prevent_exclusive_products( $passed, $product_id ) {
    	$exclusive_products = array( 6292, 6291, 6290, 6286, 6285, 6284, 6279, 6278, 6277 ); // fill your exclusive products ID here
    
    	if ( in_array( $product_id, $exclusive_products, false ) ) {
    		wc_add_notice( esc_html__( 'This is an exclusive product only available in bundles!', 'woo-product-bundle' ), 'error' );
    
    		return false;
    	}
    
    	return $passed;
    }
    
    // DISABLE ADD TO CART BUTTON, PRICE, SOCIAL SHARE AND CATEGORY LIST ON BUNDLE EXCLUSIVE CATEGORY
    
    add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
    function remove_product_description_add_cart_button() { // function for deleting ...
        // Set category ID
        $categories = array('bundle-exclusives');
    
        //Feature removal
        if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
        }
    }
    
    // HIDE BUNDLE EXCLUSIVE CATEGORY
    
    add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );
    function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
    	$new_terms = array();
    
    		if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() &&is_shop() ) {
    		foreach( $terms as $key => $term ) {
    		if ( !in_array( $term->slug, array( 'bundle-exclusives', 'uncategorized' ) ) ) { //pass the slug name here
    	
    	$new_terms[] = $term;
    	}}
    	$terms = $new_terms;
    	}
    
    return $terms;
    }
    
    // HIDE CATEGORY PRODUCTS FROM SHOP
    
    function custom_pre_get_posts_query( $q ) {
    
        $tax_query = (array) $q->get( 'tax_query' );
    
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'bundle-exclusives' ), // Don't display products in the bundle-exclusives category on the shop page.
               'operator' => 'NOT IN'
        );
    
        $q->set( 'tax_query', $tax_query );
    
    }
    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );  

    Hi @jamesjohnsonw,

    Thank you for your solution! It’s definitely very useful for other users.

    Best regards,

    Thread Starter jamesjohnsonw

    (@jamesjohnsonw)

    Thanks, @miemie

    On top of this fix, you can use the following code to make the exclusive product price read FREE instead of £0.

    add_filter( 'woocommerce_get_price_html', 'bbloomer_price_free_zero_empty', 9999, 2 );
       
    function bbloomer_price_free_zero_empty( $price, $product ){
        if ( '' === $product->get_price() || 0 == $product->get_price() ) {
            $price = '<span class="woocommerce-Price-amount amount">FREE</span>';
        }  
        return $price;
    }

    More information on the Exclusive Products code. In this example, I have created a category called ‘Bundle Exclusives’ which has the slug name ‘bundle-exclusives’. Any exclusive products need to be added to this category (which is hidden) and set to ‘Shop Only’ visibility. The product will be hidden but still purchasable through a bundle.

    Here is the exclusive products code once again for reference

    // DISABLE PURCHASE
    
    add_filter( 'woocommerce_add_to_cart_validation', 'woosb_prevent_exclusive_products', 10, 2 );
    function woosb_prevent_exclusive_products( $passed, $product_id ) {
    	$exclusive_products = array( 6292, 6291, 6290, 6286, 6285, 6284, 6279, 6278, 6277 ); // fill your exclusive products ID here
    
    	if ( in_array( $product_id, $exclusive_products, false ) ) {
    		wc_add_notice( esc_html__( 'This is an exclusive product only available in bundles!', 'woo-product-bundle' ), 'error' );
    
    		return false;
    	}
    
    	return $passed;
    }
    
    // DISABLE ADD TO CART BUTTON, PRICE, SOCIAL SHARE AND CATEGORY LIST ON BUNDLE EXCLUSIVE CATEGORY
    
    add_action('woocommerce_single_product_summary', 'remove_product_description_add_cart_button', 1 );
    function remove_product_description_add_cart_button() { // function for deleting ...
        // Set category ID
        $categories = array('bundle-exclusives');
    
        //Feature removal
        if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
    		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
        }
    }
    
    // HIDE BUNDLE EXCLUSIVE CATEGORY
    
    add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );
    function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
    	$new_terms = array();
    
    		if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() &&is_shop() ) {
    		foreach( $terms as $key => $term ) {
    		if ( !in_array( $term->slug, array( 'bundle-exclusives', 'uncategorized' ) ) ) { //pass the slug name here
    	
    	$new_terms[] = $term;
    	}}
    	$terms = $new_terms;
    	}
    
    return $terms;
    }
    
    // HIDE CATEGORY PRODUCTS FROM SHOP
    
    function custom_pre_get_posts_query( $q ) {
    
        $tax_query = (array) $q->get( 'tax_query' );
    
        $tax_query[] = array(
               'taxonomy' => 'product_cat',
               'field' => 'slug',
               'terms' => array( 'bundle-exclusives' ), // Don't display products in the bundle-exclusives category on the shop page.
               'operator' => 'NOT IN'
        );
    
        $q->set( 'tax_query', $tax_query );
    
    }
    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );  

    You can see what this might look like here: https://prnt.sc/1hlwpt5 Outlined in red is our exclusive product that is only available in this bundle.

    /JJ

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Creating Exclusive Products’ is closed to new replies.