| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_TIPS Class - UNDER DEVELOPMENT / NOT IN USE YET |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage Tips |
| 7 |
* @copyright Copyright (c) 2024, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since -- |
| 10 |
* |
| 11 |
*/ |
| 12 |
if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/ |
| 13 |
|
| 14 |
|
| 15 |
/************************************************************************************************************************ |
| 16 |
* |
| 17 |
* |
| 18 |
* WPPIZZA_TIPS |
| 19 |
* |
| 20 |
* |
| 21 |
************************************************************************************************************************/ |
| 22 |
class WPPIZZA_TIPS{ |
| 23 |
|
| 24 |
function __construct() { |
| 25 |
} |
| 26 |
|
| 27 |
/******************************************************************* |
| 28 |
* [ TIPS OPTIONS MARKUP - TIPS OPTIONS BASED ON SUBTOTAL BEFORE TIPS ] |
| 29 |
* @since -- |
| 30 |
* @param array |
| 31 |
* @param array |
| 32 |
* @param int - maximum automatic tip round up options |
| 33 |
* @param any non decimal character to distinguish between % and monitary option selected (as the value could be the same) |
| 34 |
* @return array |
| 35 |
*******************************************************************/ |
| 36 |
function markup($orderData = array(), $event = '' , $maxSteps = 10, $optionIdent = '#'){ |
| 37 |
global $wppizza_options; |
| 38 |
|
| 39 |
$ctips_key = 'ctips'; |
| 40 |
$maxSteps = apply_filters(WPPIZZA_SLUG.'_filter_tips_maxsteps', $maxSteps, $orderData, $event ); |
| 41 |
|
| 42 |
/********************************************************* |
| 43 |
parameters for percentag / total calculations |
| 44 |
*********************************************************/ |
| 45 |
$payment_amount_subtotal = !empty($orderData['summary']['total_before_tips']) ? $orderData['summary']['total_before_tips'] : 0 ; //for percentage option calculations |
| 46 |
//dropdown selected |
| 47 |
$tips_dropdown_select = isset($orderData['user']['tips']['selected']) ? $orderData['user']['tips']['selected'] : '' ; |
| 48 |
//value tips - but empty if select was set to '' |
| 49 |
//$tips_value = isset($orderData['user']['tips']['value']) && $tips_dropdown_select !='' ? wppizza_format_price_float($orderData['user']['tips']['value']) : ''; |
| 50 |
//not set or still set to empty string (i.e not ever set yet) , l;eave as empty string |
| 51 |
$tips_value = !isset($orderData['user']['tips']['value']) || $orderData['user']['tips']['value'] == '' ? '' : wppizza_format_price_float($orderData['user']['tips']['value']); |
| 52 |
|
| 53 |
/*********************************************************************************************************************** |
| 54 |
|
| 55 |
tips percentage options , if enabled |
| 56 |
|
| 57 |
***********************************************************************************************************************/ |
| 58 |
$tips_percentage_options = array(); |
| 59 |
if(!empty($wppizza_options['order_settings']['tips_percentage_options']) && $wppizza_options['order_settings']['tips_display'] > 1 ){ |
| 60 |
|
| 61 |
/* get all set percentage options , removing zero here */ |
| 62 |
$percentage_options = array_filter($wppizza_options['order_settings']['tips_percentage_options']);//remove any zero option that was set |
| 63 |
|
| 64 |
/* sort */ |
| 65 |
asort($percentage_options); |
| 66 |
|
| 67 |
/* loop */ |
| 68 |
foreach($percentage_options as $pc){ |
| 69 |
$pcTips = ($payment_amount_subtotal / 100) * $pc; |
| 70 |
$pcTips = wppizza_format_price_float($pcTips); |
| 71 |
$tips_percentage_options[] = array('label' => $pc.'%', 'tip' => $pcTips . $optionIdent, 'pc' => $pc ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/*********************************************************************************************************************** |
| 76 |
tips round up options |
| 77 |
depending on value |
| 78 |
***********************************************************************************************************************/ |
| 79 |
$tips_roundup_options = array(); |
| 80 |
|
| 81 |
//get base value (integer, without decimals) |
| 82 |
$amountInteger = (int)$payment_amount_subtotal; |
| 83 |
|
| 84 |
//set steps increase |
| 85 |
if($payment_amount_subtotal <= 10 ){ |
| 86 |
$steps = 0.5 ;//0.50 $ steps |
| 87 |
} |
| 88 |
elseif($payment_amount_subtotal <= 100 ){ |
| 89 |
$steps = 1 ;//1 $ steps |
| 90 |
} |
| 91 |
elseif($payment_amount_subtotal <= 250 ){ |
| 92 |
$steps = 5 ;//5 $ steps |
| 93 |
} |
| 94 |
elseif($payment_amount_subtotal <= 500 ){ |
| 95 |
$steps = 10 ;//10 $ steps |
| 96 |
} |
| 97 |
elseif($payment_amount_subtotal <= 1000 ){ |
| 98 |
$steps = 25 ;//25 $ steps |
| 99 |
} |
| 100 |
elseif($payment_amount_subtotal <= 2000 ){ |
| 101 |
$steps = 50 ;//50 $ steps |
| 102 |
} |
| 103 |
elseif($payment_amount_subtotal <= 5000 ){ |
| 104 |
$steps = 100 ;//100 $ steps |
| 105 |
} |
| 106 |
elseif($payment_amount_subtotal <= 10000 ){ |
| 107 |
$steps = 250 ;//250 $ steps |
| 108 |
} |
| 109 |
else{ |
| 110 |
$steps = 500 ;//500 $ steps, I guess this will do ... |
| 111 |
} |
| 112 |
$steps = apply_filters(WPPIZZA_SLUG.'_filter_tips_steps', $steps, $payment_amount_subtotal, $orderData); |
| 113 |
|
| 114 |
/*************************** |
| 115 |
calculate the options |
| 116 |
***************************/ |
| 117 |
// find the first sensible depending on steps (so we use 125,130, 135 etc for a 122 bill and not 127, 132 etc) |
| 118 |
$loopCount = $steps < 10 ? 10 : abs((int)$steps); |
| 119 |
for($i = 1; $i <= $loopCount ; $i++){ |
| 120 |
//if steps are not integers, the is_int check wont work, so will simply set the next one that's > current amount |
| 121 |
if(!is_int($steps)){ |
| 122 |
$nextStep = $amountInteger + ($i * $steps); |
| 123 |
if($nextStep > $payment_amount_subtotal){ |
| 124 |
$firstTipOption = $nextStep; |
| 125 |
break; |
| 126 |
} |
| 127 |
}else{ |
| 128 |
$nextStep = $amountInteger + $i; |
| 129 |
if(is_int($nextStep / $steps)){ |
| 130 |
$firstTipOption = $nextStep; |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
//get all options starting from first determined |
| 136 |
for($i = 0; $i < $maxSteps ; $i++){ |
| 137 |
$tipStep = $firstTipOption + ($i * $steps); |
| 138 |
$stepsTips = $tipStep - $payment_amount_subtotal ; |
| 139 |
$tips_roundup_options[] = array('label' => wppizza_format_price($tipStep), 'tip' => $stepsTips); |
| 140 |
} |
| 141 |
|
| 142 |
/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/* |
| 143 |
# |
| 144 |
# generate markup |
| 145 |
# |
| 146 |
*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*\/*/ |
| 147 |
|
| 148 |
//set 'amount' to be selected - overwritten as required |
| 149 |
$select_amount_option = true; |
| 150 |
|
| 151 |
/************************* |
| 152 |
select markup |
| 153 |
*************************/ |
| 154 |
$markup_select = ''; |
| 155 |
|
| 156 |
$markup_select .= '<select id="'.WPPIZZA_SLUG.'-'.$ctips_key.'-select">';//data-total="'.$payment_amount.'" data-fractions="'.wppizza_currency_precision().'" |
| 157 |
|
| 158 |
//option markup - amount |
| 159 |
$sel_markup = (string)$tips_dropdown_select == 'amount' ? ' selected="selected"' : '' ; |
| 160 |
$markup_select .= '<option value="amount" '.$sel_markup.'>'.$wppizza_options['localization']['tips'].'</option>'; |
| 161 |
|
| 162 |
//option markup - no tip |
| 163 |
$sel_markup = (string)$tips_dropdown_select == '0' ? ' selected="selected"' : '' ; |
| 164 |
$markup_select .= '<option value="0" '.$sel_markup.'>-- '.$wppizza_options['localization']['no_tip'].' --</option>'; |
| 165 |
|
| 166 |
//options - percentages |
| 167 |
foreach($tips_percentage_options as $value){ |
| 168 |
//option markup |
| 169 |
$sel_markup = (string)$tips_dropdown_select == (string)$value['tip'] ? ' selected="selected"' : '' ; |
| 170 |
$markup_select .= '<option data-pc="'.$value['pc'].'" value="'.$value['tip'].'" '.$sel_markup.'>'.sprintf(wppizza_sanitise_forsprintf($wppizza_options['localization']['tip_value'], 1), $value['label']).'</option>'; |
| 171 |
} |
| 172 |
|
| 173 |
//options - roundup |
| 174 |
foreach($tips_roundup_options as $value){ |
| 175 |
//option markup |
| 176 |
$sel_markup = (string)$tips_dropdown_select == (string)$value['tip'] ? ' selected="selected"' : '' ; |
| 177 |
$markup_select .= '<option value="'.$value['tip'].'" '.$sel_markup.' >'.sprintf(wppizza_sanitise_forsprintf($wppizza_options['localization']['roundup_value'], 1) , $value['label']).'</option>'; |
| 178 |
} |
| 179 |
|
| 180 |
//end select markup |
| 181 |
$markup_select .= '</select>'; |
| 182 |
|
| 183 |
/************************** |
| 184 |
direct input |
| 185 |
**************************/ |
| 186 |
$required_attribute = ''; |
| 187 |
/* is pickup */ |
| 188 |
if(!empty($orderData['summary']['self_pickup']) && !empty($wppizza_options['order_form'][$ctips_key]['required_on_pickup']) ){ |
| 189 |
$required_attribute = 'required = "required" '; |
| 190 |
} |
| 191 |
/* is delivery */ |
| 192 |
if(empty($orderData['summary']['self_pickup']) && !empty($wppizza_options['order_form'][$ctips_key]['required']) ){ |
| 193 |
$required_attribute = 'required = "required" '; |
| 194 |
} |
| 195 |
//adding - unused - subtotal before tips as data-subtotal just for info if we want to doublecheck tip percentage calculations for example |
| 196 |
$markup_input = '<input type="text" id="' . WPPIZZA_SLUG . '-'.$ctips_key.'" data-subtotal="'.esc_attr($payment_amount_subtotal).'" class="' . WPPIZZA_SLUG . '-'.$ctips_key.'" name="'.$ctips_key.'" autocomplete="off" size="20" placeholder="' .$wppizza_options['order_form'][$ctips_key]['placeholder'] . '" value="'.wppizza_format_price($tips_value, null).'" '.$required_attribute.' />'; |
| 197 |
|
| 198 |
/************************** |
| 199 |
label in div, before |
| 200 |
**************************/ |
| 201 |
$markup_label = ''; |
| 202 |
//currently not using a lable as it's added to dropdown as first option |
| 203 |
#$markup_label = !empty($wppizza_options['localization']['tips']) ? '<div id="' . WPPIZZA_SLUG . '-'.$ctips_key.'-label" class="' . WPPIZZA_SLUG . '-'.$ctips_key.'-label" >'.$wppizza_options['localization']['tips'] . '</div>' : '' ; |
| 204 |
|
| 205 |
/*---------------------------------------------------------------------------------------------- |
| 206 |
|
| 207 |
ORDERPAGE ONLY - DROPDOWN AND INPUT TEXT FIELD |
| 208 |
|
| 209 |
input and select inverted here as we are floating:right |
| 210 |
wrap in div for possible error messages (if set to be required for example) |
| 211 |
----------------------------------------------------------------------------------------------*/ |
| 212 |
if($event == 'orderpage'){ |
| 213 |
$markup = array( |
| 214 |
'sort' => 170, |
| 215 |
'class_ident' => 'tips', |
| 216 |
'label' => '<div>'.$markup_label. $markup_input,//html td +wrapper div |
| 217 |
'value_formatted' => $markup_select.'</div>',//html /td + wrapper div |
| 218 |
'value' => $tips_value, |
| 219 |
'fullwidth' => true, |
| 220 |
); |
| 221 |
} |
| 222 |
else{ |
| 223 |
|
| 224 |
$markup = array(); |
| 225 |
//only return a filled array - outside order page - if tips > 0 |
| 226 |
if(is_numeric($tips_value) && !empty($tips_value) ){ |
| 227 |
$markup = array( |
| 228 |
'sort' => 170, |
| 229 |
'class_ident' => 'tips', |
| 230 |
'label' => !empty($wppizza_options['localization']['tips']) ? $wppizza_options['localization']['tips'] : '', |
| 231 |
'value_formatted' => wppizza_format_price($tips_value), |
| 232 |
'value' => $tips_value, |
| 233 |
'fullwidth' => false, |
| 234 |
); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/* |
| 240 |
make filterable and return as array |
| 241 |
*/ |
| 242 |
$markup = apply_filters( strtolower(__CLASS__ .'_'.__method__) , $markup, $event); |
| 243 |
|
| 244 |
return $markup; |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
?> |