【问题标题】:Display WooCommerce Estimate Delivery shipping info in orders and emails在订单和电子邮件中显示 WooCommerce Estimate Delivery 运输信息
【发布时间】:2021-05-12 09:08:31
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • @LoicTheAztec 实际上,我在这里关注您的代码stackoverflow.com/questions/52747221/… 但是每当我尝试添加 add_action( 'woocommerce_thankyou_order_received_text' , 'shipping_zone_targeted_postcodes_custom_notice' );它没有显示在感谢页面上,不知道下一步该怎么做。
  • 我已经尝试了该插件,并找到了在订单和电子邮件通知中显示“交货估算”的方法,该通知显示在运输总额之后的总行上。请参阅下面的我的答案。

标签: php wordpress woocommerce plugins shipping


【解决方案1】:

当使用插件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 文件中。经过测试并且可以工作。

【讨论】:

  • 非常感谢!所以我一直都做错了:D它真的有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 2018-08-15
  • 2018-06-07
相关资源
最近更新 更多