Programmatically set vendor attributes

Hello,

Is there a way to programmatically set these vendor attributes:

  • Category (Add/Remove)
  • Verified (True/False)
  • Commission (specific to the individual vendor)

I’ve attached screenshots highlighting what areas I’m referring to. I have a plugin that authorizes the user for something, and once it’s successful, I’d like to change these attributes for that specific user (I imagine with their username to select them).

Can you provide PHP code to edit these attributes? I’ll insert the code into the plugin myself, but I just need to know how to change these for the user. If you could do this, this would be extremely helpful!

Thank you,
Andrew


Hi,
If you have the vendor ID (it differs from the user ID, vendor is a custom post type “hp_vendor”) please try using this sample snippet:

$vendor = HivePress\Models\Vendor::query()->get_by_id( 123 );

if ( $vendor ) {
	$vendor->fill(
		[
			'categories'      => [ 1 ],
			'commission_rate' => 50,
			'verified'        => true,
		]
	)->save( [ 'categories', 'commission_rate', 'verified' ] );
}

Replace 123 with vendor ID, 1 with category ID.

Hello,

Thank you! Is the commission rate in a straight percentage (meaning 50 is 50% fee) or a multi-decimal percentage (meaning 1050 is 10.50% fee)?

And do you happen to have a way to get the vendor ID from the username? That way I can replace 123 with a function call that takes the username as an input and outputs their vendor ID.

Also, how would I go about doing the reverse and getting the info from the vendor? For example, using the vendor variable created above and doing something to the effect of $vendor.commission() to get the commission?

This way, I can do comparisons, such as is $vendor.commission() != null ….

Thank you,
Andrew

If you have a user ID (e.g. via the get_current_user_id()) it would be easier, then this code snippet should get the vendor object:

$vendor   = Models\Vendor::query()->filter(
	[
		'user'   => $user_id,
	]
)->get_first();

If you have the vendor object, you can get the commission rate this way:


$vendor->get_commission_rate();

You’re awesome, thank you. I’ll try this soon and reply again if there’s an issue. Thank you for the great support as always.

Best regards,
Andrew

Hello,

It seems that all of the code snippets you sent cause a fatal wordpress error. Attempting to create the $vendor variable is what causes the error, with either of the snippets you sent above. Do you happen to have another way?

To test the code, I’m using this plugin Insert PHP Code Snippet – WordPress plugin | WordPress.org to output the results on the front-end.

Thank you,
Andrew

Sorry, forgot the HivePress namespace:

$vendor   = HivePress\Models\Vendor::query()->filter(
	[
		'user'   => $user_id,
	]
)->get_first();

Hello,

That works now to initialize the $vendor, and I’m able to get the commission rate displayed.
I used this snippet to set vendor variables:

if ( $vendor ) {
	$vendor->fill(
		[
			'categories'      => [ 1 ],
			'commission_rate' => 50,
			'verified'        => true,
		]
	)->save( [ 'categories', 'commission_rate', 'verified' ] );
}

This snippet runs without errors, however, it doesn’t seem to actually save the information for the vendor. On the backend, the categories, commission rate, and verified status do not change. The “PRO” badge for verified vendors also does not display, further backing this up.

I also wanted to know if you have any documentation listing out the possible options when pulling vendor data, such as:

$vendor->get_commission_rate();

I figured out get_id() to get the vendor ID, but I’m wondering what other functions are available. I couldn’t find the functions on github, but if you can direct me I’d be happy to look for myself.

Thank you,
Andrew

Please try to debug this code to make sure that it runs (e.g. with echo 123;, check the vendor ID and make sure that it exists, etc.). The default fields are name, description, categories and any custom attributes you added. These methods are available:

$vendor->get_attributenamehere();
$vendor->set_attributenamehere($value);

Saving is required after setting new values via the save method.

Hello Ihor,

I’ve been trying this for a few days now, but cannot seem to succeed.

I use this to get the vendor, which works fine:

$vendor   = HivePress\Models\Vendor::query()->filter(
	[
		'user'   => $user_id,
	]
)->get_first();

I’ve used the following snippets to try setting values (and having them save). The saving doesn’t seem to work for either of them, and the categories also don’t get set with either one.

Method 1:

if ( $vendor ) {
	$vendor->fill(
		[
			'categories'      => [ 1 ],
			'commission_rate' => 50,
			'verified'        => true,
		]
	)->save( [ 'categories', 'commission_rate', 'verified' ] );
}

Method 2:

if ( $vendor ) {
	$vendor->set_categories([1,2]);
	$vendor->set_commission_rate(12);
	$vendor->set_verified(true);
	$vendor->save();
}

I also cannot get the categories to be listed out.
Running this code sets categories equal to ARRAY if categories are set for the vendor and NULL if they are not:

$categories = $vendor->get_categories();

Even when categories are set, I’m unable to list them (or loop through the array and do so).

tl;dr: The settings aren’t saving, the categories aren’t setting at all, and I cannot list the categories.

Any idea why these things may be happening? I appreciate all of your help.

Thank you,
Andrew

The code snippets seem to be ok, please set the existing category ID instead of “1, 2”, otherwise they will not be saved. If any of the changed fields are not valid, then changes will not be saved, so if there are non-existing category IDs the vendor will not be saved. You can try saving a single field first (e.g. the commission rate) to check this.

Hello,

That makes sense and I’ll try it later. Do you know how to find the category IDs? I imagined they would be in the order I added them, but that doesn’t seem to be the case, and I don’t see them anywhere under Vendors > Categories in the settings.

Thank you,
Andrew

Please open Vendors/Categories and please try to edit one of the categories. You’ll see the category ID in the browser’s address bar where the category ID is the number between category&tag_ID= and &post_type