How to Automatically Add Related Products to the Cart in WooCommerce – 2026

Last updated on December 17th, 2024 at 07:34 am

Automatically adding related products to the cart in WooCommerce is a powerful way to increase average order value (AOV) and boost sales. This tactic works well for upselling, cross-selling, and bundling products together to encourage customers to buy more.

In this guide, we’ll show you how to automatically add related products to the cart when a customer adds a specific product. This approach requires no plugins, giving you complete control of the functionality.


Step 1: Add Related Products Automatically When Certain Products Are Added

The first step is to automatically add related products to the cart when specific products are added. We’ll use WooCommerce’s woocommerce_add_to_cart hook to trigger the addition of related products.

Code to Automatically Add Related Products to Cart

Add the following code to your theme’s functions.php file or your child theme:

/*
 * Snippet: How to Automatically Add Related Products to the Cart in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1777
* Tested with WooCommerce 10.7.0
* "This function automatically adds related products to the cart when specific products are added"
*/ function wcsuccess_add_related_products_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { // Define which products should trigger the automatic addition of related products $trigger_product_ids = array( 25, 30 ); // Replace with your product IDs // Define the related products to add when the trigger products are added $related_products = array( 25 => array( 50, 51 ), // When product 25 is added, add products 50 and 51 30 => array( 52 ), // When product 30 is added, add product 52 ); // Check if the product that was added to the cart is in the trigger list if ( in_array( $product_id, $trigger_product_ids ) ) { if ( ! empty( $related_products[ $product_id ] ) ) { foreach ( $related_products[ $product_id ] as $related_product_id ) { // Check if the related product is already in the cart if ( ! wcsuccess_is_product_in_cart( $related_product_id ) ) { WC()->cart->add_to_cart( $related_product_id ); } } } } } add_action( 'woocommerce_add_to_cart', 'wcsuccess_add_related_products_to_cart', 10, 6 ); /* * Snippet: How to Automatically Add Related Products to the Cart in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1777
* Tested with WooCommerce 10.7.0
* "This function checks if a product is already in the cart"
*/ function wcsuccess_is_product_in_cart( $product_id ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { if ( $cart_item['product_id'] == $product_id ) { return true; } } return false; }

Explanation:

  • Trigger Products: Products with IDs 25 and 30 are trigger products. When they are added to the cart, the related products are also added.
  • Related Products: Products with IDs 50, 51, and 52 are automatically added when trigger products are added.
  • Duplicate Check: The function checks if the product is already in the cart before adding it, preventing duplicates.

Step 2: Customise the Related Products Logic

In many cases, you’ll want to adjust the logic for which related products are added. Here’s how to modify the logic.

See also  WooCommerce Automatic Add-to-Cart on Visit - 2026

Customise the Products

Change the Product IDs:

  • Replace the values in the $trigger_product_ids array with the IDs of the products you want to trigger the related product additions.
  • Replace the product IDs in the $related_products array with the related products you want to add.

Where to Find Product IDs:

  1. Go to Products in your WordPress admin.
  2. Hover over the product name, and you’ll see the Product ID appear in the URL.

Example Customisation:

$trigger_product_ids = array( 45, 50 ); // New trigger products
$related_products = array(
    45 => array( 60, 61 ), // When product 45 is added, add products 60 and 61
    50 => array( 62 ), // When product 50 is added, add product 62
);

Step 3: Display a Message to Notify Customers About Related Products

When related products are automatically added to the cart, it’s helpful to notify the customer. This improves transparency and reduces confusion.

Code to Display a Notification Message

Add this code to your functions.php file:

/*
 * Snippet: How to Automatically Add Related Products to the Cart in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1777
* Tested with WooCommerce 10.7.0
* "This function adds a notification message when related products are automatically added"
*/ function wcsuccess_notify_related_products_added() { if ( ! is_cart() ) { return; } if ( WC()->session->get( 'related_products_added' ) ) { wc_print_notice( __( 'Related products have been added to your cart.', 'woocommerce' ), 'notice' ); WC()->session->__unset( 'related_products_added' ); } } add_action( 'woocommerce_before_cart', 'wcsuccess_notify_related_products_added' ); /* * Snippet: How to Automatically Add Related Products to the Cart in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1777
* Tested with WooCommerce 10.7.0
* "This function sets a session variable when related products are added"
*/ function wcsuccess_set_related_product_session( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { WC()->session->set( 'related_products_added', true ); } add_action( 'woocommerce_add_to_cart', 'wcsuccess_set_related_product_session', 10, 6 );

Explanation:

  • Notification: Adds a message to the cart page notifying customers that related products have been added.
  • Session Handling: Uses WooCommerce session handling to ensure the message only appears once.

Step 4: Optional – Customise the Related Products Display

If you’d like to display information about the related products on the product page before they’re added, you can show related product details directly on the product page.

Code to Display Related Products on Product Pages

Add this code to your functions.php file:

/*
 * Snippet: How to Automatically Add Related Products to the Cart in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1777
* Tested with WooCommerce 10.7.0
* "This function displays the related products on the product page before adding them to the cart"
*/ function wcsuccess_display_related_products_on_product_page() { global $product; $product_id = $product->get_id(); $related_products = array( 25 => array( 50, 51 ), // When product 25 is added, display products 50 and 51 30 => array( 52 ), // When product 30 is added, display product 52 ); if ( ! empty( $related_products[ $product_id ] ) ) { echo '<div class="related-products-banner" style="background-color: #f4f4f9; padding: 20px; text-align: center; margin-bottom: 20px;">'; echo '<h2>Related Products You’ll Get</h2>'; foreach ( $related_products[ $product_id ] as $related_product_id ) { $related_product = wc_get_product( $related_product_id ); echo '<p><strong>' . esc_html( $related_product->get_name() ) . '</strong></p>'; } echo '</div>'; } } add_action( 'woocommerce_before_add_to_cart_form', 'wcsuccess_display_related_products_on_product_page' );

Explanation:

  • Dynamic Product Display: Shows related product information on the product page.
  • Customisation: Change the background colour and layout using CSS.
See also  How to Manage Stock Programmatically in WooCommerce - 2026

Example Workflow

  1. Customer Adds a Product:
    • The customer adds Product A (ID 25) to their cart.
  2. Automatic Product Addition:
    • Products B and C (IDs 50 and 51) are automatically added to the cart.
  3. Cart Notification:
    • The customer is notified that related products have been added.

Use Case Example

Trigger ProductRelated Products Added
Product A (ID 25)Product B (ID 50), Product C (ID 51)
Product X (ID 30)Product Y (ID 52)

Conclusion

Automatically adding related products to the cart in WooCommerce can boost sales, increase order value, and encourage customers to buy more. By following this guide, you can dynamically link products together and automatically add them to the cart.

Test these changes in a staging environment before applying them to your live site. For more WooCommerce customisation options, explore our WooCommerce Visual Hooks Guide or use our wp-config generator for advanced configurations.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×