Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 可编辑的自定义签出字段,并以格式化地址显示_Php_Wordpress_Woocommerce_Checkout_Orders - Fatal编程技术网

Php 可编辑的自定义签出字段,并以格式化地址显示

Php 可编辑的自定义签出字段,并以格式化地址显示,php,wordpress,woocommerce,checkout,orders,Php,Wordpress,Woocommerce,Checkout,Orders,我正在将强制送货电话添加到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',

我正在将强制送货电话添加到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_admin_order_data_在_shipping_address之后,'shipping_phone_checkout_在_order_面板中显示');
功能发货\手机\结账\显示\订单\面板($order){
回显“Phone:”。获取发布元($order->get\u id(),“'u shipping\u Phone',true)。”

; }
最后在电子邮件中打印出来

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;

 }

add_action('woocommerce_email_customer_details','shipping_phone_display_in_order_email',25,4);
函数shipping\u phone\u display\u in\u order\u email($order,$sent\u to\u admin,$plain\u text,$email){
$output='';
$shipping\u phone=get\u post\u meta($order->id,'u shipping\u phone',true);
如果(!空($shipping_phone))
$output=“”。uuu(“电话:”,“woocommerce”)。“$shipping\u电话”。

”; echo$输出; }
一切正常。我想实现两个增强功能,但我无法做到:

  • 使“管理”面板中的“自定义电话”字段可编辑
  • 在电子邮件中,移动“配送地址”块中的“自定义电话”字段值
  • 任何帮助都将不胜感激


    您需要对代码进行一些更改…以下代码将在中显示shipping phone(发货电话)字段:

    • 结帐
    • 我的帐户>地址>编辑发货地址
    • 管理命令编辑页面
    该代码还将把配送电话添加到电子邮件配送地址部分显示的格式化的配送地址中

    // 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\u shipping\u字段
      by
      woocommerce\u billing\u字段
    • woocommerce\u admin\u shipping\u字段
      by
      woocommerce\u admin\u billing\u字段
    • woocmerce\u order\u formatted\u shipping\u address
      by
      woocmerce\u order\u formatted\u billing\u address
    • (不要使用最后一个函数)

    对于前端端点:

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

    您将在html标记中添加


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


    您需要对代码进行一些更改…以下代码将在中显示shipping phone(发货电话)字段:

    • 结帐
    • 我的帐户>地址>编辑发货地址
    • 管理命令编辑页面
    该代码还将把配送电话添加到电子邮件配送地址部分显示的格式化的配送地址中

    // 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\u shipping\u字段
      by
      woocommerce\u billing\u字段
    • woocommerce\u admin\u shipping\u字段
      by
      woocommerce\u admin\u billing\u字段
    • woocmerce\u order\u formatted\u shipping\u address
      by
      woocmerce\u order\u formatted\u billing\u address
    • (不要使用最后一个函数)

    对于前端端点:

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

    您将在html标记中添加


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


    你好,洛伊奇,谢谢你的帮助。我已经测试过了。作品很好,但sphone未打印在emails@ZeGregg我暂时无法测试电子邮件通知…我会尽快返回给您。#LoicTheAztec我只能在Woo 4.01中看到电话计费。发货时没有电话:(非常奇怪。我检查了一些客户端安装,在管理命令编辑中没有电话号码。我只检查了账单。我还检查了前端的我的帐户,没有发货电话。这就是为什么一个客户端问我这个功能:)此代码在woocommerce店面主题上非常有效,正如asker所希望的那样……现在,账单和发货元数据以始终以下划线开头的meta_键的顺序保存……此数据也以meta_键的形式保存为用户元数据,而不以下划线开头。这就是woocommerce的工作原理…因此,要从订单中获取自定义计费元数据,需要一个元数据密钥,如“\u billing\u custom”(以下划线开头)。嗨,Loic,谢谢你的帮助。我已经测试过了。作品很好,但sphone未打印在emails@ZeGregg我暂时无法测试电子邮件通知…我会尽快返回给您。#LoicTheAztec我只能在Woo 4.01中看到电话计费。发货时没有电话:(非常奇怪。我检查了一些客户端安装,在管理命令编辑中没有电话号码。我只检查了账单。我还检查了前端的我的帐户,没有发货电话。这就是为什么一个客户端问我这个功能:)此代码在woocommerce店面主题上非常有效,正如asker所希望的那样……现在,账单和发货元数据以始终以下划线开头的meta_键的顺序保存……此数据也以meta_键的形式保存为用户元数据,而不以下划线开头。这就是woocommerce的工作原理……因此,要从订单中获取自定义计费元数据,需要一个元数据键,如“\u billing\u custom”(以下划线开头)。