If you are looking to add a product to the cart as soon as a specific page is loaded, we can do the following:

Hook into the template_redirection action:

add_action( 'template_redirect', 'wpharvest_add_to_cart_programmatically');
Code language: JavaScript (javascript)

We then create a function where we place all our logic and check if a specific page is being visited. Here we can use either the page slug or the page id.

function wpharvest_add_to_cart_programmatically(){ if( is_page( 'ppc-page' ) ) { // or use the page ID } }
Code language: JavaScript (javascript)

In order to add to the cart, we use the WC() class and the add_to_cart function inside cart.

We will first need to empty the cart to ensure the product is not added multiple times if for example the user refreshes the page, also we can then optionally redirect the user to the cart page.

And now for the full code:

add_action( 'template_redirect', 'wpharvest_add_to_cart_programmatically'); function wpharvest_add_to_cart_programmatically(){ if( is_page( 'ppc-page' ) ) { // or use the page ID WC()->cart->empty_cart(); WC()->cart->add_to_cart( 69 ); // this adds the product with the ID 69; we can also add a second variable which will be the variation ID wp_safe_redirect( wc_get_checkout_url() ); // redirects to the checkout page exit(); // safely closes the function } }
Code language: PHP (php)

Bonus: Create a custom add to cart button

As long as we have access to the product class, we can do the following:

<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button btn"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
Code language: HTML, XML (xml)

Published by Dragos Micu

WooCommerce

Leave a Reply

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

experience the perfect blend of relaxation with markes cbd gummies a comprehensive review ozone cannabis gummies a comprehensive guide to edibles how long do cbd thc gummies stay in your system a comprehensive guide to cbd gummies for back pain do you have to refrigerate cbd gummies cana river a comprehensive guide to choosing the best cbd gu understanding the safety of cbd gummies how many deaths caused by cbd gummies and what you need to experience the bliss of relaxation with our cannabis oil recipe for gummies a delicious and effecti are true form keto gummies legitimate uncovering the truth behind this popular weight loss suppleme the truth about keto gummies are they legit or a scam the ultimate guide to pfizer keto gummies your sweet path to wellness unlock the power of weight loss gummies garth brooks a game changing supplement for a healthier you unlock the power of keto active gummies reviews discover the secret to a healthier happier you 85379 scott disicks 35 pound weight loss from struggles to transformation unleash a slimmer you with go keto gummies ingredients a comprehensive guide to weight loss and hea animale male enhancement gummies reviews do they really work boost intimacy and overall well being with cbd gummies for sex amazon a game changing solution maxiflex najbolje rje enje za bolove u zglobovima i relaksaciju mi i a the best diet for erectile dysfunction a comprehensive guide to improving sexual health 94917 erectile dysfunction generic medications a comprehensive guide to treatment options 6138 revolutionize your love life discover the surprising benefits of cbd gummies for men 39 s ed the power of natural erectile dysfunction foods a comprehensive guide to managing ed