• Resolved kongb

    (@kongb)


    I am trying to add a ‘Get free shipping if you order X more’ notice on my WooCommerce cart. At the moment I have added the below code to my child php -and it works perfectly. My problem is that I am building a multilingual webshop (currently two languages, Danish and English – and two currencies DKK and EUR). At the moment the free shipping threshold is a static number of 499. I would like this threshold to change based on the active currency symbol. Ex. if customer is viewing products with prices in dkk, the threshold should be 499. Else if EUR, the threshold shown in the notice should be 70 eur, etc.

    I am thinking it might be possible use a switch case statement -but I am a bit of a code newbie and can’t quite get my head around how to get it to work. Below I include both the code that works (ex. with the static threshold of 499) and a modified (non-working)version of it where I have attempted to add a switch case statement.

    Any help is much appreciated!

    Thanks,
    Ric

    Working free shipping notice with static threshold of 499:

    `add_action( ‘woocommerce_before_cart’, ‘free_shipping_cart_notice’ );

    function free_shipping_cart_notice() {

    $min_amount = 499; //change this to your free shipping threshold

    $current = WC()->cart->subtotal;

    if ( $current < $min_amount ) {
    $added_text = ‘Get free shipping if you order ‘ . wc_price( $min_amount – $current ) . ‘ more!’;
    $return_to = wc_get_page_permalink( ‘shop’ );
    $notice = sprintf( ‘<a href=”%s” class=”button wc-forward”>%s</a> %s’, esc_url( $return_to ), ‘Continue Shopping’, $added_text );
    wc_print_notice( $notice, ‘notice’ );
    }

    }`

    Non-working free shipping notice -attempting to use switch case statement:

    `function free_shipping_cart_notice($min_amount, $currency) {

    switch( $currency ) {
    case ‘DKK’: $min_amount = 499; break;
    case ‘EUR’: $min_amount = 70; break;
    }

    $current = WC()->cart->subtotal;

    if ( $current < $min_amount ) {
    $added_text = ‘Get free shipping if you order ‘ . wc_price( $min_amount – $current ) . ‘ more!’;
    $return_to = wc_get_page_permalink( ‘shop’ );
    $notice = sprintf( ‘<a href=”%s” class=”button wc-forward”>%s</a> %s’, esc_url( $return_to ), ‘Continue Shopping’, $added_text );
    wc_print_notice( $notice, ‘notice’ );
    }

    }`

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Free shipping threshold based on currency symbol -PHP function’ is closed to new replies.