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 *

pure canna cbd gummies a comprehensive guide to understanding pure canna cbd gummies side effects wana cannabis infused sour gummies the ultimate relaxation solution experience the harmony of nature with caria farms cbd gummies a holistic approach to wellness experience the redemption unlocking the full potential of redeem cbd gummies for a healthier you will cbd gummies cause you to fail a drug test understanding the risks and benefits 46622 rejuvenate cbd gummies unlocking the secrets to a healthier happier you rejuvenate cbd gummies c greenvibe cbd gummies reviews benefits effectiveness and user experiences finding relief with organic cbd gummies for pain a comprehensive guide using cbd for panic attack relief a comprehensive guide to anxiety and stress management understanding the side effects of cbd gummy a comprehensive guide 173 dive into the world of cbd gummies a shark tank approved game changer cbd gummy shark tank review does cbd gummies show up on drug tests a comprehensive guide to cbd gummies and workplace testing reveal the secret to rapid weight loss with keto gummies advanced weight loss unlock the power of a kelly clarkson keto gummies reviews unlock the power of celeb approved weight loss with these delic keto calm gummies your path to stress free keto living 74731 when do you take keto gummies unlocking the science behind optimal results mike pompeo weight loss from 300 pounds to 210 how he shed 90 pounds in six months male enhancement natural a comprehensive guide to improving male health and performance non prescription male enhancement pills a comprehensive guide finding the best penis enlarger a comprehensive guide to safe and effective enhancement