I want to calculate the yield for real estate ads listings , I can get the values from the listing to make the calculation, but the listing is not updated when I call the save function.
Any idea why ?
// Calcul du rendement brut loyer/prix de vente
add_action( 'hivepress/v1/models/listing/update', 'update_rendement',10,2);
function update_rendement ( $listing_id, $listing ) {
// To avoid a callback
remove_action('hivepress/v1/models/listing/update', 'update_rendement', 10, 2);
// Get loyer and selling price
$loyer = $listing->get_loyer();
$prix = $listing->get_prix();
// Calculate rendement
if ($prix!=0) $rendement = 100*$loyer / $prix; else $rendement=0;
$message = "Calcul effectué ".$rendement." pour $loyer $prix";
error_log($message,0);
// Check and set
if ( $listing->get_rendement() !== $rendement ) {
error_log("Boucle de mise a jour 1",0);
$listing->set_rendement( $rendement )->save_rendement();
}
}