Woocommerce 单个产品页面中的自定义字段验证 [英] Custom field validation in Woocommerce single product pages
问题描述
我想创建一个带有附加文本输入字段的 Woocommerce 产品,用于检查输入的值是否对该字段唯一,否则会输出一条消息.
换句话说,如果我输入dave"并且dave"是由其他用户提交的,那么我将无法继续购买.
任何帮助将不胜感激,我不知道从哪里开始.
这可以以非常简单的方式完成,使用 3 个小挂钩函数:
- 第一个,在单个产品页面添加到购物车按钮之前添加自定义输入文本字段
- 第二个进行验证,检查它是否是唯一值
- 第三个在验证后将值保存为现有值数组中的产品元数据
将针对当前产品验证提交的值...
代码:
//添加到购物车按钮前的产品自定义字段 - 前端add_action('woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button');函数 action_before_add_to_cart_button() {全球$产品;echo '';woocommerce_form_field( 'custom_unique', array('类型' =>'文本','类' =>数组('我的字段类表单行宽'),'标签' =>__('标签名称'),'占位符' => __('请输入...'),'必需' =>真的,), '' );//用于测试:显示现有提交的值(注释 - 非活动)//print_r( get_post_meta( $product->get_id(), '_custom_unique_values', true ) );回声'</div><br>';}//字段验证(检查)add_filter('woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 20, 3);函数 filter_add_to_cart_validation( $passed, $product_id, $quantity ) {//获取要检查的自定义字段值$custom_unic_values = (array) get_post_meta( $product_id, '_custom_unique_values', true );//检查值是否唯一if( in_array( $_POST['custom_unique'], $custom_unic_values ) ){$passed = false ;//当值存在时设置为false//显示自定义消息$message = sprintf( __( '值 "%s" 已经存在,尝试别的东西...', 'woocommerce'), sanitize_text_field( $_POST['custom_unique'] ) );wc_add_notice( $message, 'error' );}返回 $passed;}//将新的唯一值保存在值数组中(作为产品元数据)add_action( 'woocommerce_add_to_cart', 'action_add_to_cart', 20, 6 );function action_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){if(isset($_POST['custom_unique'])){//获取现有值的数组$custom_unic_values = (array) get_post_meta( $product_id, '_custom_unique_values', true );//将新值附加到值数组$custom_unic_values[] = sanitize_text_field( $_POST['custom_unique'] );//保存附加的数组update_post_meta( $product_id, '_custom_unique_values', $custom_unic_values );}}代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
I would like to create a Woocommerce product with an additional text input field which checks if the value entered is unique to that field otherwise outputs a message.
In other words if I enter "dave" and "dave" has been submitted by a different user then I cant proceed with the purchase.
Any help would be much appreciated,
I dont know where to start on this one.
解决方案 This can be done in a very simple way with 3 small hooked functions:
- The 1st one, add the custom input text field before add to cart button in single product pages
- The 2nd one make the validation, checking that is a unique value
- The 3rd one save the value as product meta data in the array of existing values after validation
The submitted value will be verified for the current product...
The code:
// The product custom field before add-to-cart button - Frontend
add_action( 'woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button' );
function action_before_add_to_cart_button() {
global $product;
echo '<div>';
woocommerce_form_field( 'custom_unique', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('The label name'),
'placeholder' =>__('Please enter …'),
'required' => true,
), '' );
// For test: displaying existing submitted values (commented - inactive)
// print_r( get_post_meta( $product->get_id(), '_custom_unique_values', true ) );
echo '</div><br>';
}
// Field validation (Checking)
add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 20, 3 );
function filter_add_to_cart_validation( $passed, $product_id, $quantity ) {
// Get the custom field values to check
$custom_unic_values = (array) get_post_meta( $product_id, '_custom_unique_values', true );
// Check that the value is unique
if( in_array( $_POST['custom_unique'], $custom_unic_values ) ){
$passed = false ; // Set as false when the value exist
// Displaying a custom message
$message = sprintf( __( 'The value "%s" already exist, try something else…', 'woocommerce' ), sanitize_text_field( $_POST['custom_unique'] ) );
wc_add_notice( $message, 'error' );
}
return $passed;
}
// Save the new unique value in the array of values (as product meta data)
add_action( 'woocommerce_add_to_cart', 'action_add_to_cart', 20, 6 );
function action_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
if( isset($_POST['custom_unique']) ){
// Get the array of existing values
$custom_unic_values = (array) get_post_meta( $product_id, '_custom_unique_values', true );
// append the new value to the array of values
$custom_unic_values[] = sanitize_text_field( $_POST['custom_unique'] );
// Save the appended array
update_post_meta( $product_id, '_custom_unique_values', $custom_unic_values );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于Woocommerce 单个产品页面中的自定义字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文