我将强制运送电话添加到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种增强功能,但我无法做到:
任何帮助将不胜感激
您需要对代码进行一些更改。以下代码将在以下位置显示运送电话字段:
该代码还将把运送电话添加到电子邮件运送地址部分中格式化的显示运送地址。
// 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_sphone'] = array(
'label' => __('Phone'),
'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['sphone'] = array( 'label' => __("Shipping phone"), 'show' => '0' );
return $fields;
}
// Adding custom placeholder to woocommerce formatted billing 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() ) {
foreach( $address_formats as $country_code => $address_format ) {
$address_formats[$country_code] .= "\n{sphone}";
}
}
return $address_formats;
}
// Custom placeholder replacement to woocommerce formatted billing address
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args ) {
$replacements['{sphone}'] = ! empty($args['sphone']) ? $args['sphone'] : '';
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', 990, 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['sphone'] = $order->get_meta('_shipping_sphone');
}
return $shipping_address;
}
代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。