How to Inform Vendors That the Same Attribute Value Exists

Question/issue (workaround is posted below):
How one prevents submission of a “Add listing” form in case certain field (in my case custom fields of a type Text, with a Field Name “messengera_address”, “messengerb_address”) contains input which which already exist in some listing (meaning a listing with same “messengera_address” for example) ?

Solution/feature request:
When editing attribute of a listing WP-Admin > Listings > Attributes > attribute name, it can be handy to have a checkbox labeled e.g. “Check and avoid duplicate submissions” (? → It will be impossible to submit more than one listing with same value of this field)

Solution/workaround:
Use WP plugin “Code snippets” (or similar) and insert in it a PHP code:

add_filter(
    'hivepress/v1/forms/listing_update/errors',
    function( $errors, $form ) {
        $current_listing_id = $form->get_model()->get_id();
        
        // Check messengera_address
        $messengera_address = $form->get_value( 'messengera_address' );
        if ( $messengera_address ) {
            $listing_id = \HivePress\Models\Listing::query()->filter(
                [
                    'status__in' => [ 'draft', 'pending', 'publish' ],
                ]
            )->set_args(
                [
                    'meta_key'   => 'hp_messengera_address',
                    'meta_value' => $messengera_address,
                ]
            )->get_first_id();
            if ( $listing_id && $listing_id !== $current_listing_id ) {
                $errors['messengera_address'] = 'This messengera_address address is already listed.';
            }
        }

        // Check messengerb_address
        $messengerb_address = $form->get_value( 'messengerb_address' );
        if ( $messengerb_address ) {
            $listing_id = \HivePress\Models\Listing::query()->filter(
                [
                    'status__in' => [ 'draft', 'pending', 'publish' ],
                ]
            )->set_args(
                [
                    'meta_key'   => 'hp_messengerb_address',
                    'meta_value' => $messengerb_address,
                ]
            )->get_first_id();
            if ( $listing_id && $listing_id !== $current_listing_id ) {
                $errors['messengerb_address'] = 'This messengerb_address address is already listed.';
            }
        }

        return $errors;
    },
    10,
    2
);

replace messengera_address and messengerb_address with your own “Field Name“ found at WP-Admin > Listings > Attributes > attribute name.

Hi,

Thanks for the solution. It can be helpful for others.

I have messaged moderators group using this forum, but not yet received any reply and i can not find that message now in my member panel here. It was about request to edit my post.

Here is yet another request to edit my post, this time it is in this thread. Please replace first post code by the code pasted at JustPaste.it - Share Text & Images the Easy Way

(old code prevented updating the listing)

Also please kindly allow editing own posts without limit, it is very annoying being unable to update/improve own content.

Hey,

Apologies if I’ve got this wrong, but from my understanding, the updated topic title isn’t correct. (@kseniia)

I believe the snippet provided by @obtrusive170 actually aims to prevent a specific attribute from being filled in with the same information that’s already found in another listing.

E.g. A vendor creates listing 1 and has an attribute with a unique business ID.

A second vendor tries to create listing 2 and attempts to use the same business ID as listing 1, however, because of the snippet shared above, the vendor is informed that they cannot use the business ID entered, as it’s already assigned to another listing.

I hope that makes sense!

Cheers,
Chris :victory_hand:

1 Like

Thanks for sharing, I replaced the code. We don’t check private messages often, but we check flagged posts on a daily basis.

We plan to add the “Require unique values” checkbox to the attribute settings in future updates, but the current workaround is using the exact same filter hook you shared hivepress/v1/forms/listing_update/errors to check for unique values when a listing is saved.

Hi @ChrisB,

Yeah, totally agree.

1 Like