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 *

questions comprehensive overview of diabetes 33daf medications mechanisms of action and therapeutic strategies support fiber s role in b9877 blood sugar regulation health blanci diabetes medication a comprehensive guide for ce83type patients questions quick f435type ways to lower blood sugar naturally effective strategies for rapid management collections can hypoglycemia cause fainting understanding 2ab6diabetes the link between low blood sugar and loss of consciousness movie high blood sugar symptoms recognizing signs in bengali 6eadcontrol individuals 330719 questions managing diabetes are you still thirsty while on medication 5b91sugar health stomach friendly diabetes b5bfdiabetes medications a guide to minimizing discomfort 23384 lifestyle can diabetes medication 029f relieve tingling media could diabetes medication lead to 06f8diabetes blurred vision reviews does 1654c12 amoxicillin impact blood sugar levels finding the best berberine for blood sugar a comprehensive guide to effective supplements 37025 lifestyle homeopathic medicine for diabetic neuropathy natural treatment 22e0control options and benefits health evaluating diabetes medication 51d3db efficacy clinical outcomes and patient impact media lada diabetes and ketoacidosis understanding the link between autoimmune diabetes and a life threatening 5b6717fa complication article the secret to lowering blood sugar how vinegar works as 31354a3 a natural remedy bdlnez 885bc3b diabetes medications starting with l a guide to liraglutide and other effective treatments blogs natural ways a4b5lower to lower blood sugar levels 891389 support is 107 mg dl fasting blood sugar considered 987747 high for non diabetics guides understanding the normal range of serum glucose fb8737 levels