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 *

support why insulin isn t lowering your blood sugar understanding the possible 8774glucose causes the power of food to reduce blood sugar a comprehensive guide to managing diabetes and pred guides ibuprofen and blood sugar regulation a comprehensive overview ed6asugar reviews does baking soda 675bcontrol help lower blood sugar will an orange raise blood sugar a comprehensive guide to fruits and diabetes management 16740 media fruits 0806glucose and vegetables that help lower blood sugar naturally wellness diabetic ketoacidosis in a 4 year old a critical 54df5d medical alert collections does hypoglycemia cause shortness of breath 72abcontrol media 5b6717fa ibm diabetes medicine a new era in blood sugar management embracing the future of diabetes management blood sugar meters no pricking 165912 managing low blood sugar with the right food a comprehensive guide to hypoglycemia preventi reviews hypoglycemic agents bbe1sugar effective blood sugar lowering supplements a comprehensive guide to managing glucose levels 292232 health green leafy vegetables and blood sugar b51dglucose regulation a medical perspective article 66313 conquer diabetic neuropathy without drugs natural and effective strategies foods to bring blood sugar down a comprehensive guide to managing blood sugar levels 4724 what the best blood sugar meter a comprehensive guide to blood glucose monitoring the power of exercise for lowering blood sugar a comprehensive guide 4368 questions how long after a meal should diabetics 5b91glucose wait before exercising media smart diabetes monitoring apple 5b6717fa watch band empowering real time health tracking