Cart BOGO rule compatible with WPML on switching the language in cart

Cart BOGO rule compatible with WPML on switching the language in cart

add_filter('woo_discount_rules_process_cart_bogo_auto_add', function ($have_to_do){
    $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
            });
        }
    }
    return $have_to_do;
}, 100);