CB code field usage

7 years 1 month ago #292836 by osmodels
CB code field usage was created by osmodels
Hi,

I am using the CB Code field to display a calculated value.

My requirement is to use the value from a drop-down field, and populate a Code field with a calculated value.

I use the following code in the Code field.
$person = $user->cb_no_sponsored_members_pii;
$p = 10 - $person;
$discountperperson = ($p * 150) + ($p * 1900);
return $discountperperson;

This is the drop-down field name "cb_no_sponsored_members_pii". Based on the value selected in this field, code field should change. The code is returning the value 20500 which is returned when the $person variable is not assigned correctly.

I was following the tutorial for CB Code Fields in the site.

Can you tell me what is the problem with my code?

My main requirement is a variable subscription price. I am using the Promotions to implement this.

Please Log in to join the conversation.

7 years 1 month ago #292844 by krileon
Replied by krileon on topic CB code field usage
Your PHP is working exactly as you've coded it. If cb_no_sponsored_members_pii doesn't have a value then $p is just 10 and the reset of your math is multiplying by 10 as coded. If cb_no_sponsored_members_pii does have a value and it's still doing that then it's not finding a field with the name or the field value isn't numeric. You can use substitutions in your code though so the below could be used for example.

$p = ( 10 - (int) '[cb_no_sponsored_members_pii]' );

return ( ( $p * 150 ) + ( $p * 1900 ) );


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

7 years 1 month ago #292896 by osmodels
Replied by osmodels on topic CB code field usage
Hi krileon

I tried using the code provided by you. You are right that the calculation is done based on the filed [cb_no_sponsored_members_pii] value is 0.

This field is a drop down box and on loading initially the value is unset. When the user fills in this field, the calculation should happen and the Code field should be updated to reflect the new amount.

I have seen an Update On option for Query Dropdown, which gets updated dynamically based on another field. I want something similar to happen here.

Is it possible?

Thanks.

Please Log in to join the conversation.

7 years 1 month ago #292902 by krileon
Replied by krileon on topic CB code field usage

When the user fills in this field, the calculation should happen and the Code field should be updated to reflect the new amount.

There is no functionality for that.

I have seen an Update On option for Query Dropdown, which gets updated dynamically based on another field. I want something similar to happen here.

Such a feature does not exist. Eventually CB Core Fields Ajax will be able to do Update On on any fieldtype, but for now it can't.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.221 seconds

Facebook Twitter LinkedIn