In WooCommerce, there are times when you might want to automatically add an additional product to the cart when a specific product is added. This is often referred to as a “forced sell.” In this blog post, we will walk you through how to achieve this using WooCommerce hooks and custom code.
Step-by-Step Guide
Prerequisites
- A running WooCommerce store.
- Basic knowledge of PHP and WordPress/WooCommerce hooks.
Step 1: Identify Product IDs
First, you need to know the IDs of both the primary product and the additional product you want to add to the cart. You can find the product IDs by going to the WooCommerce Products page and hovering over the product names.
Step 2: Add Custom Code to Your Theme
We will add the custom code to the functions.php
file of your active theme. You can access this file via the WordPress admin dashboard or using FTP.
Step 3: Write the Code
Below is the code to automatically add an additional product to the cart when a specific product is added:
// Add additional product to the cart if a certain product is present
function add_forced_sell_product( $cart_item_data, $product_id, $variation_id ) {
// Set the product ID of the primary product
$target_product_id = 123; // Replace 123 with your primary product ID
// Set the product ID of the additional product
$additional_product_id = 456; // Replace 456 with your additional product ID
// Check if the primary product is being added to the cart
if ( $product_id == $target_product_id ) {
// Check if the additional product is already in the cart
$found = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['product_id'] == $additional_product_id ) {
$found = true;
break;
}
}
// If the additional product is not found, add it to the cart
if ( ! $found ) {
WC()->cart->add_to_cart( $additional_product_id );
}
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_forced_sell_product', 10, 3 );
Step 4: Save and Test
After adding the code, save the functions.php
file and test your WooCommerce store:
- Add the primary product (with the ID you specified) to your cart.
- Check the cart to see if the additional product is automatically added.
Explanation of the Code
- Hooking into the Cart Process: We use the
woocommerce_add_cart_item_data
filter to hook into the process of adding items to the cart. - Product IDs: We set the target product ID (the product that triggers the addition) and the additional product ID (the product to be added).
- Cart Check: When the target product is added to the cart, we check if the additional product is already in the cart.
- Add Product: If the additional product is not already in the cart, we add it using the
add_to_cart
method.
Conclusion
By following these steps, you can easily set up a forced sell in WooCommerce, where adding a specific product to the cart automatically adds another product. This approach can be customized further to meet various business requirements, such as adding multiple products, applying conditions, or modifying quantities.
Feel free to leave a comment below if you have any questions or need further assistance!
Happy coding!