Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Automatically Add Upsell Products to Cart via Custom Link in WooCommerce?

When running marketing campaigns or BOGO “Buy One Get One” promotions, store owners may create custom URLs with upsell products to encourage customers to add complementary items to their carts. With this customization, you can automatically add the upsell products to the cart which will entice customers to purchase additional items in your WooCommerce store. Let’s look into how the upsell products are automatically added to the cart!

Solution: Automatically Add Upsell Products to Cart via Custom Link

The provided code works well for adding upsell products to the cart via a URL parameter and the typical URL parameter is as follows: http://yourwebsite.com/?add-upsells-to-cart=PRODUCT_ID

add_action( 'wp_loaded', 'bulk_upsell_add_to_cart_action', 20 );

function bulk_upsell_add_to_cart_action() {
	if ( ! empty( $_GET['add-upsells-to-cart'] ) ) {
		$product_id = absint( $_GET['add-upsells-to-cart'] );
		$product    = wc_get_product( $product_id );

		if ( $product ) {
			$upsell_ids = $product->get_upsells();

			if ( $upsell_ids ) {
				$count = 0;

				foreach ( $upsell_ids as $upsell_id ) {
					if ( WC()->cart->add_to_cart( $upsell_id ) ) {
						$count ++;
					}
				}

				wc_add_notice( sprintf( _n( 'Added %d item to the cart', 'Added %d items to the cart', $count ), $count ) );
			}
		}
	}
}
flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Output

Let’s look into the product ID=649 and the upsell products linked to it.

  • From the WordPress dashboard, go to “WooCommerce” and select “Products.”
  • Click on the product for which you want to set upsells. For e.g I’m setting upsells for this specific product shown in the image below.
How to Automatically Add Upsell Products to Cart via Custom Link in WooCommerce? - Tyche Softwares
  • On the product editing page, find the “Product Data” section.
  • Scroll down to the “Linked Products” tab.
  • In the “Upsells” field, start typing and select related products.
How to Automatically Add Upsell Products to Cart via Custom Link in WooCommerce? - Tyche Softwares
  • Save or update the product to apply the upsells.

When a customer clicks on the provided URL containing the product ID 649, the code automatically adds the upsell products associated with this product to the cart.

How to Automatically Add Upsell Products to Cart via Custom Link in WooCommerce? - Tyche Softwares

Just like automatically adding the upsell product to the cart, you can also add a free sample for every purchase on the WooCommerce just with a click of the “Order a Free Product” button.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x