Crear un plugin para Woocommerce usando Chatgpt
En este video te mostraré cómo podemos mejorar nuestra tienda online de Woocommerce añadiendo funcionalidades extras usando Chatgpt.
Aquí tienes el código empleado y que debes insertar en el archivo functions.php del tema activo:
add_action( 'template_redirect', 'add_product_to_cart_automatically' );
function add_product_to_cart_automatically() {
$product_id = 3682;
if ( WC()->cart->get_cart_contents_count() == 1 ) {
WC()->cart->add_to_cart( $product_id );
}
}
add_action( 'woocommerce_before_calculate_totals', 'remove_product_when_empty_cart' );
function remove_product_when_empty_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$product_id_to_remove = 3682; // Reemplaza con el ID de tu producto a eliminar
$cart_contents_count = $cart->get_cart_contents_count();
if ( $cart_contents_count === 0 ) {
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['product_id'] == $product_id_to_remove ) {
$cart->remove_cart_item( $cart_item_key );
break;
}
}
} elseif ( $cart_contents_count === 1 ) {
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['product_id'] == $product_id_to_remove ) {
$cart->remove_cart_item( $cart_item_key );
break;
}
}
}
}