In this document, you will find many useful snippets for customizing the appearance and functionality of Composite Products.
To use a snippet, you can download the linked file and activate it as you would do with any other plugin. Alternatively, you can 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 Woo Agency Partner.
Components & Component Options
↑ Back to topChange the default component options sort order
↑ Back to topTo change the default sort order of Component Options, you can use the woocommerce_composite_component_default_orderby filter.
For instance, to change the default Component Options sorting option from “Default sorting” to “Sort by price”, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Sort Component Options by Price | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to change the default Component Options sorting option from "Default sorting" to "Sort by price". | |
| * 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_composite_component_default_orderby', 'wc_cp_sort_by_price', 10, 3 ); | |
| function wc_cp_sort_by_price( $default_sort_function, $component_id, $composite ) { | |
| return 'price'; | |
| } | |
By default, if you use the “Select Products” method to enter Component Options, the Default sorting option will sort products in the order they have been entered in the Component Options field.
To make the Default sorting option operate based on menu_order instead, use the following snippet:
Similarly, to make the Default sorting option operate based on the product title, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Sort Component Options by Product Title | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to make the "default" Component Options sorting option operate based on "title". | |
| * 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_composite_component_orderby', 'wc_cp_composite_component_orderby', 10, 3 ); | |
| function wc_cp_composite_component_orderby( $order_by_options, $component_id, $composite ) { | |
| return array_merge( array( 'title-asc' => __( 'Default sorting', 'woocommerce' ) ), $order_by_options ); | |
| } | |
| add_filter( 'woocommerce_composite_component_default_orderby', 'wc_cp_composite_component_default_orderby', 10, 3 ); | |
| function wc_cp_composite_component_default_orderby( $default_order_by, $component_id, $composite ) { | |
| return 'title-asc'; | |
| } |
Change the component options thumbnail columns count
↑ Back to topThe number of Component Options columns when using the Thumbnails setting can be changed by using the woocommerce_composite_component_loop_columns filter. The following snippet is a simple example of setting the product thumbnails column count to 2:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Component Options column count | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to change the Component Options column count when using the Product Thumbnails setting. | |
| * 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_composite_component_loop_columns', 'wc_cp_component_loop_columns', 10, 3 ); | |
| function wc_cp_component_loop_columns( $cols, $component_id, $composite ) { | |
| return 2; | |
| } |
Change the component options thumbnails per page
↑ Back to topThe number of Component Options loaded per-page when using the Thumbnails setting can be changed by using the woocommerce_component_options_per_page filter. The following snippet is used to set the results count to 12:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Component Options results count | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to change the number of Component Option results returned per-page when using the Product Thumbnails setting. | |
| * 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_component_options_per_page', 'wc_cp_component_options_per_page', 10, 3 ); | |
| function wc_cp_component_options_per_page( $results_count, $component_id, $composite ) { | |
| return 12; | |
| } |
Grey-out disabled component options when using Scenarios
↑ Back to topWhen using Scenarios to Hide Component Options, incompatible products and variations are hidden by default.
To grey-out, instead of hiding them, you can use the woocommerce_component_options_hide_incompatible filter, as in the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Grey out Component Options Disabled by Scenarios | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to have incompatible selections greyed-out, instead of completely hidden, when using Scenarios. | |
| * 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_component_options_hide_incompatible', 'wc_cp_component_options_hide_incompatible', 10, 3 ); | |
| function wc_cp_component_options_hide_incompatible( $hide, $component_id, $composite ) { | |
| return false; | |
| } |
Enable/disable the toggle-box component view
↑ Back to topBy default, Components are displayed in toggle-boxes when using the Progressive layout.
This behavior can be changed by using the woocommerce_composite_component_toggled filter. For instance, you can use the filter to disable the toggle-box view completely:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Disable the toggle-box Component view | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to disable the toggle-box Component view when using the Progressive layout. | |
| * 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_composite_component_toggled', '__return_false', 10 ); |
Alternatively, you may want to enable the toggle-box view when using the Stacked layout only:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Enable the toggle-box Component view with the Stacked layout | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to enable the toggle-box Component view when using the Stacked layout only. | |
| * 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_composite_component_toggled', 'wc_cp_component_toggled', 10, 3 ); | |
| function wc_cp_component_toggled( $toggled, $component_id, $composite ) { | |
| $style = $composite->get_composite_layout_style(); | |
| return $style === 'single'; | |
| } |
Require Components to be configured in sequence when using the Componentized layout
↑ Back to topBy default, the Componentized layout does not require Components to be configured in sequence. Using this snippet will imitate the Stepped layout behavior, where access to a Component is possible only if all preceding Components have been configured.
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Sequential Componentized Configuration | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to enforce a sequential configuration of Components when using the Componentized layout. | |
| * 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_composite_sequential_comp_progress', 'wc_cp_sequential_componentized_progress', 10, 2 ); | |
| function wc_cp_sequential_componentized_progress( $seq, $composite ) { | |
| return 'yes'; | |
| } |
Add a permalink to the selected component option’s product page
↑ Back to topTo add a permalink to the selected component option section that redirects to the product page, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to add a permalink to the selected component option's product page | |
| * 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 | |
| */ | |
| add_action( 'woocommerce_composited_product_details', 'sw_cp_display_selected_product_permalink', 10, 3 ); | |
| function sw_cp_display_selected_product_permalink( $product, $component_id, $composite_product ) { | |
| echo '<a href="'. $product->get_permalink() . '">View Product</a>'; | |
| } |
Always display the “Choose an option” placeholder dropdown option and the “Clear Selection” button
↑ Back to topWhen you use the Dropdown style without defining a Default Option, a “Choose an option” placeholder option is added to the top of the drop-down menu. This placeholder option is omitted when a Default Option is defined unless the Component is Optional.
The visibility of the Clear Selection button also complies with the same rule — when a Default Option is defined, the Clear Selection button is not visible, unless the Component is Optional.
To override this behavior, use the following snippet:
Make the selected component option show up at the top of the component
↑ Back to topWhen you use the Load More pagination option, the selected component option shows up between component options.
To make the selected component option show up at the top of the component instead, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to make the selected component option show up at the top of the component. | |
| * 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 | |
| */ | |
| add_filter( 'woocommerce_component_option_details_relocation_mode', 'sw_cp_disable_relocation' ); | |
| function sw_cp_disable_relocation( $type ) { | |
| return 'off'; | |
| } |
Hide out of stock component options
↑ Back to topTo hide out of stock products from showing up inside components:
- Navigate to WooCommerce > Settings > Products > Inventory.
- Check the Hide out of stock items from the catalog.
When this option is enabled, out of stock products are hidden in Shop Archives as well. To hide out of stock products inside components only, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to hide out of stock component options | |
| * 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 | |
| */ | |
| add_filter( 'woocommerce_composite_component_options_query_args_current', 'sw_cp_exclude_out_of_stock_options' ); | |
| function sw_cp_exclude_out_of_stock_options( $args ) { | |
| $args[ 'exclude_out_of_stock' ] = true; | |
| return $args; | |
| } |
Hide terms with no matching products when option filtering is enabled on a component
↑ Back to topWhen you enable the Options Filtering option in a component, a field shows up where you can select some product attributes. Then, when you view the Composite Product, these attributes show up in the filters container together with all the terms they include.

Composite Products does not perform any checks to make sure that at least one product exists for each term. This is because enforcing these checks would have a significant performance impact on the page.
In the above example, it is possible that there aren’t any Blue products in the component. However, the Blue term still shows up. If clicked, it returns an empty products set.
To remove the terms that do not have any matching products, use the following snippet:
| <?php | |
| /* | |
| When you enable the Options Filtering option in a component, a field shows up where you can select some product attributes. | |
| The selected attributes and their terms are shown on the front-end to be used as filters. | |
| Composite Products does not perform any checks to make sure that at least one product exists for each term. | |
| This is because enforcing these checks would have a significant performance impact on the page. | |
| Use this snippet to automatically remove any terms that do not have any matching products. | |
| */ | |
| add_filter( 'woocommerce_composite_component_filters', 'sw_cp_disable_empty_filters', 10, 3 ); | |
| function sw_cp_disable_empty_filters ( $filters, $component_id, $composite ) { | |
| foreach ( $filters as $key => $value ) { | |
| if ( 'Color' == $value[ 'filter_name' ] ) { | |
| unset( $filters[ $key ][ 'filter_options' ][26] ); | |
| } | |
| } | |
| return $filters; | |
| } |
To identify the correct term IDs in your case, please follow these steps:
- Navigate to Products > Attributes.
- Select a specific attribute.
- Click the Configure terms button.
- Select a term and check its URL for the ID.
Automatically select the remaining available variation in a component
↑ Back to topIf you have set up Scenarios that exclude all but one variation in a specific component and you’d like to select it by default, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to automatically select the remaining available variation in a component | |
| * 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_composite_component_classes', 'wc_cp_autoselect_attributes', 10, 3 ); | |
| function wc_cp_autoselect_attributes( $classes, $component_id, $composite ) { | |
| $classes[] = 'autoselect_attributes'; | |
| return $classes; | |
| } |
Set a default quantity for a component
↑ Back to topTo set a default quantity for a component, use the following snippet:
Before using this snippet, replace 1234 with the ID of the component for which you’d like to set a default quantity. You can find the ID of each component by:
- Navigating to Product Data > Components and;
- Hovering over a specific component.
| <?php | |
| /* | |
| Use this snippet to set a default quantity for a component of a composite product. | |
| Before using this snippet, replace 1234 with the ID of the component for which you’d like to set a default quantity. | |
| You can find the ID of each component by: navigating to Product Data > Components and hovering over a specific component | |
| */ | |
| add_filter( 'woocommerce_composited_product_quantity', 'sw_wc_cp_set_default_quantity', 10, 6 ); | |
| function sw_wc_cp_set_default_quantity( $default_quantity, $quantity_min, $quantity_max, $product, $component_id, $composite_product ) { | |
| if ( $component_id === '1234' ) { | |
| $default_quantity = 5; | |
| } | |
| return $default_quantity; | |
| } |
Composite Summary/Review
↑ Back to topChange the number of columns in the summary/review section
↑ Back to topTo change the max number of component columns in the Summary/Review section, you can use the woocommerce_composite_component_summary_max_columns filter. The following snippet is a simple example of setting the Summary/Review section column count to 3:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Max number of Component columns in the Summary/Review section | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to change the max number of Component columns in the Summary/Review section. | |
| * 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_composite_component_summary_max_columns', 'wc_cp_summary_max_columns', 10, 2 ); | |
| function wc_cp_summary_max_columns( $cols, $composite ) { | |
| return 3; | |
| } |
Display the configuration summary widget in the review/summary step
↑ Back to topWhen you use the Stepped or Componentized layout, the configuration summary widget is hidden in the review/summary step, as it is already displayed inside the step’s content.
To display the configuration summary widget in the review/summary step, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Disable the toggle-box Component view | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to display the configuration summary widget in the review/summary step | |
| * Version: 1.0 | |
| * Author: WooCommerce | |
| * Author URI: https://woocommerce.com/ | |
| * Developer: Manos Psychogyiopoulos | |
| * | |
| * Requires at least: 3.8 | |
| * Tested up to: 5.3 | |
| * | |
| * Copyright: © 2017-2020 Automattic. | |
| * License: GNU General Public License v3.0 | |
| * License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
| */ | |
| add_filter( 'woocommerce_composite_add_to_cart_form_settings', 'sw_wc_cp_keep_widget_visible', 10, 2 ); | |
| function sw_wc_cp_keep_widget_visible ( $settings, $composite ) { | |
| $settings[ 'show_widget_in_review_step' ] = 'yes'; | |
| return $settings; | |
| } |
Pricing
↑ Back to topPrevent composite products with component discounts from being listed as on-sale
↑ Back to topBy default, composite products with component discounts are listed as On Sale. Use the following snippet to prevent this:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Sale Status Tweak | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Prevents Composite Products with component discounts from appearing as on-sale. | |
| * 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_product_is_on_sale', 'wc_cp_component_discounts_sale_status', 10, 2 ); | |
| function wc_cp_component_discounts_sale_status( $on_sale, $product ) { | |
| if ( $on_sale && 'composite' === $product->get_type() && $product->contains( 'priced_individually' ) ) { | |
| $product = new WC_Product_Simple( $product->get_id() ); | |
| $on_sale = $product->is_on_sale(); | |
| } | |
| return $on_sale; | |
| } |
Calculate discounted component prices over regular prices
↑ Back to topBy default, Component discounts reduce the final price of on-sale Component Options even further.
Alternatively, you may prefer to ignore sale prices and always apply Component discounts over the regular prices of the selected Component Options. This is possible using the woocommerce_composited_product_discount_from_regular filter:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Discounts over Regular Prices | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to allow component-level discounts over 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_composited_product_discount_from_regular', 'wc_cp_composited_product_discount_from_regular', 10, 3 ); | |
| function wc_cp_composited_product_discount_from_regular( $from_regular, $component_id, $composite_id ) { | |
| return true; | |
| } |
Prevent composite price strings from showing up in range-format
↑ Back to topFirst, make sure that you are using the latest version of Composite Products. Then, activate the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Prevent Range-Format Prices | |
| * Plugin URI: http://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to prevent Composite Products from displaying composite prices in range format. | |
| * Version: 1.0 | |
| * Author: WooCommerce | |
| * Author URI: http://woocommerce.com/ | |
| * Developer: Manos Psychogyiopoulos | |
| * | |
| * Requires at least: 4.1 | |
| * Tested up to: 4.8 | |
| * | |
| * 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_composite_force_old_style_price_html', '__return_true' ); |
Hide/replace the “Free!” Component Option price strings that show up when a Component is Priced Individually
↑ Back to topTo hide “Free!” price strings from drop-downs and selected product details, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Hide "Free!" Price Strings | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Hide "Free!" price strings from drop-downs and selected product details when the "Per-Item Pricing" option is checked. | |
| * 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_free_price_html', 'wc_cp_composited_product_free_price_html', 10, 2 ); | |
| function wc_cp_composited_product_free_price_html( $price, $product ) { | |
| if ( did_action( 'woocommerce_composite_products_apply_product_filters' ) > did_action( 'woocommerce_composite_products_remove_product_filters' ) ) { | |
| $price = ''; // wc_price( 0.0 ); | |
| } | |
| return $price; | |
| } | |
| add_filter( 'woocommerce_composited_product_price_string_inner', 'wc_cp_composited_product_free_price_string_inner', 10, 10 ); | |
| function wc_cp_composited_product_free_price_string_inner( $price_string_inner, $price_string, $qty_suffix, $suffix, $price, $is_range, $has_multiple, $product_id, $component_id, $composited_product ) { | |
| if ( $price == 0 ) { | |
| $price_string_inner = ''; // sprintf( __( '%1$s %2$s %3$s', 'dropdown price followed by per unit suffix and discount suffix', 'woocommerce-composite-products' ), WC_CP()->api->get_composited_item_price_string_price( $price ), $qty_suffix, $suffix ); | |
| } | |
| return $price_string_inner; | |
| } |
Replace the displayed price string of a Composite product with your own custom string
↑ Back to topUse this snippet to define your own custom price strings for Composite products – useful if you have activated the Hide Price option to reduce server load:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Custom Composite Price Strings | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to define your own custom price strings for Composite products, for instance when the "Hide Price" option is checked to reduce server load. | |
| * 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_get_price_html', 'wc_cp_custom_price_html', 10, 2 ); | |
| add_action( 'woocommerce_product_options_general_product_data', 'wc_cp_composite_custom_price_html', 11 ); | |
| add_action( 'woocommerce_admin_process_product_object', 'wc_cp_process_custom_composite_price_string' ); | |
| function wc_cp_custom_price_html( $price_html, $product ) { | |
| if ( $product->is_type( 'composite' ) && ( $custom_price_string = $product->get_meta( '_custom_composite_price_html', true ) ) ) { | |
| $price_html = $custom_price_string; | |
| } | |
| return $price_html; | |
| } | |
| function wc_cp_composite_custom_price_html() { | |
| echo '<div class="options_group show_if_composite">'; | |
| woocommerce_wp_text_input( array( 'id' => '_custom_composite_price_html', 'class' => 'short', 'label' => __( 'Custom Price String', 'woocommerce-composite-products' ), 'description' => __( 'Enter a custom Price String to override the auto-generated Composite product price displayed in the shop.', 'woocommerce-composite-products' ), 'desc_tip' => true ) ); | |
| echo '</div>'; | |
| } | |
| function wc_cp_process_custom_composite_price_string( $product ) { | |
| if ( ! empty( $_POST[ '_custom_composite_price_html' ] ) ) { | |
| $product->update_meta_data( '_custom_composite_price_html', stripslashes( $_POST[ '_custom_composite_price_html' ] ) ); | |
| } else { | |
| $product->delete_meta_data( '_custom_composite_price_html' ); | |
| } | |
| } |
The snippet will add a new “Custom Price” field under the Product Data > General tab, which you can use to define your own custom price string.
Display a “Total” string before the total composite price
↑ Back to topTo display a Total string before the total Composite Price, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to display a "Total" string before total Composite Price | |
| * Version: 1.0 | |
| * Author: WooCommerce | |
| * Author URI: https://woocommerce.com/ | |
| * Developer: Manos Psychogyiopoulos | |
| * | |
| * Requires at least: 3.8 | |
| * Tested up to: 5.3 | |
| * | |
| * Copyright: © 2017-2020 Automattic. | |
| * License: GNU General Public License v3.0 | |
| * License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
| */ | |
| add_filter( 'woocommerce_composite_front_end_params', 'sw_cp_display_total_string' ); | |
| function sw_cp_display_total_string( $frontend_params ) { | |
| $frontend_params[ 'i18n_price_format' ] = sprintf( _x( '%1$s%2$s%3$s', '"Total" string followed by price followed by price suffix', 'woocommerce-composite-products' ), '%t', '%p', '%s' ); | |
| return $frontend_params; | |
| } |
Hide the total composite price when the configuration is invalid
↑ Back to topBy default, the total Composite price shows up even if the components configuration is invalid (for example, if an out of stock product is selected in a component). To prevent the total Composite price from showing up when the components configuration is invalid, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to hide the total Composite price when the components configuration is invalid | |
| * Version: 1.0 | |
| * Author: WooCommerce | |
| * Author URI: https://woocommerce.com/ | |
| * Developer: Manos Psychogyiopoulos | |
| * | |
| * Requires at least: 3.8 | |
| * Tested up to: 5.3 | |
| * | |
| * Copyright: © 2017-2020 Automattic. | |
| * License: GNU General Public License v3.0 | |
| * License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
| */ | |
| add_filter( 'woocommerce_composite_add_to_cart_form_settings', 'sw_wc_cp_hide_price_for_incompatible_configs', 10, 2 ); | |
| function sw_wc_cp_hide_price_for_incompatible_configs ( $settings, $composite ) { | |
| $settings[ 'hide_total_on_validation_fail' ] = 'yes'; | |
| return $settings; | |
| } |
Calculate a components’ Relative Price based on the selected option
↑ Back to topBy default, the a component’s Relative price is calculated based on the default option. To calculate components Relative Price based on the selected option, use the following snippet:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to calculate a components' Relative Price based on the selected option | |
| * 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_composite_add_to_cart_form_settings', 'disable_relative_to_default', 10, 2 ); | |
| function disable_relative_to_default( $settings, $composite ) { | |
| foreach ( $settings[ 'price_display_data' ] as $component => $value ) { | |
| if ( 'relative' === $settings[ 'price_display_data' ][ $component ][ 'format' ] ) { | |
| $settings[ 'price_display_data' ][ $component ][ 'is_relative_to_default' ] = 'no'; | |
| } | |
| } | |
| return $settings; | |
| } |
Cart & Orders
↑ Back to topHide components in the cart, checkout, order and e-mail templates
↑ Back to topThe snippet will hide all line items associated with Components from the cart, checkout, order and e-mail templates. When the snippet is active, customers will see only the parent line item associated with the Composite product:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Hide Components in all Templates | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Use this snippet to hide Components in the cart, checkout, order and e-mail templates. | |
| * 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_order_item_visible', 'wc_cp_order_item_visible', 10, 2 ); | |
| add_filter( 'woocommerce_widget_cart_item_visible', 'wc_cp_cart_item_visible', 10, 3 ); | |
| add_filter( 'woocommerce_cart_item_visible', 'wc_cp_cart_item_visible' , 10, 3 ); | |
| add_filter( 'woocommerce_checkout_cart_item_visible', 'wc_cp_cart_item_visible', 10, 3 ); | |
| /** Visibility of components in orders. | |
| * | |
| * @param boolean $visible | |
| * @param array $order_item | |
| * @return boolean | |
| */ | |
| function wc_cp_order_item_visible( $visible, $order_item ) { | |
| if ( ! empty( $order_item[ 'composite_parent' ] ) ) { | |
| $visible = false; | |
| } | |
| return $visible; | |
| } | |
| /** | |
| * Visibility of components in cart. | |
| * | |
| * @param boolean $visible | |
| * @param array $cart_item | |
| * @param string $cart_item_key | |
| * @return boolean | |
| */ | |
| function wc_cp_cart_item_visible( $visible, $cart_item, $cart_item_key ) { | |
| if ( ! empty( $cart_item[ 'composite_parent' ] ) ) { | |
| $visible = false; | |
| } | |
| return $visible; | |
| } |
Change the way item subtotals appear in the cart/orders
↑ Back to topBy default, the cart item subtotal of a composite container item is calculated as the sum of the base composite price subtotal + the subtotals of any components that are priced individually. Additionally, the subtotals of individually-priced components are displayed with an ‘Option subtotal:’ prefix.
To prevent this, use the following snippet/plugin, which relies on the woocommerce_add_composited_cart_item_subtotals and woocommerce_add_composited_order_item_subtotals filters:
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Composite Products – Prevent Cart/Order Item Subtotal Modifications | |
| * Plugin URI: https://woocommerce.com/products/composite-products/ | |
| * Description: Prevents Composite Products from modifying the appearance of cart/order item subtotals. Requires v3.7+. | |
| * 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_add_composited_cart_item_prices', 'wc_cp_add_composited_cart_item_prices', 10, 3 ); | |
| add_filter( 'woocommerce_add_composited_cart_item_subtotals', 'wc_cp_add_composited_cart_item_subtotals', 10, 3 ); | |
| add_filter( 'woocommerce_add_composited_order_item_subtotals', 'wc_cp_add_composited_order_item_subtotals', 10, 3 ); | |
| function wc_cp_add_composited_cart_item_prices( $add, $container_item, $container_item_key ) { | |
| return false; | |
| } | |
| function wc_cp_add_composited_cart_item_subtotals( $add, $container_item, $container_item_key ) { | |
| return false; | |
| } | |
| function wc_cp_add_composited_order_item_subtotals( $add, $container_item, $order ) { | |
| return false; | |
| } |
Questions & Support
↑ Back to topHave a question? Please fill out this pre-sales form.
Already purchased and need assistance? Get in touch with us via the Help Desk!