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

问题描述 投票:2回答:1

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

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

enter image description here

php wordpress validation woocommerce custom-fields
1个回答
1
投票

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

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.