![](/img/trans.png)
[英]Email recipients based on Shipping method Id for Woocommerce new order notification
[英]Hide shipping address in WooCommerce new order email notification depending on shipping method id
如果運輸標簽被稱為“在洛克菲勒商店取貨”(但在其他取貨方式中顯示),我想隱藏送貨地址。
有太多諸如“local_pickup:3”之類的 id 供我過濾。 盡管它是一種本地取貨方式,但我啟用了在emails/email-addresses.php 中顯示的送貨地址。
我的代碼嘗試:
<?php
/**
* Email Addresses
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/email-addresses.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates\Emails
* @version 3.9.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$text_align = is_rtl() ? 'right' : 'left';
$address = $order->get_formatted_billing_address();
$shipping = $order->get_formatted_shipping_address();
?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top; margin-bottom: 40px; padding:0;" border="0">
<tr>
<td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; border:0; padding:0;" valign="top" width="50%">
<h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
<address class="address">
<?php echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
<?php if ( $order->get_billing_phone() ) : ?>
<br/><?php echo wc_make_phone_clickable( $order->get_billing_phone() ); ?>
<?php endif; ?>
<?php if ( $order->get_billing_email() ) : ?>
<br/><?php echo esc_html( $order->get_billing_email() ); ?>
<?php endif; ?>
</address>
</td>
<?php if ( $shipping ) : ?>
<td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; padding:0;" valign="top" width="50%">
<h2><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
<address class="address"><?php echo wp_kses_post( $shipping ); ?></address>
</td>
<?php endif; ?>
</tr>
</table>
我需要啟用此功能,因為我們的其他一些取件方法需要它。 我試圖擁有它,以便如果它只是“在洛克菲勒商店取貨”,那么它應該在新訂單 WooCommerce 電子郵件中隱藏送貨地址。
我將如何根據其文本標簽對其進行過濾,如圖所示?
無需編輯模板文件,因為它可以通過鈎子進行。 這個答案由兩部分組成:
1)第一部分只需要執行一次,作為調試信息。 新訂單郵件中會出現以下文字
調試信息,運輸方式 ID = THE_SHIPPING_METHOD_ID
第1部分:
// DEBUG information
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
// Only for 'new order'
if ( $email->id == 'new_order' ) {
// Get shipping methods
foreach ( $order->get_shipping_methods() as $shipping_method ) {
// Get method ID
$shipping_method_id = $shipping_method->get_method_id();
// Output
echo '<p>DEBUG INFORMATION, shipping method id = ' . $shipping_method_id . '</p>';
}
}
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );
注意:在知道正確信息后,您不應再使用第 1 部分
2)然后你可以使用我的答案的第二部分,你需要用THE_SHIPPING_METHOD_ID
替換REPLACE_THIS_WITH_THE_SHIPPING_METHOD_ID
,該信息來自我的答案的第一部分
第2部分:
// Setting the email_is as a global variable
function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$GLOBALS['email_id_str'] = $email->id;
}
add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );
function filter_woocommerce_order_hide_shipping_address( $shipping_method_id, $order ) {
// Getting the email ID global variable
$refNameGlobalsVar = $GLOBALS;
$email_id = isset( $refNameGlobalsVar['email_id_str'] ) ? $refNameGlobalsVar['email_id_str'] : '';
// Only for 'new order'
if ( $email_id == 'new_order' ) {
// Replace the 2nd part in the array with the 'shipping_method_id' from the DEBUG INFORMATION
$shipping_method_id = array( 'local_pickup', 'REPLACE_THIS_WITH_THE_SHIPPING_METHOD_ID' );
} else {
// Default
$shipping_method_id = array( 'local_pickup' );
}
return $shipping_method_id;
}
add_filter( 'woocommerce_order_hide_shipping_address', 'filter_woocommerce_order_hide_shipping_address', 10, 2 );
注意:由於我的答案的第二部分中不知道電子郵件 ID,因此使用了一種解決方法,請參閱: https : //stackoverflow.com/a/43574008/11987538
$shipping_local_pickup = false;
if ( $items_totals = $order->get_order_item_totals() ) {
foreach ( $items_totals as $items_total ) {
if ( $items_total['value'] == 'Pick Up at Rockefeller Store' && !$shipping_local_pickup ) $shipping_local_pickup = true;
}
}
將此添加到我的模板中,然后將第 48 行設置為!$shipping_local_pickup
這允許我選擇表格中使用的短語“在洛克菲勒商店取貨”,並且如果存在,則不顯示它的送貨地址。
答案是從WooCommerce 電子郵件通知中的本地取貨時隱藏送貨地址中獲取和編輯的
尋找一種方法來隱藏本地取貨方法的地址和其他字段我看到這個回復https://wpsimplehacks.com/how-to-hide-woocommerce-checkout-fields-when-local-pickup-is-selected/
這是代碼,對我有用:
// Hide Local Pickup shipping method
add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' );
function hide_local_pickup_method( $fields_pickup ) {
// change below for the method
$shipping_method_pickup ='local_pickup:4';
// change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use
$hide_fields_pickup = array( 'billing_company', 'billing_country', 'billing_postcode', 'billing_address_1', 'billing_address_2' , 'billing_city', 'billing_state');
$chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_pickup = $chosen_methods_pickup[0];
foreach($hide_fields_pickup as $field_pickup ) {
if ($chosen_shipping_pickup == $shipping_method_pickup) {
$fields_pickup['billing'][$field_pickup]['required'] = false;
$fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup';
}
$fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
}
return $fields_pickup;
}
// Local Pickup - hide fields
add_action( 'wp_head', 'local_pickup_fields', 999 );
function local_pickup_fields() {
if (is_checkout()) :
?>
<style>
.hide_pickup {display: none!important;}
</style>
<script>
jQuery( function( $ ) {
if ( typeof woocommerce_params === 'undefined' ) {
return false;
}
$(document).on( 'change', '#shipping_method input[type="radio"]', function() {
// change local_pickup:4 accordingly
$('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'local_pickup:4');
});
});
</script>
<?php
endif;
}
如果您需要隱藏結帳頁面上的“發送到不同地址”,請嘗試以下操作:
add_action( 'woocommerce_after_checkout_form', 'hide_ship_to_section' );
function hide_ship_to_section( $available_gateways ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
?>
<script type="text/javascript">
jQuery('#customer_details .col-2').fadeOut();
</script>
<?php
}
?>
<script type="text/javascript">
jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
var val = jQuery( this ).val();
if (val.match("^local_pickup")) {
jQuery('#customer_details .col-2').fadeOut();
} else {
jQuery('#customer_details .col-2').fadeIn();
}
});
</script>
<?php
}
所有功勞都歸功於 Janek T,在 stackoverflow 上找不到他的個人資料
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.