Finalize the backend for each coupon, customer and access functionalities

This commit is contained in:
dwindown
2025-09-24 21:46:03 +07:00
parent 4151ea3b9e
commit 6f6b039441
6 changed files with 70 additions and 138 deletions

View File

@@ -474,7 +474,7 @@ class Access {
// All Forms
$forms = get_posts([
'post_type' => 'formipay-form',
'post_type' => 'formipay-product',
'posts_per_page' => -1,
]);

View File

@@ -112,7 +112,6 @@ class Coupon {
'columns' => [
'id' => esc_html__( 'ID', 'formipay' ),
'code' => esc_html__( 'Coupon Code', 'formipay' ),
'products' => esc_html__( 'Product Relation', 'formipay' ),
'usages' => esc_html__( 'Usages', 'formipay' ),
'date_limit' => esc_html__( 'Date Limit', 'formipay' ),
'status' => esc_html__( 'Status', 'formipay' ),
@@ -388,6 +387,7 @@ class Coupon {
),
'customers' => array(
'type' => 'autocomplete',
'post_type' => array('formipay-product'),
'label' => __( 'Customers', 'formipay' ),
'description' => __( 'Only selected customer(s) can use the coupon. Leave empty to apply to all customers.', 'formipay' )
)
@@ -604,6 +604,7 @@ class Coupon {
}
$get_coupons = get_posts($args);
$global_currencies = get_global_currency_array();
$coupons = [];
if(!empty($get_coupons)){
@@ -611,50 +612,38 @@ class Coupon {
$active = true;
$products = !empty(formipay_get_post_meta($coupon->ID, 'forms')) ? explode(',', formipay_get_post_meta($coupon->ID, 'forms')) : [];
$product_titles = [];
if(!empty($products)){
foreach($products as $product_id){
if ( FALSE !== get_post_status( $product_id ) ) {
$product_titles[] = [
'flag' => formipay_get_flag_by_currency(formipay_get_post_meta($product_id, 'product_currency')),
'currency' => formipay_get_post_meta($product_id, 'product_currency'),
'title' => get_the_title($product_id)
];
}
}
}
if(!empty($_REQUEST['product'])){
$product_id = intval($_REQUEST['product']);
$forms = formipay_get_post_meta($coupon->ID, 'forms');
if(false !== $forms){
$forms = explode(',', $forms);
$found = false;
if(!empty($forms)){
foreach($forms as $form_id){
if($product_id == $form_id){
$active = true;
$found = true;
break;
}
$active = false;
}
if(false == $found){
unset($get_total_coupons[$key]);
}
}
}
}
$date_limit = formipay_get_post_meta($coupon->ID, 'date_limit');
$type = formipay_get_post_meta($coupon->ID, 'type');
$amount_meta_key = "amount_$type";
if($type == 'fixed'){
$amount_value = [];
foreach($global_currencies as $currency){
$raw = $currency['currency'];
$decimal_digits = $currency['decimal_digits'];
$decimal_symbol = $currency['decimal_symbol'];
$thousand_separator = $currency['thousand_separator'];
$parse_currency = explode(':::', $raw);
$currency_code = $parse_currency[0];
$amount_in_currency = formipay_get_post_meta($coupon->ID, 'amount_fixed_'.$currency_code);
$amount_value[] = [
'raw' => $raw,
'amount' => number_format($amount_in_currency, $decimal_digits, $decimal_symbol, $thousand_separator),
'flag' => formipay_get_flag_by_currency($raw)
];
}
}else{
$amount_value = floatval(formipay_get_post_meta($coupon->ID, $amount_meta_key));
}
if($active){
$coupons[] = [
'ID' => $coupon->ID,
'code' => get_the_title($coupon->ID),
'products' => $product_titles,
'value' => floatval(formipay_get_post_meta($coupon->ID, 'amount')),
'value' => $amount_value,
'type' => ucfirst(formipay_get_post_meta($coupon->ID, 'type')),
'case_sensitive' => formipay_get_post_meta($coupon->ID, 'case_sensitive'),
'usages' => [

View File

@@ -453,7 +453,17 @@ class Customer {
public function autocomplete_options($r, $args) {
console.log(print_r($args, true));
$r = [];
$customers = $this->get();
if(!empty($customers)) {
foreach($customers as $customer){
$r[] = [
'id' => $customer->id,
'title' => $customer->name . ($customer->email ? ' | ' .$customer->email : '') . ($customer->phone ? ' | ' .$customer->phone : '')
];
}
}
return $r;
}