问题描述
我正在尝试在 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',$to,'woocommerce' );
}
if ( $format === 'days' ) {
if ( $from && $to && $from != $to ) {
$delivery_estimate_value = sprintf( '%d - %d %s',$to_days );
} elseif ( $from && ! $to ) {
$delivery_estimate_value = sprintf( __('At least %d %s','woocommerce' ),$from_days );
} else {
$delivery_estimate_value = sprintf( __('Up to %d %s',$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',$from_date );
} else {
$delivery_estimate_value = sprintf( __('By %s',$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 文件中。经测试有效。