Add a new custom field to WooCommerce checkout and display on admin order pages and email notifications(将新的自定义字段添加到WooCommerce结帐并显示在管理订单页面和电子邮件通知上)
本文介绍了将新的自定义字段添加到WooCommerce结帐并显示在管理订单页面和电子邮件通知上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有应该可以工作的代码,但由于某些原因它不能工作。我想使用以下代码在WooCommerce结帐下添加一个名为"City"的新自定义字段:
// Display a custom checkout select field after Billing form
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">
' . __('City') . '';
woocommerce_form_field( 'delivery_date', array(
'type' => 'select',
'options' => array(
'Colombo 01' => __('Colombo 01', 'woocommerce' ),
'Colombo 02' => __('Colombo 02', 'woocommerce' ),
'Colombo 03' => __('Colombo 03', 'woocommerce' ),
'Colombo 04' => __('Colombo 04', 'woocommerce' ),
'Colombo 06' => __('Colombo 06', 'woocommerce' ),
'Colombo 07' => __('Colombo 07', 'woocommerce' ),
'Colombo 08' => __('Colombo 08', 'woocommerce' ),
'Colombo 09' => __('Colombo 09', 'woocommerce' ),
'Colombo 10' => __('Colombo 10', 'woocommerce' ),
'Colombo 11' => __('Colombo 11', 'woocommerce' ),
'Colombo 01' => __('Colombo 01', 'woocommerce' ),
'Dehiwela' => __('Dehiwela', 'woocommerce' ),
'Attidiya' => __('Attidiya', 'woocommerce' ),
'Mount Lavinia' => __('Mount Lavinia', 'woocommerce' ),
'Ratmalana' => __('Ratmalana', 'woocommerce' ),
'Maligawatte' => __('Maligawatte', 'woocommerce' ),
'Kotikawatta' => __('Kotikawatta', 'woocommerce' ),
'Rajagiriya' => __('Rajagiriya', 'woocommerce' ),
'Peliyagoda' => __('Peliyagoda', 'woocommerce' ),
'Kelaniya' => __('Kelaniya', 'woocommerce' ),
'Kiribathgoda' => __('Kiribathgoda', 'woocommerce' ),
'Wattala' => __('Wattala', 'woocommerce' )),
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Select City'),
), $checkout->get_value( 'city_custom' ));
echo '</div>';
}
// Save the dropdown custom field selected value as order custom meta data:
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
if ( isset($_POST['city_custom']) && ! empty($_POST['city_custom']) ) {
$order->update_meta_data( 'City', sanitize_text_field( $_POST['city_custom'] ) );
}
}
// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city_custom') . '</p>';
}
// Display the custom field value on email notifications:
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city_custom') . '</p>';
}
但是由于某些原因,我无法将订单详细信息查看到后端(必须将City作为标签,但没有从客户那里选择数据),也无法查看邮件中的订单详细信息。也可以将这个值显示到其他帐单信息中吗?有人来帮忙吗?我使用的是最新的WooCommerce 4.0.1
非常感谢
推荐答案
您将
delivery_date
、city_custom
和amp;City
混淆使用,这会为您提供不同的值,因此这就是您的问题
// Display a custom checkout select field after Billing form
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field', 10, 1 );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">
' . __('City') . '';
woocommerce_form_field( 'city_custom', array(
'type' => 'select',
'options' => array(
'Colombo 01' => __('Colombo 01', 'woocommerce' ),
'Colombo 02' => __('Colombo 02', 'woocommerce' ),
'Colombo 03' => __('Colombo 03', 'woocommerce' ),
'Colombo 04' => __('Colombo 04', 'woocommerce' ),
'Colombo 06' => __('Colombo 06', 'woocommerce' ),
'Colombo 07' => __('Colombo 07', 'woocommerce' ),
'Colombo 08' => __('Colombo 08', 'woocommerce' ),
'Colombo 09' => __('Colombo 09', 'woocommerce' ),
'Colombo 10' => __('Colombo 10', 'woocommerce' ),
'Colombo 11' => __('Colombo 11', 'woocommerce' ),
'Colombo 01' => __('Colombo 01', 'woocommerce' ),
'Dehiwela' => __('Dehiwela', 'woocommerce' ),
'Attidiya' => __('Attidiya', 'woocommerce' ),
'Mount Lavinia' => __('Mount Lavinia', 'woocommerce' ),
'Ratmalana' => __('Ratmalana', 'woocommerce' ),
'Maligawatte' => __('Maligawatte', 'woocommerce' ),
'Kotikawatta' => __('Kotikawatta', 'woocommerce' ),
'Rajagiriya' => __('Rajagiriya', 'woocommerce' ),
'Peliyagoda' => __('Peliyagoda', 'woocommerce' ),
'Kelaniya' => __('Kelaniya', 'woocommerce' ),
'Kiribathgoda' => __('Kiribathgoda', 'woocommerce' ),
'Wattala' => __('Wattala', 'woocommerce' )),
'class' => array('my-field-class form-row-wide'),
'placeholder' => __('Select City'),
), $checkout->get_value( 'city_custom' ));
echo '</div>';
}
// Save the dropdown custom field selected value as order custom meta data:
add_action( 'woocommerce_checkout_create_order', 'my_custom_checkout_field_update_order_meta', 10, 2 );
function my_custom_checkout_field_update_order_meta( $order, $data ) {
if ( isset($_POST['city_custom']) && ! empty($_POST['city_custom']) ) {
$order->update_meta_data( 'city', sanitize_text_field( $_POST['city_custom'] ) );
}
}
// Display the custom field value on admin order pages after billing adress:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city') . '</p>';
}
// Display the custom field value on email notifications:
add_action( 'woocommerce_email_after_order_table', 'custom_woocommerce_email_order_meta_fields', 10, 4 );
function custom_woocommerce_email_order_meta_fields( $order, $sent_to_admin, $plain_text, $email ) {
echo '<p><strong>'.__('City').':</strong> ' . $order->get_meta('city') . '</p>';
}
这篇关于将新的自定义字段添加到WooCommerce结帐并显示在管理订单页面和电子邮件通知上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:将新的自定义字段添加到WooCommerce结帐并显示在管理订单页面和电子邮件通知上
基础教程推荐
猜你喜欢
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01