-1

I am trying to create an Estimate Delivery Per Shipping Zone in Cart, Checkout, Thank you page, and Email for Woocommerce.

I am using WooCommerce Shipping Estimate free plugin which works great displaying in cart and checkout the estimated delivery time.

Now the plugin doesn't display that on orders and and email notifications…

I tried to hack the plugin and add a function hooked in woocommerce_thankyou_order_received_text without success so far.

Can someone point me in the right direction?

aynber
  • 22,380
  • 8
  • 50
  • 63
ice41
  • 29
  • 5
  • https://stackoverflow.com/help/how-to-ask – Ivar Feb 08 '21 at 18:14
  • @LoicTheAztec actually, I am following your code here https://stackoverflow.com/questions/52747221/display-a-custom-message-based-on-customer-shipping-zone-in-woocommerce But whenever I try to add add_action( 'woocommerce_thankyou_order_received_text' , 'shipping_zone_targeted_postcodes_custom_notice' ); it doesn't display on thank you page, not sure what to do next. – ice41 Feb 09 '21 at 15:52
  • I have tried the plugin and found the way to display the "Delivery estimate" in the orders and emails notifications on total rows after the shipping total. See My answer below. – LoicTheAztec Feb 09 '21 at 18:13

1 Answers1

1

When using the plugin WooCommerce Shipping Estimate it's possible to to display the "Delivery estimate" in the orders and emails notifications on total rows after the shipping total as follows:

add_filter( 'woocommerce_get_order_item_totals', 'delivery_estimate_as_order_item_total_row', 10, 3 );
function delivery_estimate_as_order_item_total_row( $total_rows, $order, $tax_display ){
    $from   = get_option('wc_shipping_method_estimate_from');
    $to     = get_option('wc_shipping_method_estimate_to');
    $format = get_option('wc_shipping_estimate_format');

    $shipping_methods = $order->get_shipping_methods();
    $shipping_method  = reset($shipping_methods);
    $instance_id      = $shipping_method->get_instance_id();

    $from = isset($from[$instance_id]) ? $from[$instance_id] : '';
    $to   = isset($to[$instance_id])   ? $to[$instance_id]   : '';

    if ( isset($total_rows['shipping']) && ( $from || $to ) ) {
        if ( $from ) {
            $from_days = _n( 'day', 'days', $from, 'woocommerce' );
        }

        if ( $to ) {
            $to_days   = _n( 'day', 'days', $to, 'woocommerce' );
        }

        if ( $format === 'days' ) {

            if ( $from && $to && $from != $to ) {
                $delivery_estimate_value = sprintf( '%d - %d %s', $from, $to, $to_days );
            } elseif ( $from && ! $to ) {
                $delivery_estimate_value = sprintf( __('At least %d %s', 'woocommerce' ), $from, $from_days );
            } else {
                $delivery_estimate_value = sprintf( __('Up to %d %s', 'woocommerce' ), $to, $to_days );
            }
        } else {

            $order_date = $order->get_date_created()->date_i18n('Y-m-d'); // Get Order date

            print_pr($order_date);

            if ( $from ) {
                $from_date = date_i18n( 'F d', strtotime($order_date) + ( $from * 24 * 3600 ) );
            }

            if ( $to ) {
                $to_date   = date_i18n( 'F d', strtotime($order_date) + ( $to * 24 * 3600 ) );
            }

            if ( $from && $to && $from != $to ) {
                $delivery_estimate_value = sprintf( '%s - %s', $from_date, $to_date );
            } elseif ( $from && ! $to ) {
                $delivery_estimate_value = sprintf( __('On or after %s', 'woocommerce' ), $from_date );
            } else {
                $delivery_estimate_value = sprintf( __('By %s', 'woocommerce' ), $to_date );
            }
        }

        $new_total_rows = array(); // Initializing

        // Loop through order total rows
        foreach( $total_rows as $key => $values ) {
            $new_total_rows[$key] = $values;

            // Inserting Delivery estimate array
            if( $key === 'shipping' ) {
                $new_total_rows['estimate'] = array(
                    'label' => __("Delivery estimate", "woocommerce"),
                    'value' => esc_html( $delivery_estimate_value )
                );
            }
        }
        return $new_total_rows;
    }
    return $total_rows;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399