Set default values for attributes

Hello,

when there is a “non editable” attribute on front-end, would be interesting to have the option to define the attribute or one of it`s options as default.

Let`s say I have an attribute non editable on front end, that has 2 options and I would like one of it to be default. It is not possible.

Right now when the vendor creates the new account we must manually select the option for each vendor.

Since it is an attribute not editable for the vendor, it would make sense for the admin to be able to have default attributes.

In my real case scenario, it is something like all vendors start with “Pending Verification” on profile, but could be any lable, like “Silver Member” before change manually to “Gold Member” or anything after the vendor do some action, like verifies profile, pay something, etc.

Of course would be amazing in future if it was possibile to change automatically some attribute option based on actions, like if vendor creates a listing on some category another attribute is given automatically, but right now I would be happy with the default attribute from the admin option.

Thanks

Thanks, we’ll consider adding this feature. There’s a temporary PHP snippet that you can use to set the default value for a selectable vendor attribute:

add_action(
	'hivepress/v1/models/vendor/create',
	function ($vendor_id, $vendor){
		$vendor->set_vendor_default_rate(123)->save_vendor_default_rate();
	},
	1000,
	2
);

Please replace 123 with the option ID (you can check it in the browser address bar while you edit and option).

2 Likes

Thanks Ihor,

now it worked! Also needs to substitute attribute name

add_action(
	'hivepress/v1/models/vendor/create',
	function ($vendor_id, $vendor){
		$vendor->set_vendor_default_rate(123)->save_vendor_default_rate();
	},
	1000,
	2
);

Replace 123 with the option ID (you can check it in the browser address bar while you edit and option).

Replace vendor_default_rate with specific attribute name

2 Likes

I’ve created “non editable” listing attribute on front-end for age, how to set default number to 18

Hi Rudy,

I am trying to use this code snippet and it is not working for me. Do I have to make the attribute editable? Required? Where will I see the default attribute? Will this make the vendor be able to see it in the front end of registration? Or only admin will see it in back end?

Also, what do you mean by replace vendor_default_rate with specific attribute name? Do you mean I have to change this part of the code snippet and add the specific attribute name in its place in order for the code snippet to work? Your help will be greatly appreciated!

Hello @Adams & @aqmiami7,

For default listing attribute, is this one:

add_action(
	'hivepress/v1/models/listing/update_categories',
	function ($listing_id, $value){
		$listing = HivePress\Models\Listing::query()->get_by_id($listing_id);
		
		if(!$listing){
			return;
		}
		
		$listing->set_compras_protegidas(269)->save_compras_protegidas();
	},
	1000,
	2
);

What you must change:

set_YOUR_FIELD_NAME

save_YOUR_FIELD_NAME

the other thing to change is the 269, that is the attribute option to be default

Create attribute, type select
chose non editable

after you save, you can now edit options for the select (add options to select)

include one or more option

after you include the options, if you click edit on the option you want to be default, you will see on your browser the full address of the page that contains the ID=xxxxx

so that is the 269

@aqmiami7 for vendor is the same process, only use the other code.

Only admin will see and only admin can change it. Vendor will have it by default

I use it for “Pending Verification profile”, all vendors start with pending verification.

I have non editable select attribute with 2 options:

  • Pending
  • verified

all start with pending by default and it shows on their profile “Pending Verification”

I then sell to Verify. After they pay, I must manually change the attribute option to verified and I also mark the vendor as verified.

This is the way I use to monetize this and also to apply default options that can be used on filters.

2 Likes

Hi I followed your instructions and I don’t see any default attribute.

I have one vendor category, then I added a Status attribute. I left it not editable, I did not check the allow front end editing. I named the field host_status and type select.

I created two options. One that says new host and the other that says power host. I want the default to be new host, so I looked at browser address and see the 1742 ID.

I then entered the code like this:

add_action(
	'hivepress/v1/models/vendor/create',
	function ($vendor_id, $vendor){
		$vendor->set_host_status(1742)->save_vendor_default_rate();
	},
	1000,
	2
);

I check with a newly created registered vendor and when i check on the vendor admin back end, I see the select drop down menu with the two options but no default.

Not sure what I am doing wrong. Just to clarify how do the _ work in the name part. My field name is actually host_status with the underscore.

So did I place it in the code area correctly above? Just to make sure it is the attribute field name and not the slug name of the option correct?

I am not sure what I am doing wrong, and it is driving me crazy! lol

Thanks for helping me. If you see something I am doing wrong please let me know.

Hi did you mark as mandatory? I forgot to mention that it cant be optional the attribute

So non editable and mandatory

1 Like

I just tried it with required, and no default showing. Only can select the two options, before publishing the vendor.

Did I enter the field name correct in the code snippet above? set_host_status ?

My attribute field name is host_status

So for instance with your field name is it actually comprasprotegidas or compras protegidas or actually compras_protegidas when you named it? and entered it in code as:

set_compras_protegidas

my field name is compras_protegidas

I noticed that you also didnt use the same in this line

$vendor->set_host_status(1742)->save_vendor_default_rate();

I believe in your case, if your field name is set_status , it should be there:

$vendor->set_set_status(1742)->save_set_status();

this would be correct:

add_action(
	'hivepress/v1/models/vendor/create',
	function ($vendor_id, $vendor){
		$vendor->set_set_status(1742)->save_set_status();
	},
	1000,
	2
);
1 Like

Sorry I gave you the my wrong field name. It is actually host_status

So I followed your instructions and changed the snippet to this:

add_action(
	'hivepress/v1/models/vendor/create',
	function ($vendor_id, $vendor){
		$vendor->set_host_status(1742)->save_host_status();
	},
	1000,
	2
);

And I tried again with new vendor and it still did not work. :sweat: I give up, maybe the hivepress can tell me what I am doing wrong?

Non default showing, you mean non editable? should be non editable

Hope you find the error, I know how frustrating it can be. In my case, after the vendor create sthe profile the default attribute will show where you see %value% its gonna be the host_status option 1742

1 Like

Does this code make the option set by default? In other words, it makes it automatic without the admin having to click on the drop down menu and select the option, which in my case would be “new host” instead of “power host”.

I know to make it non editable you just uncheck the
Allow front-end editing

What does it look like in your admin back end? Mine just shows the select drop down with none of the two options selected. I am guessing yours picks by default the option, so you do not have to click on it in the beginning, until later when they pay for verified status.

By the way Obrigado! Thank you! I speak portuguese as well, but Brazilian Rio accent.

Yes, thats it!

this is what it shows… the field i put there compras_protegidas (i changed it later)

2 Likes

Thank you so much Rudy!
It works great. Appreciate for your kindy help on this issue.

Now I’ve created new customized age attribute and activated search indexing option for this attribute.

Would you please help how to make this customized age attribute as default on Ascending in search results. Currently WordPress and hivepress defaults are on “Date” but I want set it by default on Ascending age_attribute

I’ve find this on WP and StackeXchange forumes:

You can use the action hook posts_orderby in your child themes functions.php:

function changeSearchSort( $orderby, $query ){
    global $wpdb;

    if(!is_admin() && is_search()) {
        $orderby =  $wpdb->prefix."posts.post_date ASC";
    }
    return  $orderby;
}
add_filter('posts_orderby','changeSearchSort',10,2);

Source via:

And more details on WP developers official website regards “posts_orderby”

What’s your idea?! How to set default search results to show this age_attribute Ascending first ?!

Hi @Adams thank you. I am sorry but I dont have any technical knowledge, basically I just have a big collection of code snippets that I use and tested.

I remember a topic from the past where the team says it is not possible to chose wich criteria to be default:

Not sure if it has changed already.

I hope the hivepress team can help you here.

2 Likes

@aqmiami7 Please make sure that the vendor attribute name is set correctly in the code (maybe it has a non-selectable type, or you’re checking listing attributes?), the code snippet above seems to be ok.

@rudy Thanks for sharing the solutions!

1 Like

@rudy

Thank you for your help. Now my attribute at default is working with code. Not sure what changed, the next day it just started working. :smiley:

How to add [snippet] for values in attributes as default instead of numbers? @ihor @rudy

If you mean setting some dynamically calculated value instead of a static number via the snippet for default attribute values, this requires customizations, depending on which calculations are required for an attribute.