How to Add a Product to Cart When a Customer Visits a Specific Page

How to Add a Product to Cart When a Customer Visits a Specific Page
On this page
  1. Why Auto-Add a Product on a Specific Page
  2. The Core Snippet
  3. Troubleshooting
  4. Frequently Asked Questions
  5. Conclusion

To add a product to cart when a customer visits a specific page in WooCommerce, hook into wp, check the current page ID with is_page(), and call WC()->cart->add_to_cart() — useful for landing pages that bundle a special offer with the page content itself.

Why Auto-Add a Product on a Specific Page

  • Landing pages promoting a single bundled offer can skip the extra “add to cart” click entirely
  • It’s handy for pre-loading a lead magnet or trial product tied to a specific campaign URL
  • It keeps the checkout populated automatically for a streamlined one-page funnel

The Core Snippet

PHP
/** Auto-add a specific product to cart when a chosen landing page loads */
add_action( 'wp', 'add_product_to_cart_on_landing_page_visit' );

function add_product_to_cart_on_landing_page_visit() {

    $target_product_id = 3342;
    $target_page_id     = 1287;

    if ( is_page( $target_page_id ) ) {
        WC()->cart->empty_cart();
        WC()->cart->add_to_cart( $target_product_id );
    }
}
Add a product to cart when a customer visits a specific page: the page before adding the code
Before
Add a product to cart when a customer visits a specific page: the page after adding the code
After

Avoiding Duplicate Additions on Refresh

Emptying the cart first (as shown above) prevents the same product from stacking up in quantity each time the visitor reloads the landing page — adjust this if your funnel actually needs to preserve other cart items instead.

Only Adding the Product Once Per Session

PHP
add_action( 'wp', 'add_product_once_per_session_on_landing_page' );

function add_product_once_per_session_on_landing_page() {
    $target_page_id = 1287;

    if ( is_page( $target_page_id ) && ! WC()->session->get( 'landing_offer_added' ) ) {
        WC()->cart->add_to_cart( 3342 );
        WC()->session->set( 'landing_offer_added', true );
    }
}

Troubleshooting

The Product Doesn’t Get Added

Confirm the product ID is correct and currently published/purchasable — a draft, out-of-stock, or deleted product ID will silently fail to add.

The Snippet Stopped Working After a Plugin Update

Temporarily deactivate all plugins except WooCommerce and retest — a caching plugin or a page builder that alters the request lifecycle around the wp hook is the most common cause of this suddenly breaking.

Frequently Asked Questions

Can I add the product only for first-time visitors?

Yes — combine the page check with a cookie or session flag so returning visitors on a repeat visit don’t get the product re-added automatically.

Can this add a product based on a URL parameter instead of a page ID?

Yes — check $_GET['offer'] (sanitized) instead of is_page() if you want the same landing page to trigger different products based on campaign links.

Will this work if the target page uses caching?

Full-page caching can prevent this PHP logic from running on every visit — exclude the specific landing page from your cache plugin’s rules to keep the auto-add reliable.

Conclusion

Adding a product to cart when a customer visits a specific page in WooCommerce is a short snippet built around is_page() and WC()->cart->add_to_cart() — just add session or cookie logic if you need to control how often it fires per visitor.

See also How to Add a Product to the WooCommerce Cart Programmatically on Visit. Then read WooCommerce Cart Page Hooks: A Visual Guide.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call