Php 商业中基于产品数量生成校验字段的ACF中继器
我正在使用woocommerce高级签出字段插件,并在计费部分添加了一个转发器字段,如下所示 如上图所示,转发器字段“名称/电子邮件”适用于产品“皮带” 现在,当我从商店购买产品,并将数量设置为3,如下所示,repeater字段显示了3次,所有这些都很好 现在,当我下订单时,登录页上没有我输入的值,如下所示 此外,这些值不会显示在订单管理部分中,如下所示Php 商业中基于产品数量生成校验字段的ACF中继器,php,mysql,wordpress,woocommerce,custom-fields,Php,Mysql,Wordpress,Woocommerce,Custom Fields,我正在使用woocommerce高级签出字段插件,并在计费部分添加了一个转发器字段,如下所示 如上图所示,转发器字段“名称/电子邮件”适用于产品“皮带” 现在,当我从商店购买产品,并将数量设置为3,如下所示,repeater字段显示了3次,所有这些都很好 现在,当我下订单时,登录页上没有我输入的值,如下所示 此外,这些值不会显示在订单管理部分中,如下所示 我相信我已经清楚地阐述了这个问题。需要你的建议来解决这个问题,因为自从你开始悬赏以来,你没有回答任何评论。如果不知道您使用的设置是什么
我相信我已经清楚地阐述了这个问题。需要你的建议来解决这个问题,因为自从你开始悬赏以来,你没有回答任何评论。如果不知道您使用的设置是什么,以及与数据库相关的post元数据(已注册的元数据键)是什么,没有人能够解决您的问题 要根据不带任何插件的特定产品的项目数量为特定的自定义结账计费字段获取中继器,请执行以下操作: 1)产品附加设置(激活此功能的复选框):
//在产品编辑页面上显示自定义设置选项
添加操作(“商业、产品、选项、常规、产品、数据”、“添加产品、中继器、复选框、选项”);
函数添加\产品\中继器\复选框\选项(){
回声';
//自定义产品复选框字段
woocommerce\u wp\u复选框(数组(
“id'=>“\u数量\u中继器”,
“标签”=>“‘数量中继器’、‘woocommerce’”,
'description'=>\uuuuuu('为其他“名称”和“电子邮件”账单签出字段启用数量中继器','woocommerce'),
“描述提示”=>“正确”
));
回声';
}
//从产品编辑页面保存自定义设置选项值
添加操作('woocommerce\u admin\u process\u product\u object','save\u product\u repeater\u复选框\u option',100,1);
功能保存\产品\中继器\复选框\选项($product){
$qty\U REPLATER=isset($\U POST[''qty\U REPLATER'])?“是”:“否”;
$product->update_meta_数据(“_qty_repeater”,$qty_repeater);
}
2)在签出时添加/保存重复的附加字段(并标记订单):
add_filter('woocommerce_billing_fields','additional_billing_checkout_fields',50,1);
功能附加\账单\结帐\字段($账单\字段){
foreach(WC()->cart->get_cart()作为$cart_项目){
//检查是否为当前项目设置了“数量转发器”选项
if($cart\u item['data']->get\u meta('quantity\u repeater')==='yes'&&is\u checkout()&&$cart\u item['quantity']>1){
//数量中继器
对于($i=1,$j=2;$i<$cart_item['quantity'];$i++,$j++){
//名称字段
$billing\u字段['billing\u name\u person.$j]=数组(
'类型'=>'文本',
“标签”=>“(“名称”,“woocommerce”)。$j,
'class'=>array('form-row-first'),
“必需”=>true,
“清除”=>错误,
);
//电子邮件字段
$billing\u字段['billing\u email\u person.$j]=数组(
'键入'=>'电子邮件',
“标签”=>“(“电子邮件”、“woocommerce”)。$j,
'class'=>数组('form-row-last'),
“必需”=>true,
“清除”=>正确,
);
}
break;//仅用于一项
}
}
返回$billing\u字段;
}
//将订单标记为具有此附加字段数据值
添加操作('woocommerce\u checkout\u create\u order','save\u additional\u billing\u checkout\u fields',20,2);
函数保存\其他\账单\结帐\字段($order$data){
foreach($order->get_items()作为$item){
$product=$item->get_product();
//将订单标记为包含其他字段
如果($product->get_meta(“'U qty_repeater')=='yes'&&$item->get_qty()>1){
$item->update_meta_数据('u qty_repeater','1');
break;//停止循环
}
}
}
3)在任何地方显示其他账单字段相关数据(管理订单、订单视图、电子邮件):
//显示其他帐单字段值
添加操作('WOOMerce\u order\u details\u after\u order\u table','display\u additional\u billing\u fields\u value');//收到订单并查看
添加操作('woocommerce\u email\u在订单\u表之后,'display\u其他\u billing\u字段\u值');//电子邮件通知
添加操作('woocommerce_admin_order_data_after_billing_address','display_additional_billing_fields_values');//管理员编辑订单
功能显示\其他\账单\字段\值($order){
如果($order->get_meta(“'u qty_repeater')){
//仅适用于电子邮件通知
if(!(is_wc_endpoint_url()| | is_checkout()| | is_admin()){
回声'
table.customer-details{宽度:100%;字体系列:\'Helvetica Neue\',Helvetica,Roboto,Arial,无衬线;
颜色:#737373;边框:1px实心#e4;边距底部:40px;}
table.customer-details td{文本对齐:左;边框顶部宽度:4px;颜色:737373;边框:1px实心#e4;
填充:12px;填充底部:4px;}
';
}
//其他
否则{
echo'table.customer-details,table.customer-details td{border:none;}';
}
回显“”。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
回声';
//循环浏览订单项
foreach($order->get_items()作为$item){
$product=$item->get_product();
如果($product->get_meta(“'u qty_repeater')=='yes'){
// Display a custom setting option on product edit pages
add_action('woocommerce_product_options_general_product_data', 'add_product_repeater_checkbox_option');
function add_product_repeater_checkbox_option(){
echo '<div class="product_custom_field">';
// Custom Product Checkbox Field
woocommerce_wp_checkbox( array(
'id' => '_qty_repeater',
'label' => __('Qty repeater', 'woocommerce'),
'description' => __('Enable quantity repeater for additional "Name" and "Email" billing checkout fields', 'woocommerce'),
'desc_tip' => 'true'
));
echo '</div>';
}
// Save the custom setting option value from product edit pages
add_action( 'woocommerce_admin_process_product_object', 'save_product_repeater_checkbox_option', 100, 1 );
function save_product_repeater_checkbox_option( $product ) {
$qty_repeater = isset( $_POST['_qty_repeater'] ) ? 'yes' : 'no';
$product->update_meta_data( '_qty_repeater', $qty_repeater );
}
add_filter('woocommerce_billing_fields', 'additional_billing_checkout_fields', 50, 1 );
function additional_billing_checkout_fields( $billing_fields ) {
foreach(WC()->cart->get_cart() as $cart_item ){
// Check if the "Quanty repeater option is set for the current item
if( $cart_item['data']->get_meta('_qty_repeater') === 'yes' && is_checkout() && $cart_item['quantity'] > 1 ) {
// Quantity repeater
for( $i = 1, $j = 2; $i < $cart_item['quantity']; $i++, $j++ ){
// Name fields
$billing_fields['billing_name_person'.$j] = array(
'type' => 'text',
'label' => __("Name", "woocommerce") . ' ' . $j,
'class' => array('form-row-first'),
'required' => true,
'clear' => false,
);
// Email fields
$billing_fields['billing_email_person'.$j] = array(
'type' => 'email',
'label' => __("Email", "woocommerce") . ' ' . $j,
'class' => array('form-row-last'),
'required' => true,
'clear' => true,
);
}
break; // Only for one item
}
}
return $billing_fields;
}
// Mark Order as having this additional fields data values
add_action('woocommerce_checkout_create_order', 'save_additional_billing_checkout_fields', 20, 2);
function save_additional_billing_checkout_fields( $order, $data ) {
foreach( $order->get_items() as $item ){
$product = $item->get_product();
// Mark the order as containing additional fields
if( $product->get_meta('_qty_repeater') === 'yes' && $item->get_quantity() > 1 ) {
$item->update_meta_data( '_qty_repeater', '1' );
break; // Stop the loop
}
}
}
// Display additional billing fields values
add_action('woocommerce_order_details_after_order_table', 'display_additional_billing_fields_values' ); // Order received and view
add_action( 'woocommerce_email_after_order_table', 'display_additional_billing_fields_values' ); // Email notifications
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_additional_billing_fields_values' ); // Admin edit Order
function display_additional_billing_fields_values( $order ) {
if( $order->get_meta('_qty_repeater') ) {
// Only for email notifications
if( ! ( is_wc_endpoint_url() || is_checkout() || is_admin() ) ){
echo '<style>
table.customer-details {width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:40px;}
table.customer-details td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4;
padding: 12px; padding-bottom: 4px;}
</style>';
}
// Others
else {
echo '<style> table.customer-details, table.customer-details td { border: none; } </style>';
}
echo '<h2>' . __( 'Customer details', 'woocommerce' ) . '</h2>';
echo '<div><table class="customer-details" cellspacing="0">';
// Loop through order items
foreach( $order->get_items() as $item ){
$product = $item->get_product();
if( $product->get_meta('_qty_repeater') === 'yes' ) {
// Loop through item quantity
for( $i = 1, $j = 2; $i < $item->get_quantity(); $i++, $j++ ){
// Name
echo '<tr><td><strong>' . __("Name", "woocommerce") . ' ' . $j;
echo ': </strong>' . $order->get_meta('_billing_name_person'.$j) . '</td>';
// Email
echo '<td><strong>' . __("Email", "woocommerce") . ' ' . $j;
echo ': </strong>' . $order->get_meta('_billing_email_person'.$j) . '</td></tr>';
}
break;
}
}
echo '</table></div>';
}
}
add_filter( 'woocommerce_admin_billing_fields' , 'additional_admin_editable_billing_fields' );
function additional_admin_editable_billing_fields( $fields ) {
global $pagenow, $post;
if( $pagenow != 'post.php' ) return $fields;
$order = wc_get_order($post->ID);
if( $order->get_meta('_qty_repeater') ) {
// Loop through order items
foreach( $order->get_items() as $item ){
$product = $item->get_product();
if( $product->get_meta('_qty_repeater') === 'yes' ) {
// Loop through item quantity
for( $i = 1, $j = 2; $i < $item->get_quantity(); $i++, $j++ ){
$fields['name_person'.$j] = array(
'label' => __("Name", "woocommerce") . ' ' . $j,
'show' => false,
'wrapper_class' => 'first',
);
$fields['email_person'.$j] = array(
'label' => __("Email", "woocommerce") . ' ' . $j,
'show' => false,
'wrapper_class' => 'last',
);
}
break;
}
}
}
return $fields;
}