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 *

discover the bliss of grove cbd gummies unlocking relaxation relief and vitality finding relief with sleep gummies cannabis a comprehensive guide the gentle wellness revolution exploring the 5mg cbd gummies effect the benefits of cv sciences sleep gummies for a restful night 39 s sleep 855 who sells cbd gummies a comprehensive guide to buying cbd gummies online revolutionize your weight loss journey with gmy keto gummies shark tank a game changing solution fo unlock the power of keto gummies discover the ingredients of keto gummies for a healthier you do turbo keto gummies work for weight loss separating fact from fiction discover the power of tamela mann weight loss keto gummies a natural and effective solution for a h how to return keto gummies a step by step guide to getting your money back semaglutide dose chart for weight loss proper administration soaking your feet in apple cider vinegar a natural approach to weight loss for 1000 us obese individuals healthy blog transform your body with extreme change keto gummies a revolutionary weight loss solution revolutionize your ed experience with airy cbd gummies a game changing solution for a healthier yo enhancing intimacy with cbd gummies for libido in women a comprehensive guide 6729 what is the best injection for erectile dysfunction a comprehensive guide 39798 7181 finding the best gummies for male enhancement a comprehensive guide 7416 experience the relief trufarm cbd gummies for ed a game changer for men 39 s health finding the best medicine for erectile dysfunction without side effects a comprehensive gui 70179 enhancing intimacy with male sex gummies a comprehensive guide