Finalize the backend for each coupon, customer and access functionalities
This commit is contained in:
@@ -20,20 +20,23 @@ a#add-new-coupon:hover {
|
|||||||
border: 1px solid #b7b7b7!important;
|
border: 1px solid #b7b7b7!important;
|
||||||
}
|
}
|
||||||
|
|
||||||
td.gridjs-td[data-column-id=productRelation] > span {
|
td.gridjs-td[data-column-id=amount] > span {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: .5em;
|
gap: .5em;
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
|
justify-content: center;
|
||||||
}
|
}
|
||||||
.product_related {
|
td.gridjs-td[data-column-id=amount] > span > span {
|
||||||
background-color: #dedede;
|
display: flex;
|
||||||
padding: 5px 7px;
|
gap: .5em;
|
||||||
|
align-items: center;
|
||||||
|
background-color: #f8f8f8;
|
||||||
|
padding: 3px 5px;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
}
|
box-shadow: 0 3px 5px #ccc;
|
||||||
span.product_related img {
|
|
||||||
margin-bottom: -3px;
|
|
||||||
}
|
}
|
||||||
th.gridjs-th[data-column-id=id],
|
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=usages],
|
||||||
th.gridjs-th[data-column-id=type],
|
th.gridjs-th[data-column-id=type],
|
||||||
th.gridjs-th[data-column-id=dateLimit],
|
th.gridjs-th[data-column-id=dateLimit],
|
||||||
@@ -41,17 +44,13 @@ th.gridjs-th[data-column-id=status] {
|
|||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
td.gridjs-td[data-column-id=id],
|
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=usages],
|
||||||
td.gridjs-td[data-column-id=type],
|
td.gridjs-td[data-column-id=type],
|
||||||
td.gridjs-td[data-column-id=dateLimit] {
|
td.gridjs-td[data-column-id=dateLimit] {
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
th.gridjs-th[data-column-id=amount],
|
|
||||||
td.gridjs-td[data-column-id=amount] {
|
|
||||||
text-align: right;
|
|
||||||
}
|
|
||||||
|
|
||||||
span:has(.status-label) {
|
span:has(.status-label) {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
display: block;
|
display: block;
|
||||||
|
|||||||
@@ -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($){
|
jQuery(function($){
|
||||||
|
|
||||||
let formipay_coupon_table_grid = new gridjs.Grid({
|
let formipay_coupon_table_grid = new gridjs.Grid({
|
||||||
@@ -56,7 +10,7 @@ jQuery(function($){
|
|||||||
}
|
}
|
||||||
|
|
||||||
return data.results.map(
|
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
|
total: data => data.total
|
||||||
@@ -83,40 +37,23 @@ jQuery(function($){
|
|||||||
</span>
|
</span>
|
||||||
`)
|
`)
|
||||||
},
|
},
|
||||||
{
|
|
||||||
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 += `
|
|
||||||
<span class="product_related">
|
|
||||||
${product.title}<hr>
|
|
||||||
<span class="country-currency">
|
|
||||||
${product.flag ? `<img src="${product.flag}" height="18">` : ''}
|
|
||||||
<span class="currency-in-use">${currencyCode} (${currencySymbol})</span>
|
|
||||||
</span>
|
|
||||||
</span>
|
|
||||||
`;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return gridjs.html(html);
|
|
||||||
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: formipay_coupons_page.columns.amount,
|
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 += `<span class="coupon-each-amount"><img src="${currency.flag}" width="18"><span class="amount-value">${symbol} ${currency.amount}</span></span>`;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return gridjs.html(amount);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: formipay_coupons_page.columns.type,
|
name: formipay_coupons_page.columns.type,
|
||||||
@@ -215,16 +152,16 @@ jQuery(function($){
|
|||||||
function refresh_table_with_filter() {
|
function refresh_table_with_filter() {
|
||||||
formipay_coupon_table_grid.updateConfig({
|
formipay_coupon_table_grid.updateConfig({
|
||||||
server: {
|
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(
|
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
|
total: data => data.total
|
||||||
}
|
}
|
||||||
}).forceRender();
|
}).forceRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
$('.form-tool, #products, #post_status').on('change', function(){
|
$('.form-tool, #post_status').on('change', function(){
|
||||||
refresh_table_with_filter();
|
refresh_table_with_filter();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -26,9 +26,6 @@
|
|||||||
<a data-value="inactive"><?php echo esc_html__( 'Inactive', 'formipay' ); ?></a><span class="inactive-post-count"></span>
|
<a data-value="inactive"><?php echo esc_html__( 'Inactive', 'formipay' ); ?></a><span class="inactive-post-count"></span>
|
||||||
</div>
|
</div>
|
||||||
<div class="filter-bar">
|
<div class="filter-bar">
|
||||||
<div class="products">
|
|
||||||
<select id="products" class="filter-products"></select>
|
|
||||||
</div>
|
|
||||||
<div class="search">
|
<div class="search">
|
||||||
<input type="text" id="keyword" placeholder="Keyword" class="form-tool">
|
<input type="text" id="keyword" placeholder="Keyword" class="form-tool">
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -474,7 +474,7 @@ class Access {
|
|||||||
|
|
||||||
// All Forms
|
// All Forms
|
||||||
$forms = get_posts([
|
$forms = get_posts([
|
||||||
'post_type' => 'formipay-form',
|
'post_type' => 'formipay-product',
|
||||||
'posts_per_page' => -1,
|
'posts_per_page' => -1,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -112,7 +112,6 @@ class Coupon {
|
|||||||
'columns' => [
|
'columns' => [
|
||||||
'id' => esc_html__( 'ID', 'formipay' ),
|
'id' => esc_html__( 'ID', 'formipay' ),
|
||||||
'code' => esc_html__( 'Coupon Code', 'formipay' ),
|
'code' => esc_html__( 'Coupon Code', 'formipay' ),
|
||||||
'products' => esc_html__( 'Product Relation', 'formipay' ),
|
|
||||||
'usages' => esc_html__( 'Usages', 'formipay' ),
|
'usages' => esc_html__( 'Usages', 'formipay' ),
|
||||||
'date_limit' => esc_html__( 'Date Limit', 'formipay' ),
|
'date_limit' => esc_html__( 'Date Limit', 'formipay' ),
|
||||||
'status' => esc_html__( 'Status', 'formipay' ),
|
'status' => esc_html__( 'Status', 'formipay' ),
|
||||||
@@ -388,6 +387,7 @@ class Coupon {
|
|||||||
),
|
),
|
||||||
'customers' => array(
|
'customers' => array(
|
||||||
'type' => 'autocomplete',
|
'type' => 'autocomplete',
|
||||||
|
'post_type' => array('formipay-product'),
|
||||||
'label' => __( 'Customers', 'formipay' ),
|
'label' => __( 'Customers', 'formipay' ),
|
||||||
'description' => __( 'Only selected customer(s) can use the coupon. Leave empty to apply to all 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);
|
$get_coupons = get_posts($args);
|
||||||
|
$global_currencies = get_global_currency_array();
|
||||||
|
|
||||||
$coupons = [];
|
$coupons = [];
|
||||||
if(!empty($get_coupons)){
|
if(!empty($get_coupons)){
|
||||||
@@ -611,50 +612,38 @@ class Coupon {
|
|||||||
|
|
||||||
$active = true;
|
$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');
|
$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){
|
if($active){
|
||||||
$coupons[] = [
|
$coupons[] = [
|
||||||
'ID' => $coupon->ID,
|
'ID' => $coupon->ID,
|
||||||
'code' => get_the_title($coupon->ID),
|
'code' => get_the_title($coupon->ID),
|
||||||
'products' => $product_titles,
|
'value' => $amount_value,
|
||||||
'value' => floatval(formipay_get_post_meta($coupon->ID, 'amount')),
|
|
||||||
'type' => ucfirst(formipay_get_post_meta($coupon->ID, 'type')),
|
'type' => ucfirst(formipay_get_post_meta($coupon->ID, 'type')),
|
||||||
'case_sensitive' => formipay_get_post_meta($coupon->ID, 'case_sensitive'),
|
'case_sensitive' => formipay_get_post_meta($coupon->ID, 'case_sensitive'),
|
||||||
'usages' => [
|
'usages' => [
|
||||||
|
|||||||
@@ -453,7 +453,17 @@ class Customer {
|
|||||||
|
|
||||||
public function autocomplete_options($r, $args) {
|
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;
|
return $r;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user