WooCommerce 可编辑的自定义结帐字段并以格式化的地址显示 [英] WooCommerce editable custom checkout field and displayed in formatted address

查看:59
本文介绍了WooCommerce 可编辑的自定义结帐字段并以格式化的地址显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向 woocommerce 结帐页面添加强制送货电话

add_filter('woocommerce_checkout_fields', 'add_shipping_phone_to_checkout_page');函数 add_shipping_phone_to_checkout_page( $fields ) {$fields['shipping']['shipping_phone'] = array('标签' =>'电话','必需' =>真的,'类' =>数组('表格行宽'),'优先级' =>25,);返回 $fields;}

然后在管理订单面板中显示

add_action('woocommerce_admin_order_data_after_shipping_address', 'shipping_phone_checkout_display_in_order_panel');函数shipping_phone_checkout_display_in_order_panel( $order ){echo '<p><b>电话:</b>' .get_post_meta( $order->get_id(), '_shipping_phone', true ) .'</p>';}

最后在电子邮件中打印

add_action('woocommerce_email_customer_details','shipping_phone_display_in_order_email', 25, 4 );函数shipping_phone_display_in_order_email( $order, $sent_to_admin, $plain_text, $email ) {$输出 = '';$shipping_phone = get_post_meta( $order->id, '_shipping_phone', true );如果 ( !empty($shipping_phone) )$output = '<p><strong>'.__("电话:","woocommerce") .'</strong>' .$shipping_phone .'</p>';回声$输出;}

一切正常.我想实现 2 项增强,但我无法做到:

  1. 在管理面板中使自定义电话字段可编辑
  2. 在电子邮件中,移动送货地址块中的自定义电话字段值

任何帮助将不胜感激

解决方案

您需要对您的代码进行一些更改... 以下代码将在以下代码中显示送货电话字段:

  • 结帐
  • 我的帐户 >地址 >修改送货地址
  • 管理订单编辑页面

该代码还会将送货电话添加到电子邮件送货地址部分的格式化显示送货地址.

//在结帐时显示送货电话和我的帐户编辑送货地址add_filter('woocommerce_shipping_fields', 'add_shipping_phone_field');函数 add_shipping_phone_field( $fields ) {$fields['shipping_phone'] = 数组('标签' =>__('电话(送货)'),'必需' =>真的,'类' =>数组('表格行宽'),'优先级' =>25,);返回 $fields;}//管理订单编辑页面上的可编辑字段在编辑运输部分内add_filter( 'woocommerce_admin_shipping_fields' , 'add_order_admin_edit_shipping_phone' );函数 add_order_admin_edit_shipping_phone( $fields ) {//包括送货电话作为可编辑字段$fields['phone'] = array( 'label' => __("Shipping phone"), 'show' => '0');返回 $fields;}//仅在后端将自定义占位符添加到 woocommerce 格式的地址add_filter('woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1);功能 admin_localisation_address_formats( $address_formats ){//仅在后端(管理员)if( is_admin() || ! is_wc_endpoint_url() ) {foreach( $address_formats as $country_code => $address_format ) {$address_formats[$country_code] .= "\n{phone}";}}返回 $address_formats;}//自定义占位符替换 woocommerce 格式的地址add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );函数 custom_formatted_address_replacements( $replacements, $args ) {$replacements['{phone}'] = !空($args['phone'])?$args['phone'] : '';返回 $replacements;}//添加要在收货地址下的电子邮件通知中显示的收货电话值add_filter( 'woocommerce_order_formatted_shipping_address', 'add_shipping_phone_to_formatted_shipping_address', 100, 2 );函数 add_shipping_phone_to_formatted_shipping_address( $shipping_address, $order ) {全球 $pagenow, $post_type;//不在管理订单编辑页面上(因为它已经显示了).if( ! ( $pagenow === 'post.php' && $post_type === 'shop_order' && isset($_GET['action']) && $_GET['action'] === '编辑' ) ) {//在格式化的送货地址中包含送货电话$shipping_address['phone'] = $order->get_meta('_shipping_phone');}返回 $shipping_address;}//从帐单地址下的电子邮件通知(和管理员)中删除双重帐单电话add_filter( 'woocommerce_order_formatted_billing_address', 'remove_billing_phone_from_formatted_billing_address', 100, 2 );函数 remove_billing_phone_from_formatted_billing_address( $billing_address, $order ) {未设置($billing_address['phone']);返回 $billing_address;}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

<块引用>

对于计费自定义字段,您将替换挂钩:

  • woocommerce_shipping_fields by woocommerce_billing_fields
  • woocommerce_admin_shipping_fieldswoocommerce_admin_billing_fields
  • woocommerce_order_formatted_shipping_address 来自 woocommerce_order_formatted_billing_address
  • (不要使用最后一个函数).


对于前端:

在收到订单(谢谢)、订单付款和我的帐户/订单视图后,您将拥有


在订单视图、订单接收和电子邮件通知中,送货电话显示在送货地址部分的末尾:

I am adding mandatory shipping phone to woocommerce checkout page with

add_filter( 'woocommerce_checkout_fields', 'add_shipping_phone_to_checkout_page' );

function add_shipping_phone_to_checkout_page( $fields ) {
   $fields['shipping']['shipping_phone'] = array(
      'label' => 'Phone',
      'required' => true,
      'class' => array( 'form-row-wide' ),
      'priority' => 25,
   );
   return $fields;
}

then display it in admin order panel

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'shipping_phone_checkout_display_in_order_panel' );

function shipping_phone_checkout_display_in_order_panel( $order ){
    echo '<p><b>Phone :</b> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}

and finally print it in email

add_action('woocommerce_email_customer_details','shipping_phone_display_in_order_email', 25, 4 );

function shipping_phone_display_in_order_email( $order, $sent_to_admin, $plain_text, $email ) {

    $output = '';
    $shipping_phone = get_post_meta( $order->id, '_shipping_phone', true );

    if ( !empty($shipping_phone) )
        $output = '<p><strong>' . __( "Phone:", "woocommerce" ) . '</strong> ' . $shipping_phone . '</p>';

    echo $output;

 }

All works as it should. I'd like to achieve 2 enhancements but I am unable to do:

  1. Make the custom phone field editable in admin panel
  2. In email, move the custom phone field value in shipping address block

Any help would be appreciated

解决方案

You need to make some changes in your code… The following code will display the shipping phone field in:

  • Checkout
  • My Account > Address > Edit shipping address
  • Admin order edit pages

The code will also add the shipping phone to formatted displayed shipping address on emails shipping address section.

// display shipping phone in checkout and my account edit shipping address
add_filter( 'woocommerce_shipping_fields', 'add_shipping_phone_field' );
function add_shipping_phone_field( $fields ) {
   $fields['shipping_phone'] = array(
      'label' => __('Phone (Shipping)'),
      'required' => true,
      'class' => array( 'form-row-wide' ),
      'priority' => 25,
   );
   return $fields;
}

// Editable field on admin order edit pages inside edit shipping section
add_filter( 'woocommerce_admin_shipping_fields' , 'add_order_admin_edit_shipping_phone' );
function add_order_admin_edit_shipping_phone( $fields ) {
    // Include shipping phone as editable field
    $fields['phone'] = array( 'label' => __("Shipping phone"), 'show' => '0' );

    return $fields;
}

// Adding custom placeholder to woocommerce formatted address only on Backend
add_filter( 'woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1 );
function admin_localisation_address_formats( $address_formats ){
    // Only in backend (Admin)
    if( is_admin() || ! is_wc_endpoint_url() ) {
        foreach( $address_formats as $country_code => $address_format ) {
            $address_formats[$country_code] .= "\n{phone}";
        }
    }
    return $address_formats;
}

// Custom placeholder replacement to woocommerce formatted address
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args  ) {
    $replacements['{phone}'] = ! empty($args['phone']) ? $args['phone'] : '';

    return $replacements;
}

// Add the shipping phone value to be displayed on email notifications under shipping address
add_filter( 'woocommerce_order_formatted_shipping_address', 'add_shipping_phone_to_formatted_shipping_address', 100, 2 );
function add_shipping_phone_to_formatted_shipping_address( $shipping_address, $order ) {
    global $pagenow, $post_type;

    // Not on admin order edit pages (as it's already displayed).
    if( ! ( $pagenow === 'post.php' && $post_type === 'shop_order' && isset($_GET['action']) && $_GET['action'] === 'edit' ) ) {
        // Include shipping phone on formatted shipping address
        $shipping_address['phone'] = $order->get_meta('_shipping_phone');
    }
    return $shipping_address;
}

// Remove double billing phone from email notifications (and admin) under billing address
add_filter( 'woocommerce_order_formatted_billing_address', 'remove_billing_phone_from_formatted_billing_address', 100, 2 );
function remove_billing_phone_from_formatted_billing_address( $billing_address, $order ) {
    unset($billing_address['phone']);
  
    return $billing_address;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

For billing custom fields, you will replace the hooks:

  • woocommerce_shipping_fields by woocommerce_billing_fields
  • woocommerce_admin_shipping_fields by woocommerce_admin_billing_fields
  • woocommerce_order_formatted_shipping_address by woocommerce_order_formatted_billing_address
  • (don't use the last function).


For the front endpoints:

On order received (thank you), order-pay, and myaccount / order-view, you will have to override via your active theme the template order/order-details-customer.php.

You will add inside the html tag <address> after line 52 the following:

        <?php if ( $shipping_phone = $order->get_meta('_shipping_phone') ) : ?>
            <p class="woocommerce-customer-details--phone"><?php echo esc_html( $shipping_phone ); ?></p>
        <?php endif; ?>


On admin side, the shipping phone is displayed and editable:


On order-view, order-received and email notifications, the shipping phone is displayed at the end of the shipping address section:

这篇关于WooCommerce 可编辑的自定义结帐字段并以格式化的地址显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆