From c39a656ab15b66d1d0c85ccf0d8eb34104b9d94b Mon Sep 17 00:00:00 2001 From: asus Date: Sun, 17 Mar 2024 17:59:27 +0100 Subject: [PATCH] price calculation now works whatever acf return is set --- .../cipf_plugin/php/paypal/user_can_pay.php | 108 ++++++------------ 1 file changed, 33 insertions(+), 75 deletions(-) diff --git a/plugins/cipf_plugin/php/paypal/user_can_pay.php b/plugins/cipf_plugin/php/paypal/user_can_pay.php index 7e2e2bb..70ea388 100644 --- a/plugins/cipf_plugin/php/paypal/user_can_pay.php +++ b/plugins/cipf_plugin/php/paypal/user_can_pay.php @@ -10,6 +10,31 @@ if (!defined('ABSPATH')) { +/* +* return value of an acf field, no matter if the field is set up to return label or value +* +*/ +function get_acf_value_CIPF($field_name, $acf_id) { + $field_object = get_field_object($field_name, $acf_id); + $return_format = $field_object['return_format']; + $field = get_field($field_name, $acf_id); + + $value = null; + if ($return_format === 'array') { + $value = $field['value']; + } + else if ($return_format === 'value') { + $value = $field; + } + else if ($return_format === 'label') { + $choices = $field_object['choices']; + $value = array_search($field, $choices); + } + + return $value; +} + + /* * can pay in certain conditions @@ -44,81 +69,14 @@ function check_can_pay_CIPF() { * in case it was changed from admin pannel * */ - $tarif_object = get_field_object($acf_card_price_choice['_name'], $acf_id); - error_log("tarif_object"); - error_log(json_encode($tarif_object)); - /* -{ - "ID":34541, - "key":"field_65d24447cc89f", - "label":"Tarif", - "name":"tarif", - "aria-label":"", - "prefix":"acf", - "type":"radio", - "value":"Je suis hors r\u00e9seau FIPF et je souhaite commander la carte \u00e0 15 \u20ac", - "menu_order":3, - "instructions":"", - "required":0, - "id":"", - "class":"", - "conditional_logic":0, - "parent":34538, - "wrapper":{ - "width":"", - "class":"", - "id":"" - }, - "choices":{ - "10":"Mon association adh\u00e8re \u00e0 la FIPF, je peux commander la carte \u00e0 10 \u20ac", - "15":"Je suis hors r\u00e9seau FIPF et je souhaite commander la carte \u00e0 15 \u20ac" - }, - "default_value":"", - "return_format":"label", - "allow_null":0, - "other_choice":0, - "layout":"vertical", - "save_other_choice":0, - "_name":"tarif", - "_valid":1 -} -{ - "ID":34541, - "key":"field_65d24447cc89f", - "label":"Tarif", - "name":"tarif", - "aria-label":"", - "prefix":"acf", - "type":"radio", - "value":"15", - "menu_order":3, - "instructions":"", - "required":0,"id":"", - "class":"", - "conditional_logic":0, - "parent":34538, - "wrapper":{"width":"","class":"","id":""}, - "choices":{ - "10":"Mon association adh\u00e8re \u00e0 la FIPF, je peux commander la carte \u00e0 10 \u20ac", - "15":"Je suis hors r\u00e9seau FIPF et je souhaite commander la carte \u00e0 15 \u20ac" - }, - "default_value":"", - "return_format":"value", - "allow_null":0, - "other_choice":0, - "layout":"vertical", - "save_other_choice":0, - "_name":"tarif", - "_valid":1 -} - */ -// $tarif_value = ; -// $tarif = (int)$tarif_value; - $livraison_object = get_field_object($acf_card_price_delivery['_name'], $acf_id); -// $livraison_value = ; -// $livraison = (int)$livraison_value; -// $price = $tarif + $livraison; - $price = 3; + $tarif = get_acf_value_CIPF($acf_card_price_choice['_name'], $acf_id); + $livraison = get_acf_value_CIPF($acf_card_price_delivery['_name'], $acf_id); + if ($tarif !== null && $livraison !== null) { + $price = $tarif + $livraison; + } + else { + $price = 0; + } update_field($acf_card_price_total['_name'], $price, $acf_id); /*