A simple composite product in WooCommerce without a plugin
A composite product lets the customer build one item from components: a gift box with a choice of tea, mug and card, or a computer with a choice of processor and memory. Extensions do this with full stock handling for every component. For a shop with one or two such products, a few hooks are enough: the components are selects on the product page, the choice is stored with the cart item, and the price is the base price plus the components.
Problem: A shop sells a gift box. The customer picks one tea out of five and one mug out of three; each choice has its own price. Variations would need 15 combinations, and every new tea multiplies them.
Solution: Store the component groups with their options (product IDs) as product meta, print one select per group before the add-to-cart button, validate the choice on woocommerce_add_to_cart_validation, keep it in the cart item data, add the component prices in woocommerce_before_calculate_totals, show the components in the cart and save them as order item meta.
The components
For a first version, the definition lives in a meta field of the box product, for example set once with WP-CLI:
wp post meta update 512 _ha_components '[{"key":"tea","label":"Tea","options":[101,102,103]},{"key":"mug","label":"Mug","options":[201,202]}]' --format=json
The options are normal simple products (hidden from the catalog if they are not sold alone), so they have their own price and stock.
function ha_get_components( int $product_id ): array {
$components = get_post_meta( $product_id, '_ha_components', true );
return is_array( $components ) ? $components : array();
}
The selects on the product page
add_action( 'woocommerce_before_add_to_cart_button', 'ha_composite_fields' );
function ha_composite_fields(): void {
global $product;
foreach ( ha_get_components( $product->get_id() ) as $group ) {
$name = 'ha_component[' . sanitize_key( $group['key'] ) . ']';
echo '<p class="ha-component"><label>' . esc_html( $group['label'] ) . '<br><select name="' . esc_attr( $name ) . '" required>';
echo '<option value="">' . esc_html__( 'Choose…', 'textdomain' ) . '</option>';
foreach ( $group['options'] as $option_id ) {
$option = wc_get_product( $option_id );
if ( ! $option || ! $option->is_purchasable() || ! $option->is_in_stock() ) {
continue;
}
printf(
'<option value="%d">%s (+%s)</option>',
$option->get_id(),
esc_html( $option->get_name() ),
wp_strip_all_tags( wc_price( $option->get_price() ) )
);
}
echo '</select></label></p>';
}
}
Options out of stock are not offered. wc_price() returns HTML, which is not allowed inside <option>, hence wp_strip_all_tags().
Validate and store the choice
add_filter( 'woocommerce_add_to_cart_validation', 'ha_composite_validate', 10, 2 );
function ha_composite_validate( bool $passed, int $product_id ): bool {
$chosen = wp_unslash( $_POST['ha_component'] ?? array() );
foreach ( ha_get_components( $product_id ) as $group ) {
$value = absint( $chosen[ $group['key'] ] ?? 0 );
if ( ! in_array( $value, array_map( 'intval', $group['options'] ), true ) ) {
/* translators: %s: component label */
wc_add_notice( sprintf( __( 'Please choose: %s', 'textdomain' ), $group['label'] ), 'error' );
return false;
}
}
return $passed;
}
add_filter( 'woocommerce_add_cart_item_data', 'ha_composite_cart_data', 10, 2 );
function ha_composite_cart_data( array $data, int $product_id ): array {
$components = ha_get_components( $product_id );
if ( ! $components ) {
return $data;
}
$chosen = wp_unslash( $_POST['ha_component'] ?? array() );
foreach ( $components as $group ) {
$data['ha_components'][ $group['key'] ] = absint( $chosen[ $group['key'] ] ?? 0 );
}
return $data;
}
The server checks that every chosen ID is one of the allowed options of its group; the select in the browser can be changed by anyone. Because the choice is part of the cart item data, two boxes with different teas become two separate cart lines.
The price
add_action( 'woocommerce_before_calculate_totals', 'ha_composite_price', 20 );
function ha_composite_price( WC_Cart $cart ): void {
foreach ( $cart->get_cart() as $item ) {
if ( empty( $item['ha_components'] ) ) {
continue;
}
$base = (float) wc_get_product( $item['product_id'] )->get_price( 'edit' );
$extra = 0.0;
foreach ( $item['ha_components'] as $option_id ) {
$option = wc_get_product( $option_id );
$extra += $option ? (float) $option->get_price( 'edit' ) : 0;
}
$item['data']->set_price( $base + $extra );
}
}
The price is calculated from the base product every time, not added to the current price; the hook runs several times per request, and adding to $item['data']->get_price() would add the components again on each run.
Show and save the components
add_filter( 'woocommerce_get_item_data', function ( array $lines, array $item ): array {
foreach ( $item['ha_components'] ?? array() as $option_id ) {
$lines[] = array( 'key' => __( 'Includes', 'textdomain' ), 'value' => esc_html( get_the_title( $option_id ) ) );
}
return $lines;
}, 10, 2 );
add_action( 'woocommerce_checkout_create_order_line_item', function ( WC_Order_Item_Product $order_item, string $key, array $values ): void {
foreach ( $values['ha_components'] ?? array() as $group => $option_id ) {
$order_item->add_meta_data( ucfirst( $group ), get_the_title( $option_id ) );
$order_item->add_meta_data( '_ha_' . $group . '_id', $option_id );
}
}, 10, 3 );
The visible meta appears in the order, the emails and the admin; the hidden keys with a leading underscore keep the IDs for reports. The Cart and Checkout blocks show the item data from woocommerce_get_item_data as well. The woocommerce_checkout_create_order_line_item hook runs for the classic checkout and for the Checkout block.
NOTE: This simple version does not reduce the stock of the components; only the box product’s stock changes. If the components are limited, reduce their stock in woocommerce_reduce_order_item_stock or use a composite products extension, which also handles refunds and returns per component. Product questions of this kind in the cart: allow only one product in the WooCommerce cart.