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 *

ilhjgokq how long do sex pills last a guide to c773dd86 their duration and effectiveness unlock the potential of male enhancement cbd gummies a comprehensive review of the benefits and eff vital grow xl testo gummies reviews elevate your game best male enhancement gummies for unstop wellness is branded ed medication f8303ee3 more effective than generic alternatives erectile dysfunction drugs over the counter a comprehensive guide 54542 lifestyle nature s boosters discover the fruits that support a4cf31 erectile health and vitality hcmlepdoi nitroxin male d0bdc73c enhancement price affordable boost for enhanced performance lifestyle top marijuana strains for enhancing sexual desire a comprehensive guide 55a4 lifestyle can 21 year olds take extenze a safety guide 55a4 media natural 75b9 penis enhancement solution effective methods for safe size growth movie advanced 633fa male enhancement supplements for enhanced vitality and performance opdtuvmq vigor prime male enhancement gummies natural energy 8416af boost for enhanced performance vitality media how to reduce erection duration effective tips and techniques 935d83b vitafusion men 39 s gummies review a comprehensive analysis of the multivitamin supplement wellness did f8303ee3 michael douglas promote male enhancement supplements can you take cbd and viagra together interactions benefits and risks enhancing male performance with phenoman male enhancement gummies a comprehensive review 57850 article the ultimate male enhancement unleashing the power of the king of the jungle 29b232 article is unprotected sex safe during placebo pills bea8libido movie natural ways to cure erectile dysfunction effective remedies for 3b2esex a healthier sex life