Jump to content

Update the code to PHP 8.2: conditionally displaying a notice about free shipping in Woocommerce


Go to solution Solved by kicken,

Recommended Posts

Hi!

I have used the code for the free shipping notice and after updating to PHP 8.2 i have the following errors:

Quote

Warning: Undefined variable $now_time in /data01/virt81820/domeenid/www.enjoythewoodestonia.ee/test/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code on line 25

Warning: Undefined variable $end_time in /data01/virt81820/domeenid/www.enjoythewoodestonia.ee/test/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code on line 25

Warning: Undefined variable $end_time in /data01/virt81820/domeenid/www.enjoythewoodestonia.ee/test/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code on line 29

Warning: Undefined variable $now_time in /data01/virt81820/domeenid/www.enjoythewoodestonia.ee/test/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code on line 29

Warning: Undefined variable $is_week_days in /data01/virt81820/domeenid/www.enjoythewoodestonia.ee/test/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code on line 29

 

So i need help with updating this code to be compatible with PHP 8.2.

What is important:

  1. I already used another code from https://www.businessbloomer.com/woocommerce-add-need-spend-x-get-free-shipping-cart-page/. And the problem was, that my site is in several languages, and there were some problems with translating.
  2. I do not really need those parts of my current code, which includes delivery and time information. I need only a "free shipping notice option". So here is a code and i have really appreciated it if someone can help with optimizing/changing it.

So here is a code and i have really appreciated it if someone can help with optimizing/changing it.

add_action( 'woocommerce_before_checkout_form', 'next_day_delivery' );
add_action( 'woocommerce_before_cart', 'next_day_delivery' );
add_action( 'flatsome_cart_sidebar', 'next_day_delivery' );

function next_day_delivery() {
    if( WC()->cart->is_empty() )
        return; // Exit


    $cart_subtotal    = WC()->cart->subtotal;
    $limit_free    = 150; // Starting freee shipping amount
    $free_shipping = '';  // Initialising

    if ( $cart_subtotal < $limit_free ) {
        $free_shipping =  sprintf(
            __('%s Add %s worth of goods to your order to get %s', 'woocommerce' ),
            '', // ' <img src="' . get_stylesheet_directory_uri() . '/images/shipping-icon.png"> ',
            strip_tags( wc_price( round( $limit_free - $cart_subtotal, 2 ) ) ),
            '<strong>' . __( 'FREE SHIPPING!', 'woocommerce' ) . '</strong>'
        );
    } elseif ( $cart_subtotal >= $limit_free ) {
        $free_shipping = '<strong>' . __( ' You get FREE SHIPPING!', 'woocommerce' ) . '</strong>';
    }

    if ( $end_time > $now_time && $is_week_days ) {
        // print the information notice
        $message = sprintf( __( '', 'woocommerce' ), $remain_time, $free_shipping );
    }
    elseif ( $end_time <= $now_time && $is_week_days ) {
        $message = sprintf( __( '', 'woocommerce' ), $after_tomorow, $free_shipping );
    } else {
        $message = __( '', 'woocommerce' ) . $free_shipping;
    }
    wc_print_notice( $message, 'success' );
}

 

2 hours ago, MikeETWE said:
    if ( $end_time > $now_time && $is_week_days ) {
        // print the information notice
        $message = sprintf( __( '', 'woocommerce' ), $remain_time, $free_shipping );
    }
    elseif ( $end_time <= $now_time && $is_week_days ) {
        $message = sprintf( __( '', 'woocommerce' ), $after_tomorow, $free_shipping );
    } else {
        $message = __( '', 'woocommerce' ) . $free_shipping;
    }

You never define the variables used here anywhere.  If you don't care about time-based shipping then remove that part of the code, only keep what is in the else branch.

$message = __( '', 'woocommerce' ) . $free_shipping;

 

7 minutes ago, kicken said:

You never define the variables used here anywhere.  If you don't care about time-based shipping then remove that part of the code, only keep what is in the else branch.

$message = __( '', 'woocommerce' ) . $free_shipping;

 

Hi, like this?
 

add_action( 'woocommerce_before_checkout_form', 'next_day_delivery' );
add_action( 'woocommerce_before_cart', 'next_day_delivery' );
add_action( 'flatsome_cart_sidebar', 'next_day_delivery' );

function next_day_delivery() {
    if( WC()->cart->is_empty() )
        return; // Exit


    $cart_subtotal    = WC()->cart->subtotal;
    $limit_free    = 150; // Starting freee shipping amount
    $free_shipping = '';  // Initialising

    if ( $cart_subtotal < $limit_free ) {
        $free_shipping =  sprintf(
            __('%s Add %s worth of goods to your order to get %s', 'woocommerce' ),
            '', // ' <img src="' . get_stylesheet_directory_uri() . '/images/shipping-icon.png"> ',
            strip_tags( wc_price( round( $limit_free - $cart_subtotal, 2 ) ) ),
            '<strong>' . __( 'FREE SHIPPING!', 'woocommerce' ) . '</strong>'
        );
    } elseif ( $cart_subtotal >= $limit_free ) {
        $free_shipping = '<strong>' . __( ' You get FREE SHIPPING!', 'woocommerce' ) . '</strong>';
    } else {
        $message = __( '', 'woocommerce' ) . $free_shipping;
    }
    wc_print_notice( $message, 'success' );
}

 

In that case i have error:

Quote

Warning: Undefined variable $message in /data01/virt81820/domeenid/www.enjoythewoodestonia.ee/test/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()'d code on line 26

And the caption "Add ... worth of goods to your order to get ...." is not displayed.
 

uTrPms.png

  • Solution
3 hours ago, MikeETWE said:

Hi, like this?

No, you do not merge the else into the previous set of if statements.  You replace the second set of if statements with only what was in the else branch.  Like this.

function next_day_delivery() {
    if( WC()->cart->is_empty() )
        return; // Exit


    $cart_subtotal    = WC()->cart->subtotal;
    $limit_free    = 150; // Starting freee shipping amount
    $free_shipping = '';  // Initialising

    if ( $cart_subtotal < $limit_free ) {
        $free_shipping =  sprintf(
            __('%s Add %s worth of goods to your order to get %s', 'woocommerce' ),
            '', // ' <img src="' . get_stylesheet_directory_uri() . '/images/shipping-icon.png"> ',
            strip_tags( wc_price( round( $limit_free - $cart_subtotal, 2 ) ) ),
            '<strong>' . __( 'FREE SHIPPING!', 'woocommerce' ) . '</strong>'
        );
    } elseif ( $cart_subtotal >= $limit_free ) {
        $free_shipping = '<strong>' . __( ' You get FREE SHIPPING!', 'woocommerce' ) . '</strong>';
    }

    $message = __( '', 'woocommerce' ) . $free_shipping;
 
    wc_print_notice( $message, 'success' );
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.