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 *

finding restful nights with love hemp sweet dreams gummies a comprehensive guide male enhancement natural a comprehensive guide to effective and safe solutions revolutionize your sex life the surprising benefits of cbd gummies for male enhancement maxman male enhancement gummies review natural libido boosters amp energy penguin cbd gummies for ed benefits and user reviews the truth about penis enlarging pills separating fact from fiction 97813 cbd gummies for erectile dysfunction do they really work 85874 herbal supplements for erectile dysfunction a comprehensive guide 9830 is xtreme fit keto gummies legit a comprehensive review of the best keto gummies for weight loss jessica simpsons 100 pound weight loss the secret behind her transformation lizzos weight loss journey a celebration of self discovery unlock the power of amazon weight loss gummies a comprehensive review of the best supplements for a the benefits and effects of 100mg thc 100mg cbd gummies a comprehensive guide discover the best cbd gummies within 20 mi boost your health and wellness with nature 39 s harmony kim sisters cbd gummies the ultimate solution for a stress free life dr oz cbd diabetic gummies the ultimate solution for diabetic management and sugar control experience the power of bioheal cbd gummies on amazon prime unlock natural pain relief and bioheali best cbd gummy for diabetes a comprehensive guide to managing blood sugar levels with premium cbd p discover the bliss of 50mg gummies cbd unlocking a world of relaxation and wellness experience the bliss of cbd green lobster gummies a game changer for your wellness journey