WooCommerce可编辑自定义结账字段并在格式化地址中显示

4

我正在使用下面的代码将必填的送货电话添加到WooCommerce结算页面:

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

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

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>';
}

最后将其打印在电子邮件中

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;

 }

所有功能都可以正常运行。我希望实现两个增强但是我无法完成:

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

任何帮助都将不胜感激

woocoomerce email order

1个回答

6

你需要在代码中进行一些更改... 以下代码将显示运输电话字段:

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

该代码还将在电子邮件的送货地址部分的格式化显示的送货地址中添加运输电话。

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

代码放在您的活动子主题(或活动主题)的functions.php文件中。测试并有效。

对于计费自定义字段,您需要替换以下钩子:

  • woocommerce_shipping_fields 替换为 woocommerce_billing_fields
  • woocommerce_admin_shipping_fields 替换为 woocommerce_admin_billing_fields
  • woocommerce_order_formatted_shipping_address 替换为 woocommerce_order_formatted_billing_address
  • (不要使用最后一个函数)

对于前端终端点:

在“订单已收到(谢谢)”,“订单支付”和“我的帐户/订单查看”页面上,您将需要通过您的活动主题重写模板(参考文档) order/order-details-customer.php

您将在第52行之后的html标签<address>内添加以下内容:

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

在管理员界面上,运输电话号码会被显示并且可以编辑:

enter image description here


在订单查看、订单接收以及电子邮件通知中,运输电话号码会被显示在运输地址部分的末尾:

enter image description here


嗨,Loic,感谢你的帮助。我已经测试过了。除了sphone没有在电子邮件中打印出来之外,其他都很好。 - Greg
#LoicTheAztec 我只能在Woo 4.01的账单中看到电话号码,运输中没有 :( - Greg
让我们在聊天中继续这个讨论 - Greg
我可以确认,发货手机不是最新的WC核心的一部分。您的代码运行良好,除了电子邮件未在发送给客户和管理员的电子邮件中打印。 - Greg
1
这段代码在WooCommerce店面主题上完美运行,正如提问者所需的那样...现在,账单和运输元数据已保存在订单中,并带有一个始终以下划线开头的meta_key...此数据也保存为用户元数据,但其meta_key不会以下划线开头。这就是WooCommerce的工作方式...因此,要从订单中获取自定义账单元数据,需要使用类似于“_billing_custom”(以下划线开头)的meta_key。 - LoicTheAztec
显示剩余5条评论

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