diff --git a/assets/src/js/setup-wizard/commission/AdminCommission.vue b/assets/src/js/setup-wizard/commission/AdminCommission.vue index 26a2558fbd..9295f94c71 100644 --- a/assets/src/js/setup-wizard/commission/AdminCommission.vue +++ b/assets/src/js/setup-wizard/commission/AdminCommission.vue @@ -61,7 +61,12 @@ this.fixedCommission.fixed = commission.additional_fee ? Number( commission.additional_fee ) : 0; this.fixedCommission.percentage = commission.admin_percentage ? Number( commission.admin_percentage ) : 0; this.selectedCommission = commission.commission_type ? String(commission.commission_type) : 'fixed'; - this.commission = commission.commission_category_based_values; + let commission_category_based_values = commission.commission_category_based_values; + + commission_category_based_values.all = ! commission_category_based_values.all || Array.isArray( commission_category_based_values.all ) ? {} : commission_category_based_values.all; + commission_category_based_values.items = ! commission_category_based_values.items || Array.isArray( commission_category_based_values.items ) ? {} : commission_category_based_values.items; + + this.commission = commission_category_based_values; }, methods: { diff --git a/includes/Admin/Settings.php b/includes/Admin/Settings.php index 8ef59f0d82..2866ef1f42 100644 --- a/includes/Admin/Settings.php +++ b/includes/Admin/Settings.php @@ -538,12 +538,13 @@ public function get_settings_fields() { ], ], 'commission_category_based_values' => [ - 'name' => 'commission_category_based_values', - 'type' => 'category_based_commission', + 'name' => 'commission_category_based_values', + 'type' => 'category_based_commission', 'dokan_pro_commission' => 'yes', - 'label' => __( 'Admin Commission', 'dokan-lite' ), - 'desc' => __( 'Amount you will get from each sale', 'dokan-lite' ), - 'show_if' => [ + 'label' => __( 'Admin Commission', 'dokan-lite' ), + 'desc' => __( 'Amount you will get from each sale', 'dokan-lite' ), + 'required' => 'yes', + 'show_if' => [ 'commission_type' => [ 'equal' => 'category_based', ], diff --git a/includes/Commission.php b/includes/Commission.php index 7e21d1c041..76fe588735 100644 --- a/includes/Commission.php +++ b/includes/Commission.php @@ -582,7 +582,15 @@ public function get_commission( $args = [], $auto_save = false ) { $category_id = $category_id ? $category_id : 0; } - if ( ! empty( $product_id ) && empty( $total_amount ) ) { + /** + * If the $total_amount is empty and $order_item_id is empty then we will calculate the commission based on the product price. + * There is a case where the $total_amount is empty and $order_item_id is empty but the $product_id is not empty + * In this case, we will calculate the commission based on the product price. + * Also there is an issue when 100% coupon is applied see the below link for more details + * + * @see https://github.com/getdokan/dokan/pull/2440#issuecomment-2488159960 + */ + if ( ! empty( $product_id ) && empty( $total_amount ) && empty( $order_item_id ) ) { $product = dokan()->product->get( $product_id ); // If product price is empty the setting the price as 0 diff --git a/includes/Commission/Upugrader/Update_Category_Commission.php b/includes/Commission/Upugrader/Update_Category_Commission.php new file mode 100644 index 0000000000..a10d568b4f --- /dev/null +++ b/includes/Commission/Upugrader/Update_Category_Commission.php @@ -0,0 +1,222 @@ +queue()->add( + self::PROCESS_BATCH_HOOK_CREATOR, + [], + 'dokan_updater_category_processing_creator' + ); + } + + /** + * Batch queue creator. + * + * @since DOKAN_SINCE + * + * @return void + */ + public function process_batch_creator() { + // Get total number of products + $total = $this->category_count(); + + if ( is_wp_error( $total ) || $total === 0 ) { + return; + } + + $total = $total + 50; + $offset = 0; + + do { + $this->schedule_next_batch( $offset ); + + // Calculate next offset + $offset = $offset + self::BATCH_SIZE; + } while ( $offset < $total ); + } + + /** + * Process a batch of categories + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Current page number + * + * @return void + */ + public function process_batch( $offset ) { + // Get categories for this batch + $categories = $this->get_categories_batch( $offset ); + + if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) { + foreach ( $categories as $category ) { + $this->schedule_cat_item( $category->term_id ); + } + } + } + + /** + * Schedule the next batch of categories + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Next page number to process + * + * @return void + */ + protected function schedule_next_batch( $offset ) { + WC()->queue()->add( + self::PROCESS_BATCH_HOOK, + [ $offset ], + 'dokan_updater_category_processing' + ); + } + + /** + * Schedule a category item for processing. + * + * @since DOKAN_SINCE + * + * @param $term + * + * @return void + */ + private function schedule_cat_item( $term ) { + WC()->queue()->add( + self::PROCESS_ITEM_HOOK, + [ $term ], + 'dokan_updater_category_item_processing' + ); + } + + /** + * Get a batch of categories. + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Page number to fetch + * + * @return array Array of term objects + */ + protected function get_categories_batch( $offset ) { + $args = [ + 'taxonomy' => 'product_cat', + 'number' => self::BATCH_SIZE, + 'orderby' => 'name', + 'order' => 'ASC', + 'hide_empty' => false, + 'offset' => $offset, + ]; + + return get_terms( $args ); + } + + /** + * Get the total number of categories + * + * @since DOKAN_SINCE + * + * @return int[]|string|string[]|\WP_Error|\WP_Term[] + */ + protected function category_count() { + return get_terms( + array( + 'taxonomy' => 'product_cat', + 'hide_empty' => false, + 'fields' => 'count', + ) + ); + } + + /** + * Process a single category. + * + * @since DOKAN_PRO_SINCE + * + * @param int $term Category term object + * + * @return void + */ + public function process_single_category( $term_id ) { + $dokan_selling = get_option( 'dokan_selling', [] ); + $category_commission = dokan_get_option( 'commission_category_based_values', 'dokan_selling', [] ); + + $commission_type = get_term_meta( $term_id, 'per_category_admin_commission_type', true ); + $admin_additional_fee = get_term_meta( $term_id, 'per_category_admin_additional_fee', true ); + $commission = get_term_meta( $term_id, 'per_category_admin_commission', true ); + + if ( ! empty( $commission_type ) ) { + $category_commission_item = [ + 'flat' => $admin_additional_fee, + 'percentage' => $commission, + ]; + + if ( Flat::SOURCE === $commission_type ) { + $category_commission_item['percentage'] = 0; + $category_commission_item['flat'] = $commission; + } elseif ( Percentage::SOURCE === $commission_type ) { + $category_commission_item['percentage'] = $commission; + $category_commission_item['flat'] = 0; + } + + $category_commission['items'][ $term_id ] = $category_commission_item; + } + + $dokan_selling['commission_category_based_values'] = $category_commission; + update_option( 'dokan_selling', $dokan_selling ); + } + + /** + * Check if processing is currently running. + * + * @since DOKAN_PRO_SINCE + * + * @return bool + */ + public function is_processing() { + return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null; + } +} diff --git a/includes/Commission/Upugrader/Update_Product_Commission.php b/includes/Commission/Upugrader/Update_Product_Commission.php new file mode 100644 index 0000000000..752e9833f9 --- /dev/null +++ b/includes/Commission/Upugrader/Update_Product_Commission.php @@ -0,0 +1,219 @@ +queue()->add( + self::PROCESS_BATCH_HOOK_CREATOR, + [], + 'dokan_updater_product_processing_creator' + ); + } + + /** + * Batch queue creator. + * + * @since DOKAN_SINCE + * + * @return void + */ + public function process_batch_creator() { + // Get total number of products + $total_products = $this->get_total_products(); + $total_products = $total_products + 50; + + if ( $total_products === 0 ) { + return; + } + + $offset = 0; + + do { + $this->schedule_next_batch( $offset, $total_products ); + + // Calculate next offset + $offset = $offset + self::BATCH_SIZE; + } while ( $offset < $total_products ); + } + + /** + * Process a batch of products + * + * @since DOKAN_PRO_SINCE + * + * @param int $offset Current offset + * @param int $total_products Total number of products + * + * @return void + */ + public function process_batch( $offset, $total_products ) { + // Get products for this batch + $products = $this->get_products_batch( $offset ); + + foreach ( $products as $product ) { + $this->schedule_item( $product->get_id() ); + } + } + + /** + * Schedule the next batch of products + * + * @since DOKAN_PRO_SINCE + * + * @param int $offset Current offset + * @param int $total_products Total number of products + * + * @return void + */ + protected function schedule_next_batch( $offset, $total_products ) { + WC()->queue()->add( + self::PROCESS_BATCH_HOOK, [ + $offset, + $total_products, + ], + 'dokan_updater_product_processing' + ); + } + + /** + * Schedule a single product for processing. + * + * @since DOKAN_SINCE + * + * @param $item + * + * @return void + */ + private function schedule_item( $item ) { + WC()->queue()->add( + self::PROCESS_ITEM_HOOK, + [ $item ], + 'dokan_updater_product_item_processing' + ); + } + + /** + * Get a batch of products + * + * @since DOKAN_PRO_SINCE + * + * @param int $offset Current offset + * + * @return WC_Product[] Array of product objects + */ + protected function get_products_batch( $offset ) { + $args = [ + 'status' => 'publish', + 'limit' => self::BATCH_SIZE, + 'offset' => $offset, + 'orderby' => 'ID', + 'order' => 'ASC', + ]; + + return wc_get_products( $args ); + } + + /** + * Get total number of products + * + * @since DOKAN_PRO_SINCE + * + * @return int + */ + protected function get_total_products() { + $args = [ + 'status' => 'any', + 'limit' => -1, + 'return' => 'ids', + ]; + + $products = wc_get_products( $args ); + + return count( $products ); + } + + /** + * Process a single product + * Customize this method based on what you need to do with each product + * + * @since DOKAN_PRO_SINCE + * + * @param int $product + * + * @return void + */ + public function process_single_product( $product_id ) { + $commission = dokan()->product->get_commission_settings( $product_id ); + + $commission_type_old = $commission->get_type(); + $commission->set_type( Fixed::SOURCE ); + + if ( Flat::SOURCE === $commission_type_old ) { + $commission->set_flat( $commission->get_percentage() ); + $commission->set_percentage( 0 ); + } elseif ( Percentage::SOURCE === $commission_type_old ) { + $commission->set_flat( 0 ); + } + + dokan()->product->save_commission_settings( + $product_id, + [ + 'type' => $commission->get_type(), + 'percentage' => $commission->get_percentage(), + 'flat' => $commission->get_flat(), + ] + ); + } + + /** + * Check if processing is currently running + * + * @since DOKAN_PRO_SINCE + * + * @return bool + */ + public function is_processing() { + return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null; + } +} diff --git a/includes/Commission/Upugrader/Update_Vendor_Commission.php b/includes/Commission/Upugrader/Update_Vendor_Commission.php new file mode 100644 index 0000000000..eaacdc9a00 --- /dev/null +++ b/includes/Commission/Upugrader/Update_Vendor_Commission.php @@ -0,0 +1,203 @@ +queue()->add( + self::PROCESS_BATCH_HOOK_CREATOR, + [], + 'dokan_updater_vendor_processing_creator' + ); + } + + /** + * Batch queue creator. + * + * @since DOKAN_SINCE + * + * @return void + */ + public function process_batch_creator() { + $counts = dokan_get_seller_status_count(); + $total_items = $counts['total']; + $max_pages = ceil( $total_items / self::BATCH_SIZE ); + $max_pages = $max_pages + 5; + + for ( $page = 1; $page <= $max_pages; $page++ ) { + // Schedule the current page for batch processing + WC()->queue()->add( + self::PROCESS_BATCH_HOOK, + [ $page, $max_pages ], + 'dokan_updater_vendor_processing' + ); + } + } + + /** + * Process a batch of vendors + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Current page number + * @param int $max_pages Total number of pages + * + * @return void + */ + public function process_batch( $page_number, $max_pages ) { // phpcs:ignore + $vendors = $this->get_vendors_batch( $page_number ); + + if ( ! empty( $vendors ) ) { + foreach ( $vendors as $vendor ) { + $this->schedule_item( $vendor->get_id() ); + } + } + } + + /** + * Get a batch of vendors + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Page number to fetch + * + * @return \WeDevs\Dokan\Vendor\Vendor[] Array of vendor objects + */ + protected function get_vendors_batch( $page_number ) { + return dokan()->vendor->all( + [ + 'paged' => $page_number, + 'number' => self::BATCH_SIZE, + ] + ); + } + + /** + * Schedule an individual vendor for processing + * + * @since DOKAN_PRO_SINCE + * + * @param int $vendor_id + * + * @return void + */ + private function schedule_item( $vendor_id ) { + WC()->queue()->add( + self::PROCESS_ITEM_HOOK, + [ $vendor_id ], + 'dokan_updater_vendor_item_processing' + ); + } + + /** + * Process a single vendor + * + * @since DOKAN_PRO_SINCE + * + * @param int $vendor_id Vendor ID + * + * @return void + */ + public function process_single_vendor( $vendor_id ) { + try { + $vendor = dokan()->vendor->get( $vendor_id ); + $commission = $vendor->get_commission_settings(); + + $commission_type_old = $commission->get_type(); + $commission->set_type( Fixed::SOURCE ); + + $percentage = $commission->get_percentage(); + + if ( Flat::SOURCE === $commission_type_old ) { + $commission->set_percentage( 0 ); + $commission->set_flat( $percentage ); + } elseif ( Percentage::SOURCE === $commission_type_old ) { + $commission->set_percentage( $percentage ); + $commission->set_flat( 0 ); + } + + $vendor->save_commission_settings( + [ + 'type' => $commission->get_type(), + 'flat' => $commission->get_flat(), + 'percentage' => $commission->get_percentage(), + 'category_commissions' => $commission->get_category_commissions(), + ] + ); + + // Log success + $this->log_vendor_update( $vendor_id, true ); + } catch ( \Exception $e ) { + // Log error + $this->log_vendor_update( $vendor_id, false, $e->getMessage() ); + } + } + + /** + * Log vendor update status + * + * @since DOKAN_PRO_SINCE + * + * @param int $vendor_id + * @param bool $success + * @param string $error_message + * + * @return void + */ + private function log_vendor_update( $vendor_id, $success, $error_message = '' ) { + $log_key = 'dokan_commission_upgrade_vendor_' . $vendor_id; + update_option( + $log_key, [ + 'status' => $success ? 'success' : 'error', + 'error_message' => $error_message, + 'timestamp' => current_time( 'mysql' ), + ] + ); + } + + /** + * Check if processing is currently running + * + * @since DOKAN_PRO_SINCE + * + * @return bool + */ + public function is_processing() { + return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null + || WC()->queue()->get_next( self::PROCESS_ITEM_HOOK ) !== null; + } +} diff --git a/includes/Product/Hooks.php b/includes/Product/Hooks.php index 961e19556d..00cd55d102 100644 --- a/includes/Product/Hooks.php +++ b/includes/Product/Hooks.php @@ -491,7 +491,7 @@ class="woocommerce-help-tip" - + @@ -540,14 +540,18 @@ public static function save_per_product_commission_options( $post_id ) { } if ( isset( $_POST['_per_product_admin_commission'] ) ) { // phpcs:ignore - $admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : sanitize_text_field( $_POST['_per_product_admin_commission'] ); // phpcs:ignore + $_per_product_admin_commission = wc_format_decimal( sanitize_text_field( $_POST['_per_product_admin_commission'] ) ); // phpcs:ignore + + if ( 0 <= $_per_product_admin_commission && 100 >= $_per_product_admin_commission ) { + $admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : $_per_product_admin_commission; // phpcs:ignore + } } if ( isset( $_POST['_per_product_admin_additional_fee'] ) ) { // phpcs:ignore $additional_fee = ( '' === $_POST['_per_product_admin_additional_fee'] ) ? '' : sanitize_text_field( $_POST['_per_product_admin_additional_fee'] ); // phpcs:ignore } - update_post_meta( $post_id, '_per_product_admin_commission', wc_format_decimal( $admin_commission ) ); + update_post_meta( $post_id, '_per_product_admin_commission', $admin_commission ); update_post_meta( $post_id, '_per_product_admin_additional_fee', wc_format_decimal( $additional_fee ) ); } } diff --git a/includes/Upgrade/Hooks.php b/includes/Upgrade/Hooks.php index c7cd4d9aa5..4c9bfc5f84 100644 --- a/includes/Upgrade/Hooks.php +++ b/includes/Upgrade/Hooks.php @@ -2,6 +2,10 @@ namespace WeDevs\Dokan\Upgrade; +use WeDevs\Dokan\Commission\Upugrader\Update_Category_Commission; +use WeDevs\Dokan\Commission\Upugrader\Update_Product_Commission; +use WeDevs\Dokan\Commission\Upugrader\Update_Vendor_Commission; + class Hooks { /** @@ -18,5 +22,13 @@ public function __construct() { add_action( 'wp_ajax_dokan_do_upgrade', [ AdminNotice::class, 'do_upgrade' ] ); add_action( 'dokan_upgrade_is_not_required', [ Upgrades::class, 'update_db_dokan_version' ] ); add_action( 'dokan_upgrade_finished', [ Upgrades::class, 'update_db_dokan_version' ] ); + + $p_scheduler = new Update_Product_Commission(); + $v_scheduler = new Update_Vendor_Commission(); + $c_scheduler = new Update_Category_Commission(); + + $p_scheduler->init_hooks(); + $v_scheduler->init_hooks(); + $c_scheduler->init_hooks(); } } diff --git a/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php b/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php deleted file mode 100644 index bd3238f51f..0000000000 --- a/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php +++ /dev/null @@ -1,130 +0,0 @@ -update_global_settings( $item['data'] ); - } - - if ( isset( $item['task'] ) && 'vendors-commission' === $item['task'] ) { - return $this->update_vendors_settings( $item['data'] ); - } - - if ( isset( $item['task'] ) && 'products-commission' === $item['task'] ) { - return $this->update_products_settings( $item['data'] ); - } - - return false; - } - - /** - * Update global category commissions. - * - * @since DOKAN_SINCE - * - * @param WP_Term[] $terms - * - * @return true - */ - private function update_global_settings( $terms ) { - $dokan_selling = get_option( 'dokan_selling', [] ); - $category_commission = dokan_get_option( 'commission_category_based_values', 'dokan_selling', [] ); - - foreach ( $terms as $term ) { - $commission_type = get_term_meta( $term->term_id, 'per_category_admin_commission_type', true ); - $admin_additional_fee = get_term_meta( $term->term_id, 'per_category_admin_additional_fee', true ); - $commission = get_term_meta( $term->term_id, 'per_category_admin_commission', true ); - - if ( ! empty( $commission_type ) ) { - $category_commission['items'][ $term->term_id ] = [ - 'flat' => $admin_additional_fee, - 'percentage' => $commission, - ]; - } - } - - $dokan_selling['commission_category_based_values'] = $category_commission; - update_option( 'dokan_selling', $dokan_selling ); - - return true; - } - - /** - * Update vendor commission settings. - * - * @since DOKAN_SINCE - * - * @param \WeDevs\Dokan\Vendor\Vendor[] $vendors - * - * @return bool - */ - private function update_vendors_settings( $vendors ) { - foreach ( $vendors as $vendor ) { - $commission = $vendor->get_commission_settings(); - $commission->set_type( Fixed::SOURCE ); - - $vendor->save_commission_settings( - [ - 'percentage' => $commission->get_percentage(), - 'type' => $commission->get_type(), - 'flat' => $commission->get_flat(), - 'category_commissions' => $commission->get_category_commissions(), - ] - ); - } - - return true; - } - - /** - * Update product commission settings. - * - * @since DOKAN_SINCE - * - * @param \WP_Post[] $posts - * - * @return bool - */ - private function update_products_settings( $posts ) { - foreach ( $posts as $post ) { - $commission = dokan()->product->get_commission_settings( $post->ID ); - $commission->set_type( Fixed::SOURCE ); - - dokan()->product->save_commission_settings( - $post->ID, - [ - 'percentage' => $commission->get_percentage(), - 'type' => $commission->get_type(), - 'flat' => $commission->get_flat(), - ] - ); - } - - return true; - } -} diff --git a/includes/Upgrade/Upgrades/V_3_14_0.php b/includes/Upgrade/Upgrades/V_3_14_0.php index bdaefbc903..339b67187c 100644 --- a/includes/Upgrade/Upgrades/V_3_14_0.php +++ b/includes/Upgrade/Upgrades/V_3_14_0.php @@ -4,7 +4,11 @@ use WeDevs\Dokan\Abstracts\DokanUpgrader; use WeDevs\Dokan\Commission\Formula\Fixed; -use WeDevs\Dokan\Upgrade\Upgrades\BackgroundProcesses\V_3_14_0_UpdateCommissions; +use WeDevs\Dokan\Commission\Formula\Flat; +use WeDevs\Dokan\Commission\Formula\Percentage; +use WeDevs\Dokan\Commission\Upugrader\Update_Category_Commission; +use WeDevs\Dokan\Commission\Upugrader\Update_Product_Commission; +use WeDevs\Dokan\Commission\Upugrader\Update_Vendor_Commission; class V_3_14_0 extends DokanUpgrader { @@ -16,125 +20,46 @@ class V_3_14_0 extends DokanUpgrader { * @return void */ public static function update_global_commission_type() { - $options = get_option( 'dokan_selling', [] ); - $commission_type = isset( $options['commission_type'] ) ? $options['commission_type'] : Fixed::SOURCE; + $options = get_option( 'dokan_selling', [] ); + + $commission_type = isset( $options['commission_type'] ) ? $options['commission_type'] : Fixed::SOURCE; + $admin_percentage = isset( $options['admin_percentage'] ) ? $options['admin_percentage'] : 0; if ( in_array( $commission_type, array_keys( dokan()->commission->get_legacy_commission_types() ), true ) ) { $options['commission_type'] = Fixed::SOURCE; - update_option( 'dokan_selling', $options ); - } - } - /** - * Update global category commisions. - * - * @since DOKAN_SINCE - * - * @return void - */ - public static function update_global_category_commission_types() { - $has_categories = true; - $page_number = 1; - $processor = new V_3_14_0_UpdateCommissions(); - - while ( $has_categories ) { - $args = [ - 'taxonomy' => 'product_cat', - 'number' => 20, - 'orderby' => 'name', - 'order' => 'ASC', - 'hide_empty' => false, - 'offset' => ( $page_number - 1 ) * 20, - ]; - - $terms = get_terms( $args ); - - if ( empty( $terms ) ) { - $has_categories = false; - } else { - $args = [ - 'data' => $terms, - 'task' => 'global-commission', - ]; - $processor->push_to_queue( $args ); + if ( Flat::SOURCE === $commission_type ) { + $options['admin_percentage'] = 0; + $options['additional_fee'] = $admin_percentage; + } elseif ( Percentage::SOURCE === $commission_type ) { + $options['admin_percentage'] = $admin_percentage; + $options['additional_fee'] = 0; } - - ++$page_number; + update_option( 'dokan_selling', $options ); } - - $processor->dispatch_process(); } /** - * Update vendor comission settings. + * Update vendor and product comission settings. * * @since DOKAN_SINCE * * @return void */ - public static function update_category_commission_of_vendors() { - $page_number = 1; - $has_vendors = true; - $processor = new V_3_14_0_UpdateCommissions(); - - while ( $has_vendors ) { - $vendors = dokan()->vendor->all( - [ - 'paged' => $page_number, - ] - ); - - if ( empty( $vendors ) ) { - $has_vendors = false; - } else { - $args = [ - 'data' => $vendors, - 'task' => 'vendors-commission', - ]; - $processor->push_to_queue( $args ); - } - - ++$page_number; + public static function update_commission() { + $product_scheduler = new Update_Product_Commission(); + if ( ! $product_scheduler->is_processing() ) { + $product_scheduler->start_processing(); } - $processor->dispatch_process(); - } - - /** - * Update product commission settings. - * - * @since DOKAN_SINCE - * - * @return void - */ - public static function update_category_commission_of_products() { - $page_number = 1; - $has_products = true; - $processor = new V_3_14_0_UpdateCommissions(); - - while ( $has_products ) { - $products = dokan()->product->all( - [ - 'posts_per_page' => 10, - 'paged' => $page_number, - ] - ); - - $products = $products->posts; - - if ( empty( $products ) ) { - $has_products = false; - } else { - $args = [ - 'data' => $products, - 'task' => 'products-commission', - ]; - $processor->push_to_queue( $args ); - } - - ++$page_number; + $vendor_scheduler = new Update_Vendor_Commission(); + if ( ! $vendor_scheduler->is_processing() ) { + $vendor_scheduler->start_processing(); } - $processor->dispatch_process(); + $category_scheduler = new Update_Category_Commission(); + if ( ! $category_scheduler->is_processing() ) { + $category_scheduler->start_processing(); + } } } diff --git a/src/admin/components/CombineInput.vue b/src/admin/components/CombineInput.vue index 3f44f660cb..f4951985ab 100644 --- a/src/admin/components/CombineInput.vue +++ b/src/admin/components/CombineInput.vue @@ -81,8 +81,6 @@ import Debounce from "debounce"; let newPercentage = this.validatePercentage( newVal.percentage ); let oldPercentage = this.validatePercentage( oldVal.percentage ); - console.log(newPercentage, oldPercentage); - if ( ! newPercentage || '' === newPercentage || Number( newPercentage ) < 0 || Number( newPercentage ) > 100 ) { newPercentage = oldPercentage; } diff --git a/src/admin/components/Commission/CategoryBasedCommission.vue b/src/admin/components/Commission/CategoryBasedCommission.vue index 754e0eaf35..c380948270 100644 --- a/src/admin/components/Commission/CategoryBasedCommission.vue +++ b/src/admin/components/Commission/CategoryBasedCommission.vue @@ -19,7 +19,7 @@

{{__( 'All Categories', 'dokan-lite' )}}

@@ -32,7 +32,7 @@ id="percentage_commission" name="percentage_commission" ref='percentage' - :value="commission.all.percentage" + :value="formatValue( commission.all.percentage )" v-on:input="e => handleAllCategoryInput(e.target.value, 'percentage', commission.all.percentage )" style="border: none !important;" /> @@ -49,7 +49,7 @@ id="fixed_commission" name="fixed_commission" ref='fixed' - :value="commission.all.flat" + :value="formatValue( commission.all.flat )" v-on:input="e => handleAllCategoryInput(e.target.value, 'flat', commission.all.flat )" style="border: none !important;" /> @@ -66,7 +66,7 @@

- {{ item.name }} + #{{ item.term_id }}

@@ -79,7 +79,7 @@ id="percentage_commission" name="percentage_commission" ref='percentage' - :value="getCommissionValue( 'percentage', item.term_id )" + :value="formatValue( getCommissionValue( 'percentage', item.term_id ) )" v-on:input="e => commissinItemHandler( e.target.value, 'percentage', item.term_id, getCommissionValue( 'percentage', item.term_id ) )" style="border: none !important;" /> @@ -96,8 +96,8 @@ id="fixed_commission" name="fixed_commission" ref='flat' - :value="getCommissionValue( 'flat', item.term_id )" - v-on:input="e => commissinItemHandler( e.target.value, 'flat', item.term_id, getCommissionValue( 'percentage', item.term_id ) )" + :value="formatValue( getCommissionValue( 'flat', item.term_id ) )" + v-on:input="e => commissinItemHandler( e.target.value, 'flat', item.term_id, getCommissionValue( 'flat', item.term_id ) )" style="border: none !important;" /> @@ -108,6 +108,8 @@ diff --git a/src/admin/components/Fields.vue b/src/admin/components/Fields.vue index 4210fab88d..bb9a9ec9ef 100644 --- a/src/admin/components/Fields.vue +++ b/src/admin/components/Fields.vue @@ -108,10 +108,10 @@ /> -

+

{{ getError( fieldData.label ) }}

-

+

{{ getValidationErrorMessage( fieldData.name ) }}

diff --git a/src/admin/pages/Settings.vue b/src/admin/pages/Settings.vue index e58739efc1..522f88d425 100644 --- a/src/admin/pages/Settings.vue +++ b/src/admin/pages/Settings.vue @@ -433,13 +433,36 @@ if ( ! this.errors.includes( field ) ) { this.errors.push( field ); - // If flat or percentage commission is set. Remove the required field. - if ( 'flat' === value['commission_type'] || 'percentage' === value['commission_type'] ) { + if ( 'flat' === value['commission_type'] || 'percentage' === value['commission_type'] || 'combine' === value['commission_type'] || 'fixed' === value['commission_type'] ) { + this.errors = this.arrayRemove( this.errors, 'commission_category_based_values' ); + } + + if ( 'category_based' === value['commission_type'] ) { this.errors = this.arrayRemove( this.errors, 'admin_percentage' ); this.errors = this.arrayRemove( this.errors, 'additional_fee' ); } } } + + if ( field in value && 'category_based' === value['commission_type'] ) { + let alreadyAdded = ! this.errors.includes( field ); + + // Validate the commission_category_based_values + if ( + 'commission_category_based_values' in value && + typeof value['commission_category_based_values'] === 'object' && + value?.commission_category_based_values?.all && + value?.commission_category_based_values?.all?.flat && + String( value?.commission_category_based_values?.all?.flat ).length > 0 && + value?.commission_category_based_values?.all?.percentage && + String( value?.commission_category_based_values?.all?.percentage ).length > 0 && + alreadyAdded + ) { + this.errors = this.arrayRemove( this.errors, 'commission_category_based_values' ); + } else { + this.errors.push( 'commission_category_based_values' ); + } + } } ); } ); @@ -1096,7 +1119,7 @@ } .metabox-holder { - width: 40%; + width: 100%; .settings-header { display: block; @@ -1129,7 +1152,7 @@ @media only screen and (max-width: 768px) { .dokan-settings-wrap { .nav-tab-wrapper { - width: 35% !important; + width: 35%; .nav-tab { .nav-content { @@ -1145,7 +1168,7 @@ } .metabox-holder { - width: 65%; + width: 100%; .settings-header { .settings-content { diff --git a/src/admin/pages/VendorSingle.vue b/src/admin/pages/VendorSingle.vue index 2295d5f37f..fb78b5d75d 100644 --- a/src/admin/pages/VendorSingle.vue +++ b/src/admin/pages/VendorSingle.vue @@ -574,22 +574,7 @@ export default { } if ( ! response.admin_commission_type ) { - this.store.admin_commission_type = 'flat'; - } - - if ( this.store.admin_additional_fee ) { - this.store.admin_additional_fee = accounting.formatNumber( this.store.admin_additional_fee, dokan.currency.precision, dokan.currency.thousand, dokan.currency.decimal, dokan.currency.format ); - } - - /** - * if admin commission type is flat and no admin commission is set then it will not not set - * - * if admin commission type is flat and admin commission is set to 0 then it will show 0 - * - * blank string is for not set - */ - if (this.store.admin_commission_type === 'flat' && this.store.admin_commission !== '') { - this.store.admin_commission = accounting.formatNumber( this.store.admin_commission, dokan.currency.precision, dokan.currency.thousand, dokan.currency.decimal, dokan.currency.format ); + this.store.admin_commission_type = ''; } },