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/a3d2b6318c85343e65ee91f2b7acba4c to your computer and use it in GitHub Desktop.
Save Oscar-Abad-Folgueira/a3d2b6318c85343e65ee91f2b7acba4c to your computer and use it in GitHub Desktop.
Comprobar si el carrito de WooCommerce está vacío y si es así, añadir un producto
<?php
/**
* @snippet Comprobar si el carrito de WooCommerce está vacío y si es así, añadir un producto
* @author Oscar Abad Folgueira
* @author_url https://www.oscarabadfolgueira.com
* @snippet_url https://www.oscarabadfolgueira.com/funcion-woocommerce-para-comprobar-si-el-carrito-esta-vacio/
*/
add_action( 'template_redirect', 'add_product_if_cart_empty' );
function add_product_if_cart_empty() {
$product_id = 80; // producto a añadir al carrito si está vacío
if ( WC()->cart->is_empty() ){ // Comprobar si el carrito está vacío
WC()->cart->add_to_cart( $product_id ); // Añadir producto al carrito
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment