Skip to content

Instantly share code, notes, and snippets.

@fbmoises
Created January 24, 2018 08:42
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 fbmoises/ed8db7c2779f09b2a5016139294b6098 to your computer and use it in GitHub Desktop.
Save fbmoises/ed8db7c2779f09b2a5016139294b6098 to your computer and use it in GitHub Desktop.
Añadir producto automáticamente al carrito al visitar la tienda
// 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