Calculated Attributes

Hi,

I am looking for a way to make an attribute that is calculated by other attributes. For example, the user enters a value for a number attribute when making a listing, and another attribute is created based on dividing the price by that attribute.

Thanks

Hi,
Please try this sample code snippet:

add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {
		$value = $listing->get_foo() / 123;

		if ( $listing->get_bar() !== $value ) {
			$listing->set_bar( $value )->save_bar();
		}
	},
	1000,
	2
);

Replace foo and bar with attribute names, and the calculation formula.

Hi,

Thanks for the reply. If my attribute names were to have spaces in them, how would I do this? Would there be some sort of attribute ID that I could use in place?

Thanks

Yes, the attribute name is a slug with underscores, e.g. if the attribute is named “Some Attribute” then its name is “some_attribute”.

Hi Ihor,

My attribute name is SHA-256 Hashrate (TH). I don’t think that it is compatible with the PHP syntax. Is there a way to change the attribute name in the backend, or how would I get it’s value?

Thanks

Please check the attribute slug field. You should use it as an attribute name in the code snippet which is mentioned above. Also, you can change it when you edit the attribute

Hi,

Where can see/edit the url slug value for attributes? I do not see an option to do so in either the edit page or the quick edit menu.

Thanks

Please click Screen Options at the top of the page while editing an attribute, if you check the Slug there then the Slug field should appear.

Hi Yevhen,

Thanks for the reply. I was able to change the slugs for the categories to use underscores.

I am encountering a problem however. Now that I have activated the code in my functions.php file, I get an error when updating listings and the calculated attribute is not set.

This is the error:

'There has been a critical error on this website. Please check your site admin email inbox for instructions.'

My code is here:

// Calculated Efficiency Attribute (SHA-256)
add_action(
	'hivepress/v1/models/listing/update',
	function( $listing_id, $listing ) {
		
		// Get Hashrate and power
		$hashrate = $listing->get_sha_256_hashrate();
		$power_consumption = $listing->get_power_consumption();
		
		// Calculate efficiency in J/TH
		$efficiency = $power_consumption / $hashrate;
		
		// Check and set
		if ( $listing->get_sha_256_efficiency() !== $efficiency ) {
			$listing->set_sha_256_efficiency( $efficiency )->save_sha_256_efficiency();
		}
	},
	1000,
	2
);

Something else that happens is that the sha_256_hashrate attribute gets its value deleted

Not sure what the bug is here.

Thanks

The code snippet seems to be correct, if the attribute names are correct it should be ok. If this issue persists please copy this PHP error from the PHP error log (usually there is a detailed error with the line number).

Hi,

As soon as I took a look at my php error log, I realized what the issue was. I had to add an if statement to make sure power consumption was not equal to 0 in order to prevent division by 0 errors.

My issue now is that the entire site is incredibly slow with the code enabled. Is there a way to change the frequency it executes or is there anything else I can do to limit the performance impact?

Thanks

It should not affect the overall performance, this action fires only when a listing is updated - please check this by adding echo 123; die(); to the function and try to save any listing via the back-end. The 123 should appear only when the listing is saved, not on every page load.
You can also follow this tutorial to improve the website performance for non-registered users How to Speed up a Directory or Marketplace Website Built with HivePress | HivePress Blog

Hi,

The problem was that I was dividing the numbers using the " / " operator. It would return a value with a lot of decimals and would cause a timeout. I solved it by using intdiv() instead.

Thanks for the help.

You just did automated field hidden from Front-End editing by users right? I mean users cant see input while Adding Listing?

Yes thats correct

1 Like

Thank you!

Hi Hivepress team,

One issue I am facing now is when a user tries to add a new listing from the homepage, it will not load. This is because all the values that the functions take as inputs are invalid.

Is there a way to disable the function when the ad is initially created?

Thanks

Yes, please try checking if the attribute values exist before the calculation, you can check this with is_null function. Then it should be ok for new listings without any attribute values yet.

Do I do the is_null check on the listing or on the $listing->get_foo() part?

Thanks

Yes, please add checking on the $listing->get_foo() part with is_null function