php - Woocommerce product custom field: Check if the input already exist

Solution:
Update 2 - Handle the field validation, allowing only a unique non existant text input
Here is the complete way to add a custom text field in single product pages, add it as cart item data, display it in cart and checkout pages, save it as order item data and display it on orders and email notifications:
// 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'] );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.