[英]WooCommerce email based on shipping zone
當客戶選擇送貨zone id = 0
(世界其他地區)時,我需要發送電子郵件說明。
我在下面找到了代碼,但它基於付款方式:
add_action( 'woocommerce_email_before_order_table', 'add_order_email_instructions', 10, 2 );
function add_order_email_instructions( $order, $sent_to_admin ) {
if ( ! $sent_to_admin ) {
if ( 'cod' == $order->payment_method ) {
// cash on delivery method
echo '<p><strong>Instructions:</strong> Full payment is due immediately upon delivery: <em>cash only, no exceptions</em>.</p>';
} else {
// other methods (ie credit card)
echo '<p><strong>Instructions:</strong> Please look for "Madrigal Electromotive GmbH" on your next credit card statement.</p>';
}
}
}
請問如何將其更改為特定的送貨區域?
如何根據 WooCommerce 中的送貨區域設置特定的電子郵件說明?
對此的任何幫助將不勝感激。
獲取訂單的送貨區域並非易事。 可以通過以下代碼示例來完成:
add_action( 'woocommerce_email_before_order_table', 'custom_text_in_email_shipping_zone_based', 10, 4 );
function custom_text_in_email_shipping_zone_based( $order, $sent_to_admin, $plain_text, $email ) {
if ( ! $sent_to_admin ) {
// Get the shipping method related data (we need the Instance ID)
$shipping_item = $order->get_items('shipping');
$item = reset($shipping_item);
$shipping_method_id = $item->get_method_id();
$method_arr = explode( ':', $shipping_method_id );
// Get the Zone ID and related data
$shipping_zone_object = WC_Shipping_Zones::get_zone_by( 'instance_id', $method_arr[1] );
$zone_id = $shipping_zone_object->get_id(); // Zone ID
$zone_name = $shipping_zone_object->get_zone_name(); // Zone name
// Get the zone locations codes and types (if needed)
foreach( $shipping_zone_object->get_zone_locations() as $zone_location ){
$zone_location_code = $zone_location->code;
$zone_location_type = $zone_location->type;
}
if ( '0' == $zone_id ) {
// Rest of the world
echo '<p><strong>Instructions:</strong> for Rest of the world.</p>';
}
elseif ( 'Mexico' == $zone_name ) {
// Mexico zone name
echo '<p><strong>Instructions:</strong> for Mexico.</p>';
}
else {
// All other zones
echo '<p><strong>Instructions:</strong> Other zones.</p>';
}
}
}
代碼位於活動子主題(或活動主題)的 function.php 文件中。
測試和工作。
根據WC_Abstract_Order
頁面https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html#_has_shipping_method您可以使用has_shipping_method()
檢查運輸方式。
所以,你的 if 語句應該是這樣的
if ( $order->has_shipping_method('name_of_my_shipping_method') ) {
不過,我找不到對應的運輸區域。
希望有幫助
add_action('woocommerce_email_before_order_table', 'custom_text_in_email_shipping_zone_based', 10, 4); 函數 custom_text_in_email_shipping_zone_based( $order, $sent_to_admin, $plain_text, $email ) { if ( ! $sent_to_admin ) {
// Get the shipping method related data (we need the Instance ID)
$shipping_item = $order->get_items('shipping');
$item = reset($shipping_item);
$shipping_method_id = $item->get_method_id();
$method_arr = explode( ':', $shipping_method_id );
// Get the Zone ID and related data
$shipping_zone_object = WC_Shipping_Zones::get_zone_by( 'instance_id', $method_arr[1] );
$zone_id = $shipping_zone_object->get_id(); // Zone ID
$zone_name = $shipping_zone_object->get_zone_name(); // Zone name
// Get the zone locations codes and types (if needed)
foreach( $shipping_zone_object->get_zone_locations() as $zone_location ){
$zone_location_code = $zone_location->code;
$zone_location_type = $zone_location->type;
}
if ( '0' == $zone_id ) {
// Rest of the world
echo '<p><strong>Instructions:</strong> for Rest of the world.</p>';
}
elseif ( 'Mexico' == $zone_name ) {
// Mexico zone name
echo '<p><strong>Instructions:</strong> for Mexico.</p>';
}
else {
// All other zones
echo '<p><strong>Instructions:</strong> Other zones.</p>';
}
}
}
即使作者已經聲明嘗試和測試,上面顯示的代碼也不起作用,任何人都可以更新代碼。
無論您選擇哪個區域,您最終都會選擇所有其他區域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.