WooCommerce Automatic Add-to-Cart on Visit – 2024

Automatically adding a product to a customer’s cart when they visit a page can streamline the shopping process, especially for promotions or products essential to specific campaigns. This tutorial will show you how to implement the WooCommerce Automatic Add-to-Cart on Visit feature using custom code in your WordPress theme’s functions.php file.


Before starting, make sure you have administrative access to your WordPress site and are comfortable with editing PHP files. Implementing these changes in a child theme is recommended to preserve them during theme updates.

Step 1: Select the Product and Determine the Trigger Page

Identify the product you want to automatically add to the cart and the page that will trigger this addition. For instance, this could be a landing page for a specific campaign.

Step 2: Add Custom Code to Your Theme’s functions.php File

Add the following code to your theme’s functions.php file. This script checks if the visitor is on a specific page and if the cart does not already contain the item; if these conditions are met, the product is added to the cart.

 * Snippet: WooCommerce Automatic Add-to-Cart on Visit – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?985
* Tested with WooCommerce 8.8.3
*/ // WooCommerce Automatic Add-to-Cart on Visit add_action('template_redirect', 'wcsuccess_automatic_add_to_cart'); function wcsuccess_automatic_add_to_cart() { if (!is_page('special-offer')) { // Replace 'special-offer' with the slug of your trigger page return; } $product_id = 123; // Replace 123 with the ID of the product to add $found = false; // Check if the product is already in the cart foreach (WC()->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; if ($_product->get_id() == $product_id) { $found = true; break; } } // If the product is not found, add it to the cart if (!$found) { WC()->cart->add_to_cart($product_id); } }

Explanation of the Code

  • Hook Used: template_redirect is a WordPress hook that allows you to perform a redirect or additional actions before the page template is loaded.
  • Page Check: This code first checks if the current page is the trigger page.
  • Product Check: It then searches the cart to see if the product is already there to prevent duplicates.
  • Add to Cart: If the product isn’t in the cart, it adds it automatically.
See also  Limit Purchase Quantity in WooCommerce - 2024

Step 3: Test the Automatic Add-to-Cart Functionality

After adding the code:

  • Visit the trigger page as a customer.
  • Check if the specified product is automatically added to the cart.
  • Navigate away and return to the page to ensure the product isn’t added again if it’s already in the cart.

Customising Further

  • Multiple Products: Adapt the code to add multiple products by including additional product IDs and extending the logic to check and add each product.
  • Conditional Additions: Expand the conditions under which products are added, such as only adding them for new visitors or during specific promotional periods.


Implementing WooCommerce Automatic Add-to-Cart on Visit can enhance the shopping experience, making it easier for customers to take advantage of specific offers without extra steps. This feature is particularly useful for promotional campaigns where specific products are heavily featured.

See also  WooCommerce Hide Specific Products for Certain Users - 2024

Testing on a staging site before applying changes to your live site is always recommended to ensure that everything works smoothly and doesn’t disrupt the overall customer experience. This tutorial on WooCommerce Automatic Add-to-Cart on Visit should help you streamline the customer journey and potentially increase conversion rates on promotional items.

0 0 votes
Article Rating

Stay In Touch

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

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x
Scroll to Top