Add product to cart from functions.php

What I’m trying to accomplish is when the checkout page is accessed with certain get arguments a product to be added to the cart.

function my_custom_function() {
if ( $_SERVER[‘REQUEST_URI’] == ‘checkout/?some=argv’ ) {
WC()->cart->add_to_cart( 90759, 1 );
}
}
add_action( ‘init’, ‘my_custom_function’, 7 );

I tried this but when I access the checkout page it’s not added to the cart and i get redirected to the cart page.

Read more here:: Add product to cart from functions.php

Leave a Reply

Your email address will not be published. Required fields are marked *