Snippets

Use these snippets to customize the appearance and functionality of WooCommerce Product Bundles.

To use a snippet, download the linked file and activate it as you would with any other plugin. Alternatively, copy the contained code into your child theme’s functions.php file.

Note: We are unable to provide support for customizations under our Support Policy. If you need to customize a snippet, or extend its functionality, seek assistance from a qualified WordPress/WooCommerce Developer. We highly recommend Codeable, or a Certified WooExpert.

Snippets

↑ Back to top

Make optional bundled items checked/selected by default

↑ Back to top

By default, optional bundled item checkboxes are unticked by default. This can be changed with the following snippet:

<?php
/**
* Plugin Name: WooCommerce Product Bundles - Optional Items Checked by Default
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this plugin to have optional bundled items checked/selected by default.
* Version: 1.1
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Manos Psychogyiopoulos
*
* Requires at least: 4.1
* Tested up to: 5.3
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
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;
}

Show an “optional” suffix in optional bundled item titles

↑ Back to top

To add an “- optional” suffix next to the title of bundled items that have been marked as optional, use the following snippet/plugin:

<?php
/**
* Plugin Name: WooCommerce Product Bundles.
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to add an “- optional” suffix next to bundled items' titles
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Jason Kytros
*
* Requires at least: 2.6.0
* Tested up to: 5.7
*
* Copyright: © 2017-2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
"add_filter( 'woocommerce_bundles_optional_bundled_item_add_suffix', '__return_true' );

Calculate discounted bundled item prices over regular prices

↑ Back to top

By default, a bundled item discount can co-exist with a sale price, reducing the final price of a bundled product even further.

Alternatively, you may prefer to ignore sale prices and apply bundled product discounts over the regular prices of bundled products. This is possible with the following snippet:

<?php
/**
* Plugin Name: WooCommerce Product Bundles - Discounts over Regular Prices
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to apply bundled item discounts on regular prices.
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Manos Psychogyiopoulos
*
* Requires at least: 3.8
* Tested up to: 5.3
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// To use this snippet, download this file into your plugins directory and activate it, or copy the code under this line into the functions.php file of your (child) theme.
add_filter( 'woocommerce_bundled_item_discount_from_regular', 'wc_pb_bundled_item_discount_from_regular', 10, 2 );
function wc_pb_bundled_item_discount_from_regular( $from_regular, $bundled_item ) {
return true;
}

Prevent product bundles price strings from showing up in range-format

↑ Back to top

Ensure that you are using the latest version of Product Bundles, then use the following snippet/plugin:

<?php
/**
* Plugin Name: WooCommerce Product Bundles - Prevent Range-Format Prices
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to prevent Product Bundles from displaying bundle prices in range format.
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Manos Psychogyiopoulos
*
* Requires at least: 4.1
* Tested up to: 5.3
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// To use this snippet, download this file into your plugins directory and activate it, or copy the code under this line into the functions.php file of your (child) theme.
add_filter( 'woocommerce_bundle_force_old_style_price_html', '__return_true' );

Change the number of bundled product columns displayed when the Grid Layout option is active

↑ Back to top

The following snippet demonstrates how to adjust the number of bundled item columns to 4:

<?php
/**
* Plugin Name: WooCommerce Product Bundles - Bundled Items Grid Layout column count
* Plugin URI: http://woocommerce.com/products/product-bundles/
* Description: Use this snippet to change the bundled items column count when using the 'Grid' Layout option.
* Version: 1.0
* Author: WooCommerce
* Author URI: http://woocommerce.com
* Developer: Jason Kytros
*
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_filter( 'woocommerce_bundled_items_grid_layout_columns', 'wc_pb_grid_layout_change_number_of_columns', 10, 2 );
function wc_pb_grid_layout_change_number_of_columns( $columns, $bundle ) {
return 4;
}

Change the appearance of item subtotals in the cart/orders

↑ Back to top

When the Grouped selection is active under Item Grouping, the subtotals of parent/child line items in cart/order templates are added together and the actual subtotal of the parent item is replaced by the aggregated subtotal. Additionally, the subtotals of individually-priced items are indented and displayed with a Subtotal: prefix.

To prevent this, use the following snippet/plugin:

<?php
/**
* Plugin Name: WooCommerce Product Bundles - Prevent Subtotals Aggregation
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Prevents Product Bundles from displaying aggregated item subtotals in cart/order templates. Requires v5.5+.
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Manos Psychogyiopoulos
*
* Requires at least: 4.1
* Tested up to: 5.3
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_filter( 'woocommerce_bundles_group_mode_options_data', 'sw_pb_group_mode_options_data' );
function sw_pb_group_mode_options_data( $data ) {
$data[ 'parent' ][ 'features' ] = array( 'parent_item', 'child_item_indent' );
return $data;
}

Hide the remaining stock of the parent item

↑ Back to top

By default, the stock of a Product Bundle is calculated based on the remaining stock of the bundled items.

To calculate the stock of the bundle based on the stock of parent item instead, which is defined under Product Data > Inventory, use the following snippet:

<?php
/**
* Plugin Name: WooCommerce Product Bundles.
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to hide the remaining stock of the parent item
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Jason Kytros
*
* Requires at least: 2.6.0
* Tested up to: 5.7
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_filter( 'woocommerce_bundle_display_bundled_items_stock_quantity', 'wc_sw_hide_bundled_items_stock_quantity' );
function wc_sw_hide_bundled_items_stock_quantity() {
return false;
}

Hide the short description that shows up for each Bundle-Sell

To hide the short description that shows up for each Bundle-Sell, use the following snippet:

<?php
/**
* Plugin Name: WooCommerce Product Bundles.
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to hide the bundled sells short description
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Jason Kytros
*
* Requires at least: 2.6.0
* Tested up to: 5.7
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_filter( 'wc_pb_bundle_sell_data_item_args', 'sw_pb_hide_bundle_sells_description', 10, 3 );
function sw_pb_hide_bundle_sells_description( $args, $bundle_sell_id, $product ) {
$args[ 'meta_data' ][ 'override_description' ] = 'yes';
return $args;
}

Set the Bundle Sells layout to Grid

To change the default Bundle-Sells layout and make them show up in a Grid layout, use the following snippet:

<?php
/**
* Plugin Name: WooCommerce Product Bundles.
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to change the bundle sells layout to grid
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Jason Kytros
*
* Requires at least: 2.6.0
* Tested up to: 5.7
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_filter( 'wc_pb_bundle_sells_dummy_bundle', 'wc_sw_bs_change_layout' );
function wc_sw_bs_change_layout( $bundle ) {
$bundle->set_layout( 'grid' );
return $bundle;
}

Set the Bundle Sells layout to Tabular

To change the default Bundle-Sells layout and make them show up in a Tabular layout, use the following snippet:

* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to change the bundle sells layout to tabular
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Jason Kytros
*
* Requires at least: 2.6.0
* Tested up to: 5.7
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_filter( 'wc_pb_bundle_sells_dummy_bundle', 'wc_sw_bs_change_layout' );
function wc_sw_bs_change_layout( $bundle ) {
$bundle->set_layout( 'tabular' );
return $bundle;
}

Display the bundled products’ full description

By default, bundled items display the short description of the corresponding Simple/Variable products.

To display the full description, use the following snippet:

<?php
/**
* Plugin Name: WooCommerce Product Bundles.
* Plugin URI: https://woocommerce.com/products/product-bundles/
* Description: Use this snippet to display the bundled products' full description
* Version: 1.0
* Author: WooCommerce
* Author URI: https://woocommerce.com/
* Developer: Jason Kytros
*
* Requires at least: 2.6.0
* Tested up to: 5.7
*
* Copyright: © 2021 Automattic.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
add_action( 'init', 'sw_pb_wc_replace_bundled_items_description' );
function sw_pb_wc_replace_bundled_items_description() {
remove_action( 'woocommerce_bundled_item_details', 'wc_pb_template_bundled_item_description', 20, 2 );
add_action( 'woocommerce_bundled_item_details', 'wc_pb_template_bundled_item_full_description', 20, 2 );
}
function wc_pb_template_bundled_item_full_description( $bundled_item, $bundle ) {
wc_get_template( 'single-product/bundled-item-description.php', array(
'description' => $bundled_item->product->get_description()
), false, WC_PB()->plugin_path() . '/templates/' );
}

Display bundles in a grid layout on mobile screens

↑ Back to top

By default, bundled items show up in a list in mobile screens.

However, we understand that in some cases it is preferable to display bundled items on mobile using the same layout as in desktop view. To achieve this, please use the following snippet:

add_filter( 'woocommerce_bundle_front_end_params', 'sw_pb_change_responsive_breakpoint' );
function sw_pb_change_responsive_breakpoint( $frontend_params ) {
	$frontend_params[ 'responsive_breakpoint' ] = 0;
	return $frontend_params;
}

Hide bundles from Upsells if all bundled products are out of stock

↑ Back to top
add_filter( 'woocommerce_product_is_visible', 'wc_hide_insufficient_stock_bundles_from_upsells', 10, 2 );

function wc_hide_insufficient_stock_bundles_from_upsells( $visible, $product_id ) {

	if ( ! $visible ) {
		return $visible;
	}

	$product_type = WC_Data_Store::load( 'product' )->get_product_type( $product_id );

	if ( 'bundle' === $product_type ) {

		$bundle = wc_get_product( $product_id );

		if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $bundle->is_in_stock() ) {
			$visible = false;
		}
	}
	return $visible;
}

Hide out of stock items in a bundle

↑ Back to top

To remove optional out of stock bundled items from a bundle, use the following snippet:

add_filter( 'woocommerce_bundled_items', 'sw_remove_out_of_stock_items', 10, 2 );

function sw_remove_out_of_stock_items( $bundled_items, $bundle ) {
    foreach ( $bundled_items as $key => $bundled_item ) {
        if ( $bundled_item->is_optional() || $bundled_item->get_quantity( 'min' ) == 0 ) {
            if ( 'outofstock' === $bundled_item->product->get_stock_status() ) {
                unset( $bundled_items[ $key ] );
            }
        }
    }
    return $bundled_items;
}

Note: it is not possible to remove mandatory out of stock products as this would trigger an add to cart validation error.

Questions & Support

↑ Back to top

Have a question before you buy? Please fill out this pre-sales form.
Already purchased and need assistance? Get in touch with us via the Help Desk!