Add a WooCommerce product to the cart with PHP
Sometimes a product should land in the cart without the customer clicking “Add to cart”: a landing page for a campaign that leads straight to checkout, a free gift added when the cart reaches a certain total, a starter kit added after a quiz. WC()->cart->add_to_cart() does it in one call, as long as it runs at the right moment, when WooCommerce has loaded the session and the cart.
Problem: Visitors who open a promotion page should get product 42 in their cart and be sent to the checkout, and a gift product should be added automatically when the cart total passes 100.
Solution: Call WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation ) on template_redirect (front end, cart loaded), check whether the product is already in the cart with find_product_in_cart(), and add gifts on woocommerce_before_calculate_totals.
A landing page that fills the cart
<?php
add_action( 'template_redirect', function () {
if ( ! is_page( 'spring-offer' ) || ! function_exists( 'WC' ) || ! WC()->cart ) {
return;
}
$product_id = 42;
$cart_id = WC()->cart->generate_cart_id( $product_id );
if ( ! WC()->cart->find_product_in_cart( $cart_id ) ) {
WC()->cart->add_to_cart( $product_id, 1 );
}
wp_safe_redirect( wc_get_checkout_url() );
exit;
} );
add_to_cart() returns the cart item key on success and false when the product cannot be added (out of stock, not purchasable, a variable product without a variation); WooCommerce then also adds an error notice that is shown on the next page.
A variation
A variable product needs the variation ID and its attributes:
<?php
WC()->cart->add_to_cart(
50, // the parent product
1,
51, // the variation
array( 'attribute_pa_size' => 'large' ) // its attributes
);
A gift above a cart total
<?php
add_action( 'woocommerce_before_calculate_totals', function ( $cart ) {
if ( is_admin() && ! wp_doing_ajax() ) {
return;
}
$gift_id = 99;
$gift_key = $cart->find_product_in_cart( $cart->generate_cart_id( $gift_id ) );
$subtotal = 0;
foreach ( $cart->get_cart() as $key => $item ) {
if ( $key !== $gift_key ) {
$subtotal += $item['line_subtotal'];
}
}
if ( $subtotal >= 100 && ! $gift_key ) {
$cart->add_to_cart( $gift_id, 1 );
} elseif ( $subtotal < 100 && $gift_key ) {
$cart->remove_cart_item( $gift_key );
}
} );
The gift product itself should cost 0 (or get its price set to 0 in the same hook), and it should be hidden from the catalogue so nobody orders it on its own. The subtotal is calculated without the gift, otherwise the gift would keep itself in the cart.
When it does not work
- On the server side outside a page view (cron, WP-CLI, REST requests without a session),
WC()->cartisnull: there is no customer whose cart could be changed. - Called too early (in
initwith a low priority), the session is not loaded yet.template_redirectandwp_loadedare safe. - With page caching, a cached page never runs the PHP. Exclude the landing page from the cache, as cache plugins already do for the cart and checkout.
Limiting the cart to one specific product is covered in allowing only one product in the cart.
NOTE: Adding products the customer did not choose must be obvious to them: show a notice (“A free gift was added to your cart”) with wc_add_notice(), and make sure the gift can be removed. A surprise item in the cart without explanation reduces trust and conversions.