Require selection of one attribute between two during listing creation

I’m trying to require sellers to select from one attribute dropdown when creating a listing, but not allow selection of both:

add_filter(
    'hivepress/v1/forms/listing_update/errors',
    function( $errors, $form ) {
        $listing = $form->get_model();

        // Attempt to retrieve the values using direct property access
        $service_1 = $listing->service_1;
        $service_2 = $listing->service_2;

        // Check if both fields are filled
        if ($service_1 && $service_2) {
            $errors[] = 'Please only fill out either "Service 1" or "Service 2", but not both.';
        }

        // Check if both fields are empty
        if (!$service_1 && !$service_2) {
            $errors[] = 'Please fill out one of the fields "Service 1" or "Service 2".';
        }

        return $errors;
    },
    100,
    2
);

but I can’t seem to get it to work. I’m trying to check if both are selected, then send an error message and then check if neither are selected, then send an error message.

I’m at a loss.

If you know the field name for sure, please try fetching values this way:

$service_1 = $listing->get_service_1();

The rest of the code snippet seems to be ok.

Still having an issue. These fields are select dropdowns. Would that change the necessary value retrieval method? I’m trying to check if any of the options has been selected within these dropdowns or not

If these are Select fields and the field names are correct, you can check if the field is set this way:

if($listing->get_service_1__id()) {
// it's set
}

Please notice the double underscore. If it still doesn’t work, you can debug this by using var_dump and check if the $listing object is not empty, if the function fires at all, and if so you can check the $listing->get_service_1__id() value.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.