/* 
Theme Name: Hello Elementor Child
Theme URI: https://github.com/elementor/hello-theme/
Description: Hello Elementor Child is a child theme of Hello Elementor, created by Elementor team
Author: Elementor Team
Author URI: https://elementor.com/
Template: hello-elementor
Version: 1.0.1
Text Domain: hello-elementor-child
License: GNU General Public License v3 or later.
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Tags: flexible-header, custom-colors, custom-menu, custom-logo, editor-style, featured-images, rtl-language-support, threaded-comments, translation-ready
*/

/*
    Add your custom styles here
*/

/*
    Codigo para ancho completo y ancho ampliado gutemberg
https://decodecms.com/ancho-ampliado-y-ancho-completo-en-gutenberg/
*/
.alignfull {
	margin: 32px calc(50% - 50vw);
	max-width: 100vw;
}

.alignwide {
	margin: 32px calc(25% - 25vw);
	max-width: 100vw;
}

add_filter('woocommerce_package_rates', 'restringir_envio_gratis_despues_descuento', 10, 2);
function restringir_envio_gratis_despues_descuento($rates, $package) {
    $cart_total_after_discount = WC()->cart->get_total('edit'); // Total después de aplicar el descuento
    $applied_coupons = WC()->cart->get_applied_coupons(); // Obtener cupones aplicados

    // Verifica si el usuario ha aplicado un cupón
    if (!empty($applied_coupons)) {
        foreach ($applied_coupons as $coupon_code) {
            $coupon = new WC_Coupon($coupon_code);
            // Verifica si el cupón es del 50%
            if ($coupon->get_discount_type() === 'percent' && $coupon->get_amount() == 50) {
                // Si el total después del descuento es menor a 200€, eliminamos el envío gratuito
                if ($cart_total_after_discount < 200) {
                    foreach ($rates as $rate_id => $rate) {
                        if ('free_shipping' === $rate->method_id) {
                            unset($rates[$rate_id]);
                        }
                    }
                }
            }
        }
    }
    return $rates;
}
