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 fbmoises/a540022c2138f3bd3ea71c162d37c567 to your computer and use it in GitHub Desktop.
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
// 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