__('Coupons', 'formipay'),
'singular_name' => __('Coupon', 'formipay'),
// 'menu_name' => 'Formipay',
'add_new' => __('Add New', 'formipay'),
'add_new_item' => __('Add New Coupon', 'formipay'),
'edit' => __('Edit', 'formipay'),
'edit_item' => __('Edit Coupon', 'formipay'),
'new_item' => __('New Coupon', 'formipay'),
'view' => __('View', 'formipay'),
'view_item' => __('View Coupon', 'formipay'),
'search_items' => __('Search Coupon', 'formipay'),
'not_found' => __('No coupons found', 'formipay'),
'not_found_in_trash' => __('No coupons found in trash', 'formipay'),
'parent' => __('Coupon Parent', 'formipay')
);
$args = array(
'label' => 'Coupons',
'description' => __('Coupon for marketing campaign', 'formipay'),
'labels' => $labels,
'public' => false,
'supports' => array( 'title' ),
'hierarchical' => false,
'has_archive' => false,
'show_ui' => true,
'show_in_menu' => false,
'show_in_rest' => false,
'query_var' => true
);
register_post_type( 'formipay-coupon', $args );
}
public function add_submenu() {
add_submenu_page(
'formipay',
__( 'Coupons', 'formipay' ),
__( 'Coupons', 'formipay' ),
'manage_options',
'formipay-coupons',
[$this, 'formipay_coupon'],
);
}
public function formipay_coupon() {
include_once FORMIPAY_PATH . 'admin/page-coupons.php';
}
public function enqueue_admin() {
global $current_screen;
if($current_screen->id == 'formipay_page_formipay-coupons') {
wp_enqueue_style( 'page-coupons', FORMIPAY_URL . 'admin/assets/css/page-coupons.css', [], FORMIPAY_VERSION, 'all' );
wp_enqueue_script( 'page-coupons', FORMIPAY_URL . 'admin/assets/js/page-coupons.js', ['jquery', 'gridjs'], FORMIPAY_VERSION, true );
wp_localize_script( 'page-coupons', 'formipay_coupons_page', [
'ajax_url' => admin_url('admin-ajax.php'),
'site_url' => site_url(),
'columns' => [
'id' => esc_html__( 'ID', 'formipay' ),
'code' => esc_html__( 'Coupon Code', 'formipay' ),
'usages' => esc_html__( 'Usages', 'formipay' ),
'date_limit' => esc_html__( 'Date Limit', 'formipay' ),
'status' => esc_html__( 'Status', 'formipay' ),
'type' => esc_html__( 'Type', 'formipay' ),
'amount' => esc_html__( 'Amount', 'formipay' )
],
'filter_form' => [
'products' => [
'placeholder' => esc_html__( 'Filter by Product', 'formipay' ),
'noresult_text' => esc_html__( 'No results found', 'formipay' )
]
],
'modal' => [
'add' => [
'title' => esc_html__( 'Your New Coupon Code', 'formipay' ),
'validation' => esc_html__( 'Coupon code is still empty. Please input the code before continue', 'formipay' ),
'cancelButton' => esc_html__( 'Cancel', 'formipay' ),
'confirmButton' => esc_html__( 'Create New Coupon', 'formipay' )
],
'delete' => [
'question' => esc_html__( 'Do you want to delete the coupon?', 'formipay' ),
'cancelButton' => esc_html__( 'Cancel', 'formipay' ),
'confirmButton' => esc_html__( 'Delete Permanently', 'formipay' )
],
'bulk_delete' => [
'question' => esc_html__( 'Do you want to delete the selected the coupon(s)?', 'formipay' ),
'cancelButton' => esc_html__( 'Cancel', 'formipay' ),
'confirmButton' => esc_html__( 'Confirm', 'formipay' )
],
'duplicate' => [
'question' => esc_html__( 'Do you want to duplicate the coupon?', 'formipay' ),
'cancelButton' => esc_html__( 'Cancel', 'formipay' ),
'confirmButton' => esc_html__( 'Confirm', 'formipay' )
],
],
'nonce' => wp_create_nonce('formipay-admin-coupon-page')
] );
}
$screen = get_current_screen();
if ( $screen->post_type === 'formipay-coupon' && $screen->base === 'post' ) {
wp_enqueue_style( 'sweetalert2', FORMIPAY_URL . 'vendor/SweetAlert2/sweetalert2.min.css', [], '11.14.4', 'all');
wp_enqueue_script( 'sweetalert2', FORMIPAY_URL . 'vendor/SweetAlert2/sweetalert2.min.js', ['jquery'], '11.14.4', true);
wp_localize_script( 'sweetalert2', 'formipay_admin', [
'ajax_url' => admin_url('admin-ajax.php'),
'site_url' => site_url(),
] );
}
}
public function cpt_post_fields_box($boxes) {
$boxes['formipay_coupon_settings'] = array(
'post_type' => array('formipay-coupon'),
'label' => __('Details', 'formipay'),
);
return $boxes;
}
public function cpt_post_fields_content($fields) {
$fields['formipay_coupon_settings'] = array();
$fields = apply_filters( 'formipay/coupon-config', $fields );
return $fields;
}
public function rule_config($fields) {
$multicurrency = formipay_is_multi_currency_active();
$all_currencies = formipay_currency_as_options();
$global_selected_currencies = formipay_global_currency_options();
$global_currencies = formipay_global_currency_options('raw');
$default_currency = formipay_default_currency();
// Group : Rules
$rules_group_1 = array(
'rules_general_group' => array(
'type' => 'group_title',
'group' => 'started'
),
'active' => array(
'type' => 'checkbox',
'label' => __( 'Active this coupon', 'formipay' ),
'value' => true,
),
'type' => array(
'type' => 'radio',
'label' => __( 'Type', 'formipay' ),
'required' => true,
'options' => array(
'fixed' => __( 'Fixed', 'formipay' ),
'percentage' => __( 'Percentage', 'formipay' )
),
),
'amount_percentage' => array(
'type' => 'number',
'label' => __( 'Amount', 'formipay' ),
'required' => true,
'dependency' => array(
'key' => 'type',
'value' => 'percentage'
),
'group' => 'ended'
)
);
$rules_group_2 = [];
$global_currencies = get_global_currency_array();
$default_currency = formipay_default_currency();
$rules_group_2['discount_amount_wrapper'] = array(
'type' => 'group_title',
'label' => __( 'Discount Amount', 'formipay' ),
'dependency' => array(
'key' => 'type',
'value' => 'fixed'
),
'group' => 'started',
);
$rules_group_max['discount_max_amount_wrapper'] = array(
'type' => 'group_title',
'label' => __( 'Max Discount Amount', 'formipay' ),
'description' => __( 'Leave empty to not limit the max discount amount.', 'formipay' ),
'group' => 'started',
);
foreach($global_currencies as $currency){
$default_currency_symbol = formipay_get_currency_data_by_value($default_currency, 'symbol');
$currency_symbol = formipay_get_currency_data_by_value($currency['currency'], 'symbol');
$currency_title = ucwords(formipay_get_currency_data_by_value($currency['currency'], 'title'));
$decimal_digits = intval($currency['decimal_digits']);
$step = $decimal_digits * 10;
$step = $step > 0 ? 1 / $step : 1;
$rules_group_2['amount_fixed_'.$currency_symbol] = array(
'type' => 'number',
'label' => sprintf(
__( ' Amount in %s', 'formipay' ),
formipay_get_flag_by_currency($currency['currency']),
$currency_symbol
),
'step' => $step,
'min' => 0,
'required' => true,
'placeholder' => __( 'Enter Amount...', 'formipay' ),
'dependency' => array(
'key' => 'type',
'value' => 'fixed'
)
);
$rules_group_max['max_amount_'.$currency_symbol] = array(
'type' => 'number',
'label' => sprintf(
__( '
Max Amount in %s', 'formipay' ),
formipay_get_flag_by_currency($currency['currency']),
$currency_symbol
),
'step' => $step,
'min' => 0,
'placeholder' => __( 'Enter Max Amount...', 'formipay' )
);
}
$last_rules_group_2 = array_key_last($rules_group_2);
$rules_group_2[$last_rules_group_2]['group'] = 'ended';
$last_rules_group_max = array_key_last($rules_group_max);
$rules_group_max[$last_rules_group_max]['group'] = 'ended';
$rules_group_3 = array(
'rules_group' => array(
'type' => 'group_title',
'label' => __( 'Rules', 'formipay' ),
'description' => __( 'Define the rules of this coupon.', 'formipay' ),
'group' => 'started'
),
'case_sensitive' => array(
'type' => 'checkbox',
'label' => __( 'Case Sensitive', 'formipay' ),
'description' => __( 'If this activated, coupon codes must be entered with the exact capitalization as specified.
For example code SAVE10:
- SAVE10 is valid
- save10 is invalid
- Save10 is invalid.', 'formipay' )
),
'free_shipping' => array(
'type' => 'checkbox',
'label' => __( 'Free Shipping', 'formipay' ),
'description' => __( 'Shipping cost will be free when this coupon applied.', 'formipay' )
),
'quantity_active' => array(
'type' => 'checkbox',
'label' => __( 'Influenced by Quantity', 'formipay' ),
'description' => __( 'Example: when buyer buy 4 pcs of item, 4 × discount amount will be implemented.', 'formipay' ),
'dependency' => array(
'key' => 'type',
'value' => 'fixed'
)
),
// 'max_discount' => array(
// 'type' => 'number',
// 'label' => __( 'Max Discount Amount', 'formipay' ),
// 'description' => __( 'Leave it empty to not limit the max discount amount.', 'formipay' )
// ),
);
$rules_group = $rules_group_1 + $rules_group_2 + $rules_group_max + $rules_group_3;
$rules_group = apply_filters( 'formipay/coupon-settings/tab:rules/group:rules', $rules_group );
$last_rules_group = array_key_last($rules_group);
$rules_group[$last_rules_group]['group'] = 'ended';
$all_group_merged = $rules_group;
$settings_all_fields = apply_filters( 'formipay/coupon-settings/tab:rules', $all_group_merged );
$settings_fields = [];
if(!empty($settings_all_fields)){
foreach($settings_all_fields as $key => $value){
$settings_fields[$key] = $value;
}
}
$fields['formipay_coupon_settings']['rules'] = array(
'name' => __('Rules', 'formipay'),
'fields' => $settings_fields
);
return $fields;
}
public function restriction_config($fields) {
// Group : Restrictions
$restriction_group = array(
'restriction_group' => array(
'type' => 'group_title',
'label' => __( 'Restrictions', 'formipay' ),
'group' => 'started'
),
'use_limit' => array(
'type' => 'number',
'label' => __( 'Usage Limit', 'formipay' ),
'description' => __( 'Leave it empty or 0 (zero) to set it as unlimited usage.', 'formipay' )
),
'date_limit' => array(
'type' => 'date',
'label' => __( 'Date Limit', 'formipay' ),
'description' => __( 'Chosen date is the last day the coupon can be used. Leave it empty tio set it as no-limit date.', 'formipay' )
),
'forms' => array(
'type' => 'autocomplete',
'post_type' => array('formipay-form'),
'label' => __( 'Forms', 'formipay' ),
'description' => __( 'Only selected form(s) can use the coupon. Leave empty to apply to all forms.', 'formipay' )
),
'products' => array(
'type' => 'autocomplete',
'post_type' => array('formipay-product'),
'label' => __( 'Products', 'formipay' ),
'description' => __( 'Only selected product(s) can use the coupon. Leave empty to apply to all products.', 'formipay' )
),
'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' )
)
);
$restriction_group = apply_filters( 'formipay/coupon-settings/tab:restriction/group:restrictions', $restriction_group );
$last_restriction_group = array_key_last($restriction_group);
$restriction_group[$last_restriction_group]['group'] = 'ended';
$details_all_fields = apply_filters( 'formipay/coupon-settings/tab:restriction', $restriction_group );
$details_fields = [];
if(!empty($details_all_fields)){
foreach($details_all_fields as $key => $value){
$details_fields[$key] = $value;
}
}
$fields['formipay_coupon_settings']['restriction'] = array(
'name' => __('Restrictions', 'formipay'),
'fields' => $details_fields
);
return $fields;
}
public function add_static_element($elements) {
$elements[] = [
'id' => 'coupon_field',
'label' => __( 'Coupon Field', 'formipay' )
];
return $elements;
}
public function render_static_element($element_id) {
if($element_id == 'coupon_field'){
?>