Php Woocommerce单个产品页面中的自定义字段验证
我想创建一个Woocommerce产品,带有一个额外的文本输入字段,用于检查输入的值是否是该字段的唯一值,否则将输出一条消息 换句话说,如果我输入“dave”并且“dave”已由其他用户提交,则我无法继续购买 任何帮助都将不胜感激,Php Woocommerce单个产品页面中的自定义字段验证,php,wordpress,validation,woocommerce,product,Php,Wordpress,Validation,Woocommerce,Product,我想创建一个Woocommerce产品,带有一个额外的文本输入字段,用于检查输入的值是否是该字段的唯一值,否则将输出一条消息 换句话说,如果我输入“dave”并且“dave”已由其他用户提交,则我无法继续购买 任何帮助都将不胜感激, 我不知道从何处开始。这可以用非常简单的方法通过3个小钩子函数来完成: 第一,在单个产品页面中的“添加到购物车”按钮之前添加自定义输入文本字段 第二个进行验证,检查是否为唯一值 第三种方法是在验证后将值保存为现有值数组中的产品元数据 提交的值将针对当前产品进行验证
我不知道从何处开始。这可以用非常简单的方法通过3个小钩子函数来完成:
- 第一,在单个产品页面中的“添加到购物车”按钮之前添加自定义输入文本字段
- 第二个进行验证,检查是否为唯一值
- 第三种方法是在验证后将值保存为现有值数组中的产品元数据
// 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 );
}
}
//添加到购物车按钮之前的产品自定义字段-前端
添加操作(“添加到购物车按钮之前的操作”、“添加到购物车按钮之前的操作”);
功能操作在添加到购物车按钮()之前{
全球$产品;
回声';
woocommerce\u表单\u字段('custom\u unique',数组(
'类型'=>'文本',
'class'=>array('my-field-class表单行宽'),
“标签”=>“(“标签名称”),
“占位符”=>(请输入…),
“必需”=>true,
), '' );
//对于测试:显示现有提交的值(已注释-非活动)
//打印(get_post_meta($product->get_id(),'u custom_unique_values',true));
回声“
”;
}
//现场验证(检查)
添加过滤器('woocommerce'u add'u to'u cart'u validation'、'filter'u add'u to'u cart'u validation',20,3);
函数筛选器\u添加到\u购物车\u验证($passed,$product\u id,$quantity){
//获取要检查的自定义字段值
$custom_unic_values=(数组)get_post_meta($product_id,''u custom_unique_values',true);
//检查该值是否唯一
if(在数组中($\u POST['custom\u unique'],$custom\u unic\u value)){
$passed=false;//值存在时设置为false
//显示自定义消息
$message=sprintf($值“%s”已存在,请尝试其他…,'woocommerce'),清理文本字段($发布['custom\u unique']);
wc_添加_通知($message,'error');
}
返回$passed;
}
//将新的唯一值保存在值数组中(作为产品元数据)
添加行动('woocommerce'u add'u to'u cart','action'u add'u to'u cart',20,6);
功能操作添加到购物车($cart\u item\u key、$product\u id、$quantity、$variation\u id、$variation、$cart\u item\u data){
如果(isset($\u POST['custom\u unique'])){
//获取现有值的数组
$custom_unic_values=(数组)get_post_meta($product_id,''u custom_unique_values',true);
//将新值附加到值数组中
$custom_unic_values[]=清理文本字段($u POST['custom_unique']);
//保存附加的数组
更新发布元($product_id、$custom_unic_values’、$custom_unic_values);
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作
这可以通过3个小钩子函数以非常简单的方式完成:
- 第一,在单个产品页面中的“添加到购物车”按钮之前添加自定义输入文本字段
- 第二个进行验证,检查是否为唯一值
- 第三种方法是在验证后将值保存为现有值数组中的产品元数据
// 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 );
}
}
//添加到购物车按钮之前的产品自定义字段-前端
添加操作(“添加到购物车按钮之前的操作”、“添加到购物车按钮之前的操作”);
功能操作在添加到购物车按钮()之前{
全球$产品;
回声';
woocommerce\u表单\u字段('custom\u unique',数组(
'类型'=>'文本',
'class'=>array('my-field-class表单行宽'),
“标签”=>“(“标签名称”),
“占位符”=>(请输入…),
“必需”=>true,
), '' );
//对于测试:显示现有提交的值(已注释-非活动)
//打印(get_post_meta($product->get_id(),'u custom_unique_values',true));
回声“
”;
}
//现场验证(检查)
添加过滤器('woocommerce'u add'u to'u cart'u validation'、'filter'u add'u to'u cart'u validation',20,3);
函数筛选器\u添加到\u购物车\u验证($passed,$product\u id,$quantity){
//获取要检查的自定义字段值
$custom_unic_values=(数组)get_post_meta($product_id,''u custom_unique_values',true);
//检查该值是否唯一
if(在数组中($\u POST['custom\u unique'],$custom\u unic\u value)){
$passed=false;//值存在时设置为false
//显示自定义消息
$message=sprintf($值“%s”已存在,请尝试其他…,'woocommerce'),清理文本字段($发布['custom\u unique']);
wc_添加_通知($message,'error');
}
返回$passed;
}
//将新的唯一值保存在值数组中(作为产品元数据)
添加行动('woocommerce'u add'u to'u cart','action'u add'u to'u cart',20,6);
功能操作添加到购物车($cart\u item\u key、$product\u id、$quantity、$variation\u id、$variation、$cart\u item\u data){
如果(isset($\u POST['custom\u unique'])){
//获取现有值的数组
$custom_unic_values=(数组)get_post_meta($product_id,''u custom_unique_values',true);
//将新值附加到值数组中
$custom_unic_values[]=清理文本字段($u POST['custom_unique']);
//保存附加的数组
更新发布元($product_id、$custom_unic_values’、$custom_unic_values);
}
}
代码进入活动子主题的function.php文件(或