diff --git a/admin/assets/css/page-coupons.css b/admin/assets/css/page-coupons.css index e188f03..11c491b 100644 --- a/admin/assets/css/page-coupons.css +++ b/admin/assets/css/page-coupons.css @@ -20,20 +20,23 @@ a#add-new-coupon:hover { border: 1px solid #b7b7b7!important; } -td.gridjs-td[data-column-id=productRelation] > span { +td.gridjs-td[data-column-id=amount] > span { display: flex; gap: .5em; flex-wrap: wrap; + justify-content: center; } -.product_related { - background-color: #dedede; - padding: 5px 7px; +td.gridjs-td[data-column-id=amount] > span > span { + display: flex; + gap: .5em; + align-items: center; + background-color: #f8f8f8; + padding: 3px 5px; border-radius: 5px; -} -span.product_related img { - margin-bottom: -3px; + box-shadow: 0 3px 5px #ccc; } th.gridjs-th[data-column-id=id], +th.gridjs-th[data-column-id=amount], th.gridjs-th[data-column-id=usages], th.gridjs-th[data-column-id=type], th.gridjs-th[data-column-id=dateLimit], @@ -41,17 +44,13 @@ th.gridjs-th[data-column-id=status] { text-align: center; } td.gridjs-td[data-column-id=id], +td.gridjs-td[data-column-id=amount], td.gridjs-td[data-column-id=usages], td.gridjs-td[data-column-id=type], td.gridjs-td[data-column-id=dateLimit] { text-align: center; } -th.gridjs-th[data-column-id=amount], -td.gridjs-td[data-column-id=amount] { - text-align: right; -} - span:has(.status-label) { width: 100%; display: block; diff --git a/admin/assets/js/page-coupons.js b/admin/assets/js/page-coupons.js index ba3c84b..79c5cbd 100644 --- a/admin/assets/js/page-coupons.js +++ b/admin/assets/js/page-coupons.js @@ -1,49 +1,3 @@ -document.addEventListener('DOMContentLoaded', function () { - const choices = new Choices('#products', { - searchEnabled: true, - searchChoices: false, // Prevent Choices.js from filtering the local list - searchResultLimit: 10, // Optional: Limit visible results - placeholder: true, - placeholderValue: formipay_coupons_page.filter_form.products.placeholder, - noResultsText: formipay_coupons_page.filter_form.products.noresult_text, - itemSelectText: '', - }); - - const searchInput = document.querySelector('.choices__input--cloned'); - let typingTimer; - - searchInput.addEventListener('input', function () { - const query = searchInput.value; - - if (query.length >= 3) { - clearTimeout(typingTimer); - typingTimer = setTimeout(() => { - fetchChoices(query); - }, 300); // Add a debounce delay - } - }); - - function fetchChoices(query) { - fetch(formipay_coupons_page.ajax_url, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', - }, - body: new URLSearchParams({ - action: 'formipay_coupon_get_products', - search: query, - _wpnonce: formipay_coupons_page.nonce - }), - }) - .then((response) => response.json()) - .then((data) => { - choices.clearChoices(); - choices.setChoices(data, 'value', 'label', false); - }) - .catch((error) => console.error('Error:', error)); - } -}); - jQuery(function($){ let formipay_coupon_table_grid = new gridjs.Grid({ @@ -56,7 +10,7 @@ jQuery(function($){ } return data.results.map( - coupon => [coupon.ID, coupon.ID, coupon.code, coupon.products, coupon.value, coupon.type, coupon.usages, coupon.date_limit, coupon.status, coupon.case_sensitive] + coupon => [coupon.ID, coupon.ID, coupon.code, coupon.value, coupon.type, coupon.usages, coupon.date_limit, coupon.status, coupon.case_sensitive] ); }, total: data => data.total @@ -83,40 +37,23 @@ jQuery(function($){ `) }, - { - name: formipay_coupons_page.columns.products, - formatter: (products, row) => { - - let html = ''; - - // Loop through each product in the products array - if(products.length > 0){ - products.forEach(product => { - if (product) { - const currencyDetails = product.currency.split(':::'); - const currencyCode = currencyDetails[0]; - const currencySymbol = currencyDetails[2]; - - html += ` - - ${product.title}
- - ${product.flag ? `` : ''} - ${currencyCode} (${currencySymbol}) - -
- `; - } - }); - } - - return gridjs.html(html); - - } - }, { name: formipay_coupons_page.columns.amount, - formatter: (_, row) => numberFormat(_) + formatter: (_, row) => { + var amount = 'unset'; + if(row.cells[4].data === 'Percentage') { + amount = numberFormat(_)+'%'; + } + if(row.cells[4].data === 'Fixed'){ + amount = ''; + _.forEach(function(currency){ + const splitRaw = currency.raw.split(':::'); + const symbol = splitRaw[2] ? splitRaw[2] : splitRaw[0]; + amount += `${symbol} ${currency.amount}`; + }) + } + return gridjs.html(amount); + } }, { name: formipay_coupons_page.columns.type, @@ -215,16 +152,16 @@ jQuery(function($){ function refresh_table_with_filter() { formipay_coupon_table_grid.updateConfig({ server: { - url: formipay_coupons_page.ajax_url+'?action=formipay-tabledata-coupons&product='+document.getElementById('products').value+'&search='+document.getElementById('keyword').value+'&_wpnonce='+formipay_coupons_page.nonce, + url: formipay_coupons_page.ajax_url+'?action=formipay-tabledata-coupons&search='+document.getElementById('keyword').value+'&_wpnonce='+formipay_coupons_page.nonce, then: data => data.results.map( - coupon => [coupon.ID, coupon.ID, coupon.code, coupon.products, coupon.value, coupon.type, coupon.usages, coupon.date_limit, coupon.status, coupon.case_sensitive] + coupon => [coupon.ID, coupon.ID, coupon.code, coupon.value, coupon.type, coupon.usages, coupon.date_limit, coupon.status, coupon.case_sensitive] ), total: data => data.total } }).forceRender(); } - $('.form-tool, #products, #post_status').on('change', function(){ + $('.form-tool, #post_status').on('change', function(){ refresh_table_with_filter(); }); diff --git a/admin/page-coupons.php b/admin/page-coupons.php index 8b71160..ba448c5 100644 --- a/admin/page-coupons.php +++ b/admin/page-coupons.php @@ -26,9 +26,6 @@
-
- -
diff --git a/includes/Access.php b/includes/Access.php index aee1748..5d9d59a 100644 --- a/includes/Access.php +++ b/includes/Access.php @@ -474,7 +474,7 @@ class Access { // All Forms $forms = get_posts([ - 'post_type' => 'formipay-form', + 'post_type' => 'formipay-product', 'posts_per_page' => -1, ]); diff --git a/includes/Coupon.php b/includes/Coupon.php index cffec98..a32b067 100644 --- a/includes/Coupon.php +++ b/includes/Coupon.php @@ -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' => [ diff --git a/includes/Customer.php b/includes/Customer.php index 54696c1..acd4a99 100644 --- a/includes/Customer.php +++ b/includes/Customer.php @@ -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; }