My listing has a unique identifier, like a Document ID, and I have that as an attribute. How can I prevent duplicates, ensuring that the same Document ID cannot be entered twice? I want the system to reject the entry if the Document ID already exists. How can I achieve this? Thank you.
Hi,
Please clarify the type of attribute (text, etc.) and the attribute field name, and we will try to provide a PHP snippet.
Thank you ,
The Field Type is Text
The field Name is document_number
Please try this code snippet:
add_filter(
'hivepress/v1/forms/listing_update/errors',
function( $errors, $form ) {
$document_number = $form->get_value( 'document_number' );
if ( $document_number ) {
$listing_id = \HivePress\Models\Listing::query()->filter(
[
'status__in' => [ 'draft', 'pending', 'publish' ],
]
)->set_args(
[
'meta_key' => 'hp_document_number',
'meta_value' => $document_number,
]
)->get_first_id();
if ( $listing_id ) {
$errors['document_number'] = 'A listing with this document ID already exists. Please choose a different one.';
}
}
return $errors;
},
1000,
2
);
Hi ihor, thank you for the snippet. I have tried it, but it still accepts duplicates and says nothing.
Sorry for the delay.
We tested this again and the error appears when setting the same values for different listings. Please make sure that the attribute field name is “document_number”, then if you try to save 2 listings with the same value there should be an error (please note that this works for the front-end listing edit form only).
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.