根据运输方式ID,在WooCommerce新订单电子邮件通知中隐藏运输地址。

3
如果货运标签被命名为“在洛克菲勒店内取货”(但其他取货方式需要显示),我希望隐藏运输地址。
有太多的id,例如“local_pickup:3”,我无法筛选。尽管它是本地自提方式,但我在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>

由于我们的其他取货方式需要此选项,因此我需要启用它。如果订单选择了“Rockefeller店铺取货”,那么在新订单WooCommerce电子邮件中应该隐藏运送地址。

如何基于其文本标签(如图片所示)过滤此选项?

enter image description here

3个回答

2

不需要编辑模板文件,可以通过钩子实现。此答案包含2个部分:

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

注意:在了解正确信息后,您应该不再使用第一部分


2)然后您可以使用我的答案的第二部分,其中您需要用THE_SHIPPING_METHOD_ID替换REPLACE_THIS_WITH_THE_SHIPPING_METHOD_ID该信息来自我的答案的第一部分

第二部分:

// 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


谢谢您的回复,但我的问题是我有两种本地自取方式。一种叫做“在洛克菲勒店自取”,另一种叫做“信使服务”。尽管“信使服务”不是技术上的本地自取,但我需要它成为本地自取,以便在结账页面共享日历。所以我的问题是,由于它们都相同,如何更深入地区分这两种本地自取方法的ID?是否有一种方法可以调用$shipping_method_id = $shipping_method->get_method_value()或$shipping_method_id = $shipping_method->get_method_label();? - Buttsilio
1
@Buttsilio 是的,这是完全可能的。但是,我看到您已经通过修改模板文件来应用解决方案(这也是可能的,但不太建议,考虑到 WooCommerce 的未来更新)。 我认为您的问题只涉及新订单电子邮件通知,而这似乎没有在您的答案中得到考虑? - 7uc1f3r

0

寻找一种方法来隐藏本地自取方式的地址和其他字段,我看到了这个回复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上找不到他的个人资料。


0
$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电子邮件通知中隐藏本地取货的送货地址,并进行了编辑。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接