在订单和电子邮件中显示 WooCommerce Estimate Delivery 运输信息

Display WooCommerce Estimate Delivery shipping info in orders and emails

我正在尝试为 Woocommerce 在购物车、结帐、感谢页面和电子邮件中创建每个运输区域的预估送货。

我正在使用 WooCommerce Shipping Estimate 免费插件,它在购物车中显示效果很好,并可以查看预计送达时间。

现在插件不会在订单和电子邮件通知中显示…

我试图破解插件并添加一个挂钩的函数 woocommerce_thankyou_order_received_text 到目前为止没有成功。

有人能指出我正确的方向吗?

使用插件时 WooCommerce Shipping Estimate 可以在总计行的订单和电子邮件通知中显示“预计送达”,如下所示:

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;
}

代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。