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.
Prerequisites
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.
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.
Conclusion
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.
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.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch