Skip Navigation

[Resolved] Problems trying to create a calculate function

This support ticket is created 5 years, 4 months ago. 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.

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)

This topic contains 1 reply, has 2 voices.

Last updated by Edward Barker 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#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

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

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

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.