// Need enable Cloudways geolocation
// Displays a message "Delivery is free for orders of 6 items or more!" next to add to cart button when quantity goes over 1. Only for user who do not have a membership plan. Only for Europe
add_action( 'woocommerce_after_add_to_cart_button', 'suggest_box_when_qty_over_six' ); 
function suggest_box_when_qty_over_six() { 
    if (!class_exists('WooCommerce')) return;
    if (is_product()) { 
        $productId = get_the_ID();
        $product = wc_get_product( $productId );

        // If not in Europe -> abort
        $FORWARDED_CONTINENT = getenv('HTTP_X_FORWARDED_CONTINENT');
        if ( $FORWARDED_CONTINENT === 'EU' ) {

            // Don't display the message for membership_plan_name
            if ( ! function_exists( 'wc_memberships' ) ) { return; }
            if ( ! wc_memberships_is_user_member() ) {
                ?>

                <script>
                    (function($){
                        $(document).ready(function(){
                            $('[name="quantity"]').on('change', function(e){
                                var qty_box = $(this);

                                var freeshipping_message = $('<?php echo __( '<div class="freeshipping_msg_div">Delivery is free for orders of 6 items or more!</div>', 'avada-child' ); ?>');
                                
                                if(parseInt(qty_box.val()) >= 2) {
                                    if($('.cart .freeshipping_msg_div').length < 1){
                                        $('.cart').append(freeshipping_message);
                                        $('.freeshipping_msg_div').hide().fadeIn(200);
                                    }
                                }
                                else {
                                    $('.cart .freeshipping_msg_div').remove();
                                }
                            });
                        });
                    })(jQuery);
                </script> 

                <?php 
            }
        }
    } 
}