Woocommerce产品定制字段:检查输入是否已经存在

时间:2018-08-17 10:47:42

标签: php wordpress validation woocommerce custom-fields

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

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

enter image description here

1 个答案:

答案 0 :(得分:1)

  

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

这是在单个产品页面中添加自定义文本字段,将其添加为购物车商品数据,在购物车和结帐页面中显示,将其另存为订单商品数据并在订单和电子邮件通知上显示的完整方法:< / p>

// 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'] );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

enter image description here

enter image description here

enter image description here