【问题标题】:WooCommerce editable custom checkout field and displayed in formatted addressWooCommerce 可编辑的自定义结帐字段并以格式化地址显示
【发布时间】:2020-08-14 18:34:21
【问题描述】:

我正在向 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;

 }

一切正常。我想实现 2 项增强功能,但我做不到:

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

任何帮助将不胜感激

【问题讨论】:

    标签: php wordpress woocommerce checkout orders


    【解决方案1】:

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

    • 结帐
    • 我的账户 > 地址 > 编辑收货地址
    • 管理订单编辑页面

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

    // 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_fieldswoocommerce_billing_fields
    • woocommerce_admin_shipping_fieldswoocommerce_admin_billing_fields
    • woocommerce_order_formatted_shipping_addresswoocommerce_order_formatted_billing_address
    • (不要使用最后一个函数).

    对于前端:

    收到订单(谢谢)、订单付款和 myaccount / order-view,您将拥有 to override via your active theme 模板 order/order-details-customer.php

    您将在第 52 行之后的 html 标记 &lt;address&gt; 内添加以下内容:

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

    在管理员方面,发货电话会显示并可编辑:


    在查看订单、收到订单和电子邮件通知时,送货电话会显示在送货地址部分的末尾:

    【讨论】:

    • 嗨 Loic,感谢您的帮助。我已经测试过了。工作正常,除了 sphone 没有打印在电子邮件中
    • @ZeGregg 我暂时无法测试电子邮件通知...我会尽快回复您。
    • #LoicTheAztec 我只能在 Woo 4.01 的计费中看到电话。发货中没有电话:(
    • 超级奇怪。我检查了几个客户端安装,并且在管理员订单编辑中没有电话号码。我只有在帐单上。我还从前端检查了我的帐户,没有发货电话。这就是为什么客户问我这个功能:)
    • 这段代码在 woocommerce 店面主题上完美运行,正如提问者想要的那样……现在,计费和运输元数据按顺序保存,meta_key 始终以下划线开头……此数据也保存为用户带有 meta_key 且不以下划线开头的元数据。这就是 woocommerce 的工作原理……因此,要从订单中获取自定义计费元数据,需要一个 meta_key,例如“_billing_custom”(以下划线开头)。
    猜你喜欢
    • 2018-02-22
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2022-01-20
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多