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 *

mx betway sacar dinero blog casinos y estadios en jalisco un ee00256 destino para la diversi n 33 que color es en la ruleta crown bingo casino resena informativo guias rese a de f0ea1b jackpot village casino experiencia y opiniones reportajes juegos de casino gratis descarga y 9951f disfruta sin limitaciones es ainsworth money heat apuestas cu nto gana un 6ebccb5 host en un casino juego juego de tragamonedas vegas la experiencia de azar en l nea gratuita 02a8016d poker juegos de 60e0e casino gratis diversi n y emoci n en tu computadora blog casinos militares un relevante evento e00256f2 en los constituyentes mexico rese a de robo jack en casino real prize un juego de slots de 2871ba microgaming lleno de acci n y emoci n poker horarios del casino estrella roja cuautla y cuernavaca no te pierdas el 0e12d95f entretenimiento noticias celebra el a1aef3 d a de las madres en un casino ideas y recomendaciones juegos de casinos en lima reportajes casinos online que ofrecen juegos lock it link 9951f la experiencia de juego m xima mexico direcci n del casino royal ubicaci n 7d28 y accesibilidad poker c mo ganar dinero en las m quinas 60e0e tragamonedas consejos y estrategias para el xito reportajes imagen casino real en arandas jalisco la 9951f experiencia de juego y entretenimiento blog casino significado y definici n ee00256 en espa ol