/**
* Automatically add product to cart on visit and auto-fill the cart in WooCommerce.
*/
function auto_add_to_cart_on_visit() {
// Check if the user has already added the product to the cart.
if (WC()->cart->is_empty()) {
// Get the product ID you want to add to the cart automatically.
$product_id = 123; // Replace with your product ID.
// Get the product quantity to add to the cart.
$quantity = 1; // Replace with the desired quantity.
// Check if the product exists.
if (wc_get_product($product_id)) {
// Add the product to the cart.
WC()->cart->add_to_cart($product_id, $quantity);
}
}
}
add_action('template_redirect', 'auto_add_to_cart_on_visit');
0 Comments