Created
January 24, 2018 08:47
-
-
Save fbmoises/a540022c2138f3bd3ea71c162d37c567 to your computer and use it in GitHub Desktop.
Añadir un producto automáticamente al carrito a partir de un gasto concreto
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 un producto automáticamente al carrito a partir de un gasto concreto | |
if ( in_array( 'woocommerce/woocommerce.php', get_option( 'active_plugins' ) ) ){ | |
add_action( 'init', 'wcfb_add_auto_product_to_cart_minimum_order_amount' ); | |
function wcfb_add_auto_product_to_cart_minimum_order_amount() { | |
if ( ! is_admin() ) { | |
global $woocommerce; | |
$product_id = 54; //ID del producto | |
$found = false; | |
$cart_total = 50; //Importe total mínimo del carrito | |
if( $woocommerce->cart->total >= $cart_total ) { | |
//Comprobamos que el producto no esté ya en el carrito | |
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) { | |
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { | |
$_product = $values['data']; | |
if ( $_product->id == $product_id ) | |
$found = true; | |
} | |
// si no está el producto en el carrito lo añadimos | |
if ( ! $found ) | |
$woocommerce->cart->add_to_cart( $product_id ); | |
} else { | |
// si no hay productos en el carrito lo añadimos | |
$woocommerce->cart->add_to_cart( $product_id ); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment