How to add attribute values?

Hello.
I have such a task - to publish / edit and activate / deactivate listing through PHP code, which in turn is periodically opened through cron.
Under certain conditions, a new listing is published or the parameters of an existing one are edited or an existing one is deactivated.
To get started, let’s start by posting a new listing.
I have created all the necessary categories and attributes in advance.
After reading your documentation I use the following code

$listing = ( new HivePress\Models\Listing() )->fill(
	[ 'title'       => 'Custom title',
      'description' => 'Custom text',
	  'slug' => 'new-post-slug',
	  'status' => 'publish',
	  'user' => 1,
	  'categories' => [29,42],
	  ]);
if ( ! $listing->save() ) {
	$errors = $listing->_get_errors();
}else{$id = $listing->get_id();
//------
}

In this situation, I can’t figure out how to add attribute values to this listing? Moreover, the attributes are pre-created and text fields and radio buttons and drop-down selects.
So how do you add attribute values?

Please share more details about the issue, do you mean adding attributes to an array passed to the fill method? You can do this in the same way, e.g. if you added a Price attribute and its name is price:

'price' => 123,

I am satisfied with any option, and if you add an attribute to the array passed to the method or add it separately to an already created declaration.
What you say doesn’t work!

$listing = ( new HivePress\Models\Listing() )->fill(
	[ 'title'       => 'Custom title',
      'description' => 'Custom text',
	  'slug' => 'new-post-slug',
	  'status' => 'publish',
	  'user' => 1,
	  'categories' => [21,43],
	  'vendor' => 257,
	  'free_shipping' => 'Yes',
	  'min_interest_rate' => 120,
	  'max_credit_limit' => 87,
	  'price' => 12345,
	  'fillprice' => 12345,
	  ]);
if ( ! $listing->save() ) {
	$errors = $listing->_get_errors();
}else{echo $listing->get_id();}	

Everything after ‘vendor’ does not work, that is, values are not added to attributes.
When viewed from the admin panel - there are empty fields.

Please try to debug this code by checking the $listing->_get_errors() contents, e.g. using var_dump or error_log. The listing will not be saved if at least one required field is not set or at least one value is not valid, otherwise if you set the attribute names correctly (please check these names in Listing/Attributes, edit an attribute and check the Field Name) this is the correct way to save the listing fields. You can also save specific fields only by passing an array to save:

$listing->save(['price', 'fillprice']);

I spent two full working days, but could not solve this issue.
Any of the below different options do not work.
All fields after ‘vendor’ (‘free_shipping’,‘min_interest_rate’…) that are not added to the $this->fields object

$listing = ( new HivePress\Models\Listing() )->fill(
	[ 'title'       => 'Custom title',
      'description' => 'Custom text',
	  'slug' => 'new-post-slug',
	  'status' => 'publish',
	  'user' => 1,
	  'categories' => [21,43],
	  'vendor' => 257,
	  'free_shipping' => 'Да',
	  'min_interest_rate' => 120,
	  'max_credit_limit' => 87,
	  'price' => 12345,
	  'fillprice' => 12345,
	  ]);
if ( ! $listing->save() ) {
	$errors = $listing->_get_errors();
	print_r($errors);
}else{echo $listing->get_id().'<br/><br/>';
$errors = $listing->_get_errors();
	print_r($errors);
}

or

$listing = ( new HivePress\Models\Listing() )->fill(
	[ 'title'       => 'Custom title',
      'description' => 'Custom text',
	  'slug' => 'new-post-slug',
	  'status' => 'publish',
	  'user' => 1,
	  'categories' => [21,43],
	  'vendor' => 257,
	  'free_shipping' => 'Да',
	  'min_interest_rate' => 120,
	  'max_credit_limit' => 87,
	  'price' => 12345,
	  'fillprice' => 12345,
	  ]);
if ( ! $listing->save('free_shipping' => 'Да',
	  'min_interest_rate' => 120,
	  'max_credit_limit' => 87,
	  'price' => 12345,
	  'fillprice' => 12345) ) {
	$errors = $listing->_get_errors();
	print_r($errors);
}else{echo $listing->get_id().'<br/><br/>';
$errors = $listing->_get_errors();
	print_r($errors);
}	 

or

$listing = ( new HivePress\Models\Listing() )->fill(
	[ 'title'       => 'Custom title',
      'description' => 'Custom text',
	  'slug' => 'new-post-slug',
	  'status' => 'publish',
	  'user' => 1,
	  'categories' => [21,43],
	  'vendor' => 257,
	  'free_shipping' => 'Да',
	  'min_interest_rate' => 120,
	  'max_credit_limit' => 87,
	  'price' => 12345,
	  'fillprice' => 12345,
	  ]);
if ( ! $listing->save('free_shipping',
	  'min_interest_rate',
	  'max_credit_limit',
	  'price',
	  'fillprice') ) {
	$errors = $listing->_get_errors();
	print_r($errors);
}else{echo $listing->get_id().'<br/><br/>';
$errors = $listing->_get_errors();
	print_r($errors);
}	

does not show an error in any case, even if I enter a non-existent (previously not created) field-> attribute value

So all the same, how do I add attributes (previously created)?
Is there any other way?

Please make sure that WP_DEBUG is turned on and that this code is called, because it should show PHP errors or at least notices (the array brackets are missing in save). Please try using this code:

add_action(
	'template_redirect',
	function() {
		$listing = ( new HivePress\Models\Listing() )->fill(
			[
				'title'       => 'Custom title',
				'description' => 'Custom text',
				'status'      => 'publish',
				'user'        => 1,
				'price'       => 123,
			]
		);

		if ( ! $listing->save() ) {
			var_dump( $listing->_get_errors() );
			die();
		}
	}
);

I tested it locally and it shows errors if you view the home page, this way you can add details to the fill array one by one and resolve the validation errors Screenshot by Lightshot

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