wordpress - 如何根据数量产品创建自定义字段到 wc 结帐字段,保存并显示到管理订单页面和 dokan 订单页面?
问题描述
我需要在 WC 结帐表单中添加一些自定义字段。
我尝试了很多方法,这种方法效果很好:
//Custom WooCommerce Checkout Fields based on Quantity
add_action('woocommerce_before_order_notes', 'person_details');
function person_details($checkout)
{
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 1;
for ($k = 2; $k <= $count; ++$k) {
++$i;
echo '<h3>Please enter details of attendee '.$i.'</h3>';
woocommerce_form_field('cstm_full_name'.$i, [
'type' => 'text',
'class' => ['my-field-class form-row-wide'],
'label' => __('Full name'),
'placeholder' => __('Enter full name'),
'required' => true,
],
$checkout->get_value('cstm_full_name'.$i));
echo '<div class="clear"></div>';
woocommerce_form_field('cstm_phone'.$i, [
'type' => 'text',
'class' => ['my-field-class form-row-first'],
'label' => __('Phone'),
'placeholder' => __('Enter phone number'),
'required' => true,
],
$checkout->get_value('cstm_phone'.$i));
woocommerce_form_field('cstm_email'.$i, [
'type' => 'email',
'class' => ['my-field-class form-row-last'],
'label' => __('Email address'),
'placeholder' => __('Enter email address'),
'required' => true,
],
$checkout->get_value('cstm_email'.$i));
echo '<div class="clear"></div>';
woocommerce_form_field('cstm_address'.$i, [
'type' => 'textarea',
'class' => ['my-field-class form-row-wide'],
'label' => __('Full address'),
'placeholder' => __('Enter full address'),
'required' => true,
],
$checkout->get_value('cstm_address'.$i));
}
}
并使用此代码段保存:
/*
* Save value of fields
*/
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta');
function customise_checkout_field_update_order_meta($order_id)
{
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 1;
for ($k = 2; $k <= $count; ++$k) {
++$i;
if (!empty($_POST['cstm_full_name'.$i])) {
update_post_meta($order_id, 'Name of Attendee'.$i, sanitize_text_field($_POST['cstm_full_name'.$i]));
}
if (!empty($_POST['cstm_phone'.$i])) {
update_post_meta($order_id, 'Phone of Attendee'.$i, sanitize_text_field($_POST['cstm_phone'.$i]));
}
if (!empty($_POST['cstm_email'.$i])) {
update_post_meta($order_id, 'Email of Attendee'.$i, sanitize_text_field($_POST['cstm_email'.$i]));
}
if (!empty($_POST['cstm_address'.$i])) {
update_post_meta($order_id, 'Address of Attendee'.$i, sanitize_text_field($_POST['cstm_address'.$i]));
}
}
}
并将订单电子邮件与此一起:
/*
* Add fields to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys($keys)
{
$i = 1;
for ($k = 2; $k <= 50; ++$k) {
++$i;
$keys[] = 'Name of Attendee'.$i;
$keys[] = 'Phone of Attendee'.$i;
$keys[] = 'Email of Attendee'.$i;
$keys[] = 'Email of Participant '.$i;
$keys[] = 'Address of Attendee'.$i;
}
return $keys;
}
然后对于验证字段使用这个:
/*
* For data validation of the custom field.
*/
add_action('woocommerce_checkout_process', 'customise_checkout_field_process');
function customise_checkout_field_process()
{
global $woocommerce;
$count = $woocommerce->cart->cart_contents_count;
$i = 1;
for ($k = 2; $k <= $count; ++$k) {
++$i;
// if the field is set, if not then show an error message.
if (!$_POST['cstm_full_name'.$i]) {
wc_add_notice(__('نام و نام خانوادگی شخص '.$i.' الزامیست.'), 'error');
}
}
}
现在,我需要将所有保存的字段放入管理订单页面和 Dokan 订单页面,例如地址和其他默认详细信息。
我应该如何更改此代码?
解决方案
推荐阅读
- python - 如何创建一个元组的元组,其中在python中分配了变量
- terraform - 每个循环或 for 循环的 Terraform 不起作用
- java - 对 NamedParameterJdbcTemplate 的引用不明确,batchUpdate(String,Map
[]) 和 batchUpdate(String,SqlParameterSource[]) 匹配 - c++ - 如何用 CUB 库编译 C++?
- css - 布尔玛限制?
- python - DeepSearch - 仅返回索引号
- qt - QML 没有从另一个文件中的单例获取属性
- javascript - jquery嵌套时找不到id,但不嵌套时找到
- r - 根据R中同一行的另一列中单元格的值对一列求和
- c++ - 在 Mac OS X 上将 Docker 映像转换为二进制文件