Admin Settings in all CPTs are already set. Continue to frontend.

This commit is contained in:
dwindown
2025-10-13 15:20:14 +07:00
parent 6f6b039441
commit 0446eb1064
11 changed files with 1610 additions and 884 deletions

View File

@@ -15,6 +15,8 @@ class Order {
private $order_details;
private $chosen_currency;
/**
* Initializes the plugin by setting filters and administration functions.
*/
@@ -78,7 +80,6 @@ class Order {
wp_send_json_error([
'message' => 'Nonce verification failed'
]);
// wp_send_json_error() dies internally, no need for exit
}
// Sanitize and unslash inputs explicitly
@@ -89,6 +90,7 @@ class Order {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$order_meta_data = isset($_REQUEST['meta_data']) ? wp_unslash($_REQUEST['meta_data']) : [];
$purpose = isset($_REQUEST['purpose']) ? sanitize_text_field(wp_unslash($_REQUEST['purpose'])) : '';
$this->currency = isset($_REQUEST['currency']) ? wp_unslash($_REQUEST['currency']) : formipay_default_currency('symbol');
$this->form_id = $form_id;
@@ -104,6 +106,7 @@ class Order {
if ($purpose === 'calculate') {
$order_data['total'] = $total;
$order_data['items'] = $order_details;
$order_data['form_data'] = formipay_get_post_meta($form_id);
wp_send_json($order_data);
}
@@ -206,64 +209,98 @@ class Order {
public function process_order_details() {
$currency_codes = [];
$allowed_currencies = formipay_get_post_meta($this->form_id, 'allowed_currencies');
if(!empty($allowed_currencies)){
$parse_currencies = json_decode($allowed_currencies, true);
foreach($parse_currencies as $currency_data){
$parse_currency = explode(':::', $currency_data);
$currency_codes[] = $parse_currency[0];
}
}
$details = [];
$product_price = floatval(formipay_get_post_meta($this->form_id, 'product_price'));
$details[] = [
'item' => html_entity_decode(get_the_title($this->form_id)),
'amount' => $product_price,
'qty' => $this->order_data['qty'],
'subtotal' => floatval($product_price) * intval($this->order_data['qty']),
'context' => 'main'
];
// $product_price = floatval(formipay_get_post_meta($this->form_id, 'product_price'));
// $details[] = [
// 'item' => html_entity_decode(get_the_title($this->form_id)),
// 'amount' => $product_price,
// 'qty' => (int) $this->order_data['qty'],
// 'subtotal' => floatval($product_price) * intval($this->order_data['qty']),
// 'context' => 'main'
// ];
$check_fields = formipay_get_post_meta($this->form_id, 'formipay_settings');
// $check_fields = formipay_get_post_meta($this->form_id, 'formipay_settings');
if(!empty($check_fields['fields'])){
foreach($check_fields['fields'] as $field){
// if($field['field_type'] == 'select'){
if(in_array($field['field_type'], ['select','checkbox', 'radio'])) {
$options = $field['field_options'];
if(!empty($options)){
foreach($options as $option){
// if(!empty($check_fields['fields'])){
// foreach($check_fields['fields'] as $field){
// // if($field['field_type'] == 'select'){
// if(in_array($field['field_type'], ['select','checkbox', 'radio'])) {
// $options = $field['field_options'];
// if(!empty($options)){
// foreach($options as $option){
$option_value = ($field['show_toggle']['value'] && '' !== $option['value']) ? $option['value'] : $option['label'];
// $option_value = ($field['show_toggle']['value'] && '' !== $option['value']) ? $option['value'] : $option['label'];
if(!empty($this->order_data[$field['field_id']])) {
$field_value = $this->order_data[$field['field_id']];
if($field['field_type'] == 'select'){
$field_value = ($field['show_toggle']['value']) ?
$this->order_data[$field['field_id']]['value'] :
$this->order_data[$field['field_id']]['label'];
}
$field_value = explode(',', $field_value);
// if(!empty($this->order_data[$field['field_id']])) {
// $field_value = $this->order_data[$field['field_id']];
// if($field['field_type'] == 'select'){
// $field_value = ($field['show_toggle']['value']) ?
// $this->order_data[$field['field_id']]['value'] :
// $this->order_data[$field['field_id']]['label'];
// }
// $field_value = explode(',', $field_value);
$context = 'no-context';
if(floatval($option['amount']) < 0){
$context = 'sub';
}elseif(floatval($option['amount']) > 0){
$context = 'add';
}
// $context = 'no-context';
// if(floatval($option['amount']) < 0){
// $context = 'sub';
// }elseif(floatval($option['amount']) > 0){
// $context = 'add';
// }
if(!empty($field_value) && $field['show_toggle']['amount'] == 'yes'){
foreach($field_value as $f_value){
if($option_value == $f_value){
$qty = ($option['qty'] == 'yes') ? $this->order_data['qty'] : 1;
$details[] = [
'item' => $field['label'] .' - '. $option['label'],
'amount' => floatval($option['amount']),
'qty' => $qty,
'subtotal' => floatval($option['amount']) * intval($qty),
'context' => $context
];
}
}
}
}
// if(!empty($field_value) && $field['show_toggle']['amount'] == 'yes'){
// foreach($field_value as $f_value){
// if($option_value == $f_value){
// $qty = ($option['qty'] == 'yes') ? $this->order_data['qty'] : 1;
// $details[] = [
// 'item' => $field['label'] .' - '. $option['label'],
// 'amount' => floatval($option['amount']),
// 'qty' => (int) $qty,
// 'subtotal' => floatval($option['amount']) * intval($qty),
// 'context' => $context
// ];
// }
// }
// }
// }
}
}
}
// }
// }
// }
// }
// }
/**
* Cart items (not implemented yet)
*/
/**
* Attached Product
*/
$products = formipay_get_post_meta($this->form_id, 'static_products');
if(!empty($products)){
$products = explode(',', $products);
foreach($products as $product_id){
$product_data = formipay_get_post_meta($product_id);
$regular_price = formipay_get_post_meta($product_id, 'setting_product_price_regular_'.$this->currency);
$sale_price = formipay_get_post_meta($product_id, 'setting_product_price_sale_'.$this->currency);
$this_item = [
'item' => html_entity_decode(get_the_title($product_id)),
'amount' => (float) $sale_price ?: $regular_price,
'qty' => 1,
'subtotal' => (float) $sale_price ?: $regular_price,
];
}
}