Created
January 24, 2018 08:42
-
-
Save fbmoises/ed8db7c2779f09b2a5016139294b6098 to your computer and use it in GitHub Desktop.
Añadir producto automáticamente al carrito al visitar la tienda
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
// Añadir producto automáticamente al carrito al visitar la tienda | |
if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){ | |
add_action( 'template_redirect', 'wcfb_add_auto_product_to_cart' ); | |
function wcfb_add_auto_product_to_cart() { | |
if ( ! is_admin() ) { | |
$product_id = 54; //El ID del producto | |
$found = false; | |
//primero comprobamos que no esté ya en el carrito | |
if ( sizeof( WC()->cart->get_cart() ) > 0 ) { | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if ( $_product->id == $product_id ) | |
$found = true; | |
} | |
// si no está el producto ya se añade | |
if ( ! $found ) | |
WC()->cart->add_to_cart( $product_id ); | |
} else { | |
// si no hay productos en el carrito se añade | |
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