Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oscar-Abad-Folgueira/fe509b6e244716c8f2cf3263ad484b6f to your computer and use it in GitHub Desktop.
Save Oscar-Abad-Folgueira/fe509b6e244716c8f2cf3263ad484b6f to your computer and use it in GitHub Desktop.
Añadir un producto al carrito de Woocommerce automaticamente con código
<?php
/**
* @snippet Añadir un producto al carrito de Woocommerce automaticamente con código
* @author Oscar Abad Folgueira
* @author_url https://www.oscarabadfolgueira.com
* @snippet_url https://www.oscarabadfolgueira.com/anadir-un-producto-al-carrito-de-woocommerce-automaticamente-mediante-codigo/
*/
add_action( 'template_redirect', 'add_product_to_cart_automatically' );
function add_product_to_cart_automatically() {
$product_id = 80;
if ( WC()->cart->get_cart_contents_count() == 0 ) {
WC()->cart->add_to_cart( $product_id );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment