Last active
May 10, 2020 11:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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