Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Woocommerce产品自定义字段:检查输入是否已存在_Php_Wordpress_Validation_Woocommerce_Custom Fields - Fatal编程技术网

Php Woocommerce产品自定义字段:检查输入是否已存在

Php Woocommerce产品自定义字段:检查输入是否已存在,php,wordpress,validation,woocommerce,custom-fields,Php,Wordpress,Validation,Woocommerce,Custom Fields,我安装了这个“WC Fields Factory”插件来向产品中添加新字段,但是如果数据库中已经存在它的值,我需要检查这个新文本框。 举个例子,比如在注册中,你不能使用已经在使用的电子邮件 我必须像在屏幕上一样添加文本字段 更新2-处理字段验证,只允许唯一的非存在文本输入 以下是在单个产品页面中添加自定义文本字段、将其添加为购物车项目数据、在购物车和结帐页面中显示、将其另存为订单项目数据以及在订单和电子邮件通知中显示的完整方法: // HERE define your field label

我安装了这个“WC Fields Factory”插件来向产品中添加新字段,但是如果数据库中已经存在它的值,我需要检查这个新文本框。 举个例子,比如在注册中,你不能使用已经在使用的电子邮件

我必须像在屏幕上一样添加文本字段

更新2-处理字段验证,只允许唯一的非存在文本输入

以下是在单个产品页面中添加自定义文本字段、将其添加为购物车项目数据、在购物车和结帐页面中显示、将其另存为订单项目数据以及在订单和电子邮件通知中显示的完整方法:

// HERE define your field label name
function get_field_label_name(){
    return __( "The label name" );
}


// Add a custom product note below product meta in single product pages
add_action('woocommerce_before_add_to_cart_button', 'my_product_custom_field', 100 );
function my_product_custom_field() {

    echo '<div class="my-custom-field">';

    woocommerce_form_field('custom_field1', array(
        'type'        => 'text',
        'class'       => array( 'my-field-class form-row-wide') ,
        'label'       => get_field_label_name(),
        'placeholder' => __("The field placeholder…" ),
        'required'    => true, // Or false
    ) , '');

    echo '</div>';
}

// Check if the custom field value is unique
add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3 );
function wc_add_on_feature( $passed, $product_id, $quantity ) {
    if( isset($_POST['custom_field1']) && ! empty($_POST['custom_field1']) ){
        global $wpdb;

        $label = get_field_label_name();
        $value = sanitize_text_field( $_POST['custom_field1'] );

        // Check if value exits already
        $result = $wpdb->get_var( "
            SELECT COUNT(meta_value) FROM {$wpdb->prefix}woocommerce_order_itemmeta
            WHERE meta_key LIKE '$label' AND meta_value LIKE '$value'
        " );

        // If it exist we don't allow add to cart
        if( $result > 0 ){
            // Display an error notice
            wc_add_notice( sprintf( __( 'This "%s" input already exist. Please choose another one…' ), $value ), 'error' );
            $passed = false;
        }
    }
    return $passed;
}

// Add custom field value to cart item data
add_filter( 'woocommerce_add_cart_item_data', 'custom_field_value_to_cart_item_data', 20, 2 );
function custom_field_value_to_cart_item_data( $cart_item_data, $product_id ){
    if( isset($_POST['custom_field1']) && ! empty($_POST['custom_field1']) ){
        $cart_item_data['custom_data'] = sanitize_textarea_field( $_POST['custom_field1'] );
    }
    return $cart_item_data;
}


// Display custom cart item data in cart
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {

    if ( isset( $cart_item['custom_data'] ) ){
        $cart_item_data[] = array(
            'name' => get_field_label_name(),
            'value' =>  $cart_item['custom_data']
        );
    }
    return $cart_item_data;
}

// Save and display custom field in orders and email notifications (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 );
function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset( $values['custom_data'] ) ){
        $item->update_meta_data( get_field_label_name(),  $values['custom_data'] );
    }
}
//此处定义字段标签名称
函数get\u field\u label\u name(){
返回(“标签名称”);
}
//在单个产品页面中的产品元下面添加自定义产品注释
添加操作(“添加到购物车按钮之前的woocommerce”和“我的产品”和“自定义”字段”,100);
函数my_product_custom_field(){
回声';
woocommerce\u表单\u字段('自定义\u字段1',数组(
'类型'=>'文本',
'class'=>数组('my field class form row-wide'),
'label'=>get_field_label_name(),
“占位符”=>(字段占位符…),
“必需”=>true、//或false
) , '');
回声';
}
//检查自定义字段值是否唯一
添加过滤器(“woocommerce\u添加到购物车\u验证”、“wc\u添加到\u功能”,20,3);
函数wc\u添加功能($passed,$product\u id,$quantity){
如果(isset($\u POST['custom\u field1'])和&!empty($\u POST['custom\u field1'])){
全球$wpdb;
$label=get_field_label_name();
$value=sanitize_text_字段($_POST['custom_field1']);
//检查值是否已经存在
$result=$wpdb->get_var(“
从{$wpdb->prefix}woocommerce\u order\u itemmeta中选择COUNT(meta\u value)
其中meta_键如“$label”和meta_值如“$value”
" );
//如果存在,我们不允许添加到购物车
如果($result>0){
//显示错误通知
wc_添加_通知(sprintf(_uuu('此“%s”输入已存在。请选择另一个…'),$value),'error');
$passed=false;
}
}
返回$passed;
}
//将自定义字段值添加到购物车项目数据
添加过滤器('woocommerce\u add\u cart\u item\u data','custom\u field\u value\u to\u cart\u item\u data',20,2);
函数自定义\字段\值\到\购物车\项目\数据($cart\项目\数据,$product\ id){
如果(isset($\u POST['custom\u field1'])和&!empty($\u POST['custom\u field1'])){
$cart\u item\u data['custom\u data']=sanitize\u textarea\u字段($\u POST['custom\u field1']);
}
返回$cart\u item\u数据;
}
//在购物车中显示自定义购物车项目数据
添加_过滤器('woocommerce_get_item_data','display_custom_item_data',10,2);
函数显示自定义项目数据($cart\u item\u data,$cart\u item){
if(isset($cart\U item['custom\u data'])){
$cart\u item\u data[]=数组(
'name'=>get_field_label_name(),
'value'=>$cart\u item['custom\u data']
);
}
返回$cart\u item\u数据;
}
//在订单和电子邮件通知中保存和显示自定义字段(无处不在)
添加操作('woocommerce\u checkout\u create\u order\u line\u item'、'custom\u field\u update\u order\u item\u meta',20,4);
函数自定义\字段\更新\订单\项目\元($item,$cart\项目\键,$values,$order){
如果(isset($values['custom_data'])){
$item->update_meta_数据(获取_field_label_name(),$values['custom_data']);
}
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作


您应该避免使用此插件,因为StackOverFlow中存在许多相关问题和问题……请记住,当您使用插件时,您会受到该插件的限制。如果没有插件,我如何在产品中添加新字段?如果已使用检查,您希望在前端或后端添加这些字段吗?哪种领域…也许你可以更新你的问题,提供尽可能多的细节…我更新了线程