compatible with WPML auto add ends in loop in some site

Compatible with WPML auto add ends in loop in some site

add_filter('advanced_woo_discount_rules_free_product_cart_item_data', function ($cart_item_data){
    remove_all_filters('woocommerce_add_cart_item');
    remove_all_actions('woocommerce_add_to_cart');

    return $cart_item_data;
});

function wpml_language_has_switched_awdr_compatible(){
    if (is_cart()) {
        $wpml_language = '';
        if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
            $wpml_language = ICL_LANGUAGE_CODE;
        }
        if(!empty($wpml_language)){
            $carts = WC()->cart->get_cart();
            foreach ($carts as $cart_item_key => $cart_item) {
                $have_to_do = false;
                $product_id = $cart_item['data']->get_id();
                $lang_code = wpml_element_language_code_filter( null, array('element_id' => $product_id, 'element_type' => 'post'));

                if($lang_code == $wpml_language){
                    $have_to_do = true;
                }
            }
            if(!$have_to_do){
                add_action('woocommerce_before_cart', function (){
                    ?>
                    <script type="text/javascript">
                        jQuery( document ).ready(function() {
                            jQuery("[name='update_cart']").removeAttr('disabled');
                            jQuery("[name='update_cart']").trigger("click");
                        });
                    </script>
                    <?php
                });
            }
        }
    }
}
add_action('wpml_language_has_switched', 'wpml_language_has_switched_awdr_compatible');