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 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?
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?
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
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
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).
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?
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
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.
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?
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.