Skip Navigation

[Resuelto] Problems trying to create a calculate function

This support ticket is created hace 5 años, 11 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Este tema contiene 1 respuesta, tiene 2 mensajes.

Última actualización por Edward Barker hace 5 años, 11 meses.

Asistido por: Waqar.

Autor
Mensajes
#1168903

Tell us what you are trying to do?

I'm trying to create a function that populates a custom field based on a percentage value presented in one field (wpcf-commission-percent) and the value to find the percentage of (wpcf-property-price) creating a populated field (wpcf-percentage-commission-value). This is so I can show how much someone could earn if a house is sold.

The code I'm using currently is as follows ...........

<?php
/**
* New custom code snippet.
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

function calculate_commission( $post_ID ) {

if ( get_post_type( $post_ID ) == 'property' ) {

$commission_percent = get_post_meta($post_ID, 'wpcf-commission-percent', true);

$property_price = get_post_meta($post_ID, 'wpcf-property-price', true);

$commission_divider = $property_price / 100;
update_post_meta( $post_ID, 'wpcf-commission-divider', $commission_divider );

$commission_value = $commission_divider * $commission_percent;
update_post_meta( $post_ID, 'wpcf-percentage-commission-value', $commission_value );

}

}
add_action( 'save_post', 'calculate_commission', 99 );

Is there a similar example that we can see?

I built this based off of a previous example of something similar here ......

https://toolset.com/forums/topic/calculate-value-of-a-custom-field-based-on-other-custom-fields-values/

Any help would be greatly appreciated.
Ed

#1169623

Hi Edward,

Thank you for contacting us and I'll be happy to assist.

Can you please make sure that all custom field slugs and post type slug (i.e. "property") used are correct and update your code slightly to:


function calculate_commission( $post_id ) {

	if ( get_post_type( $post_id ) == 'property' ) {

		$commission_percent = get_post_meta($post_id, 'wpcf-commission-percent', true);

		$property_price = get_post_meta($post_id, 'wpcf-property-price', true);

		$commission_divider = $property_price / 100;
		update_post_meta( $post_id, 'wpcf-commission-divider', $commission_divider );

		$commission_value = $commission_divider * $commission_percent;
		update_post_meta( $post_id, 'wpcf-percentage-commission-value', $commission_value );

	}

}
add_action( 'save_post', 'calculate_commission', 99 );

Please test this code with all non-Toolset plugins temporarily disabled, to rule out any code conflict.

You're welcome to share temporary admin login details, in reply to this message, in case the issue still persists. Your next reply will be private so that only you and our support team will have access to it.

Important note: Please make a complete backup copy of the website, before sharing the login details.

regards,
Waqar