__('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' ), 'products' => esc_html__( 'Product Relation', '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) { // Group : Rules $rules_group = array( 'active' => array( 'type' => 'checkbox', 'label' => __( 'Active this coupon', 'formipay' ), 'value' => true ), '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' ) ), 'type' => array( 'type' => 'radio', 'label' => __( 'Type', 'formipay' ), 'options' => array( 'fixed' => __( 'Fixed', 'formipay' ), 'percentage' => __( 'Percentage', 'formipay' ) ) ), 'amount' => array( 'type' => 'number', 'label' => __( 'Amount', 'formipay' ), 'description' => sprintf('' . __( 'Be carefully, this amount is not regarding the currency.', '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 = 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' => __( 'These forms can use the coupon', 'formipay' ), 'description' => __( 'Leave it empty to enable all forms to use this coupon. Please be carefully, this coupon amount is not regarding the currency of the selected form.', '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'){ ?>
__( 'Coupon code is not valid', 'formipay' ) ] ); } wp_send_json_success(); } public function order_details($details, $form_id, $order_data) { if( isset($order_data['coupon_code']) && !empty($order_data['coupon_code']) ) { $coupon_id = formipay_get_coupon_id_by_code( sanitize_text_field( $order_data['coupon_code'] ), $form_id ); if(false !== $coupon_id) { $amount = floatval( formipay_get_post_meta( $coupon_id, 'amount' ) ); $discount = $amount; $coupon_type = formipay_get_post_meta( $coupon_id, 'type' ); $qty = 1; if( formipay_get_post_meta( $form_id, 'product_quantity_toggle' ) == 'on' ){ $qty = intval($order_data['qty']); } if( $coupon_type == 'percentage' ){ $product_price = floatval( formipay_get_post_meta( $form_id, 'product_price' ) ); $amount = $amount / 100; $discount = $product_price * $amount; } $amount = $discount * $qty; $amount = $amount * -1; $details[] = [ 'item' => esc_html( $order_data['coupon_code'] ), 'amount' => floatval($amount), 'subtotal' => floatval($amount), 'context' => 'sub' ]; } } return $details; } public function count_order_by_coupon_code($coupon_code) { global $wpdb; $table = $wpdb->prefix .'formipay_orders'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $data = $wpdb->get_results( $wpdb->prepare( "SELECT COUNT(id) as count FROM %i WHERE `items` LIKE %s", $table, '%' . $wpdb->esc_like($coupon_code) . '%' ) ); return $data[0]->count; } public function add_discount_item($breakdown, $currency, $order_data) { if( isset($order_data['form_data']['coupon_code']) && !empty($order_data['form_data']['coupon_code']) ) { $form_id = $order_data['form_id']; $coupon_id = formipay_get_coupon_id_by_code( sanitize_text_field( $order_data['form_data']['coupon_code'] ), $form_id ); if(false !== $coupon_id) { $amount = floatval( formipay_get_post_meta( $coupon_id, 'amount' ) ); $discount = $amount; $coupon_type = formipay_get_post_meta( $coupon_id, 'type' ); $qty = 1; if( formipay_get_post_meta( $form_id, 'product_quantity_toggle' ) == 'on' ){ $qty = intval($order_data['form_data']['qty']); } if( $coupon_type == 'percentage' ){ $product_price = floatval( formipay_get_post_meta( $form_id, 'product_price' ) ); $amount = $amount / 100; $discount = $product_price * $amount; } $breakdown['item_total']['value'] += $discount; $breakdown['discount'] = array( "currency_code" => $currency, "value" => $discount ); } } return $breakdown; } public function formipay_tabledata_coupons() { check_ajax_referer( 'formipay-admin-coupon-page', '_wpnonce' ); $args = [ 'post_type' => 'formipay-coupon', 'posts_per_page' => -1, 'post_status' => array( 'pending', 'draft', 'publish' ) ]; $get_total_coupons = get_posts($args); $coupon_status = [ 'all' => 0, 'active' => 0, 'inactive' => 0 ]; if(!empty($get_total_coupons)){ foreach($get_total_coupons as $coupon){ $status = (formipay_get_post_meta($coupon->ID, 'active') == 'on') ? 'active' : 'inactive'; $coupon_status['all'] = intval($coupon_status['all']) + 1; if($status == 'active'){ $coupon_status['active'] = intval($coupon_status['active']) + 1; }elseif($status == 'inactive'){ $coupon_status['inactive'] = intval($coupon_status['inactive']) + 1; } } } if(isset($_REQUEST['limit'])){ $args['posts_per_page'] = intval($_REQUEST['limit']); } if(isset($_REQUEST['offset'])){ $args['offset'] = intval($_REQUEST['offset']); } if(!empty($_REQUEST['search'])){ $args['s'] = sanitize_text_field( wp_unslash($_REQUEST['search']) ); } $get_coupons = get_posts($args); $coupons = []; if(!empty($get_coupons)){ foreach($get_coupons as $key => $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'); if($active){ $coupons[] = [ 'ID' => $coupon->ID, 'code' => get_the_title($coupon->ID), 'products' => $product_titles, 'value' => floatval(formipay_get_post_meta($coupon->ID, 'amount')), 'type' => ucfirst(formipay_get_post_meta($coupon->ID, 'type')), 'case_sensitive' => formipay_get_post_meta($coupon->ID, 'case_sensitive'), 'usages' => [ 'used' => $this->count_order_by_coupon_code(get_the_title($coupon->ID)), 'limit' => (intval(formipay_get_post_meta($coupon->ID, 'use_limit')) > 0) ? formipay_get_post_meta($coupon->ID, 'use_limit') : '∞' ], 'date_limit' => false !== $date_limit ? formipay_date('Y-m-d', intval(formipay_get_post_meta($coupon->ID, 'date_limit')) / 1000) : 'none', 'status' => (formipay_get_post_meta($coupon->ID, 'active') == 'on') ? 'Active' : 'Inactive' ]; } } } $response = [ 'results' => $coupons, 'total' => count($get_total_coupons), // Total number of forms 'posts_report' => array_map('intval', (array)$coupon_status) ]; wp_send_json($response); } public function formipay_coupon_get_products() { check_ajax_referer( 'formipay-admin-coupon-page', '_wpnonce' ); if (!isset($_POST['search']) || empty($_POST['search'])) { wp_send_json_error( __('No search term provided.', 'formipay') ); } $search_term = sanitize_text_field( wp_unslash($_POST['search']) ); $query = get_posts([ 'post_type' => 'formipay-form', 's' => $search_term, 'posts_per_page' => -1, ]); $results = []; if(!empty($query)){ foreach($query as $post){ $results[] = [ 'value' => $post->ID, 'label' => $post->post_title, ]; } } wp_send_json($results); } public function formipay_create_coupon_post() { check_ajax_referer( 'formipay-admin-coupon-page', '_wpnonce' ); $code = isset($_REQUEST['title']) ? sanitize_text_field( wp_unslash($_REQUEST['title']) ) : ''; if( !empty($code) && '' !== $code ){ $post_id = wp_insert_post( [ 'post_title' => $code, 'post_type' => 'formipay-coupon', ], true ); if(is_wp_error($post_id)){ wp_send_json_error( [ 'message' => $post_id->get_error_message() ] ); } update_post_meta($post_id, 'type', 'percentage'); update_post_meta($post_id, 'amount', 0); wp_send_json_success( [ 'edit_post_url' => admin_url('post.php?post='.$post_id.'&action=edit') ] ); } wp_send_json_error( [ 'message' => esc_html__( 'Coupon code is empty.', 'formipay' ) ] ); } public function formipay_delete_coupon() { check_ajax_referer( 'formipay-admin-coupon-page', '_wpnonce' ); $post_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0; $delete = wp_delete_post($post_id, true); if(is_wp_error( $delete )){ wp_send_json_error( [ 'title' => esc_html__( 'Failed', 'formipay' ), 'message' => esc_html__( 'Failed to delete coupon. Please try again.', 'formipay' ), 'icon' => 'error' ] ); } wp_send_json_success( [ 'title' => esc_html__( 'Deleted', 'formipay' ), 'message' => esc_html__( 'Coupon is deleted permanently.', 'formipay' ), 'icon' => 'success' ] ); } public function formipay_bulk_delete_coupon() { check_ajax_referer( 'formipay-admin-coupon-page', '_wpnonce' ); if( empty($_REQUEST['ids']) ){ wp_send_json_error( [ 'title' => esc_html__( 'Failed', 'formipay' ), 'message' => esc_html__( 'There is no coupon selected. Please try again.', 'formipay' ), 'icon' => 'error' ] ); } $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : []; $success = 0; $failed = 0; $report = __( 'Done.', 'formipay' ); if(!empty($ids)){ foreach($ids as $id){ $delete = wp_delete_post($id, true); if(is_wp_error( $delete )){ $failed++; }else{ $success++; } } } if($success > 0){ $report .= sprintf( __( ' Deleted %d coupon(s).', 'formipay' ), $success); } if($failed > 0){ $report .= sprintf( __( ' Failed %d coupon(s).', 'formipay' ), $failed); } wp_send_json_success( [ 'title' => esc_html__( 'Done!', 'formipay' ), 'message' => $report, 'icon' => 'info' ] ); } public function formipay_duplicate_coupon() { check_ajax_referer( 'formipay-admin-coupon-page', '_wpnonce' ); $post_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0; $post = get_post($post_id); if (!$post) { wp_send_json_error( [ 'title' => esc_html__('Failed', 'formipay'), 'message' => esc_html__( 'Coupon is not defined.', 'formipay' ), 'icon' => 'error' ] ); } if (!in_array($post->post_type, ['formipay-coupon'])) { wp_send_json_error( [ 'title' => esc_html__('Failed', 'formipay'), 'message' => esc_html__( 'Wrong Post Type.', 'formipay' ), 'icon' => 'error' ] ); } $duplicate_post = [ 'post_title' => $post->post_title . ' ' . __('Copy', 'formipay'), 'post_content' => $post->post_content, 'post_status' => 'draft', // Set sebagai draft 'post_type' => $post->post_type, 'post_author' => $post->post_author, ]; // Masukkan duplikat ke database $new_post_id = wp_insert_post($duplicate_post); if (is_wp_error($new_post_id)) { wp_send_json_error( [ 'title' => esc_html__('Failed', 'formipay'), 'message' => esc_html__( 'Something happened. Please try again.', 'formipay' ), 'icon' => 'error' ] ); } // Salin semua meta data $meta_data = get_post_meta($post_id); foreach ($meta_data as $key => $values) { if($key !== 'forms'){ foreach ($values as $value) { add_post_meta($new_post_id, $key, maybe_unserialize($value)); } } } wp_send_json_success( [ 'title' => esc_html__('Duplicated!', 'formipay'), 'message' => esc_html__( 'Coupon is successfully duplicated.', 'formipay' ), 'icon' => 'success' ] ); } }