{{__( 'All Categories', 'dokan-lite' )}}
- {{ item.name }} + #{{ item.term_id }}
+
{{ getError( fieldData.label ) }}
-+
{{ getValidationErrorMessage( fieldData.name ) }}
From dcb8376f8f5d814267834e54b4942de411a1fb02 Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Wed, 20 Nov 2024 18:01:27 +0600 Subject: [PATCH 09/14] Fix commission calculation when 100% coupon is applied. --- includes/Commission.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 From f26fd566c75a771a2041fff58ca6c5b00ba8fb3a Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:50:50 +0600 Subject: [PATCH 10/14] Fix commission upgrade --- .../V_3_14_0_UpdateCommissions.php | 53 ++++++++++++++++--- includes/Upgrade/Upgrades/V_3_14_0.php | 16 +++++- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php b/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php index bd3238f51f..ce5bfebe45 100644 --- a/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php +++ b/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php @@ -4,6 +4,8 @@ use WeDevs\Dokan\Abstracts\DokanBackgroundProcesses; use WeDevs\Dokan\Commission\Formula\Fixed; +use WeDevs\Dokan\Commission\Formula\Flat; +use WeDevs\Dokan\Commission\Formula\Percentage; use WP_Term; /** @@ -61,10 +63,20 @@ private function update_global_settings( $terms ) { $commission = get_term_meta( $term->term_id, 'per_category_admin_commission', true ); if ( ! empty( $commission_type ) ) { - $category_commission['items'][ $term->term_id ] = [ + $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->term_id ] = $category_commission_item; } } @@ -85,14 +97,26 @@ private function update_global_settings( $terms ) { */ private function update_vendors_settings( $vendors ) { foreach ( $vendors as $vendor ) { - $commission = $vendor->get_commission_settings(); + $commission = $vendor->get_commission_settings(); + $commission_type_old = $commission->get_type(); $commission->set_type( Fixed::SOURCE ); + $commission_type = $commission->get_type(); + $flat = $commission->get_flat(); + $percentage = $commission->get_percentage(); + + if ( Flat::SOURCE === $commission_type_old ) { + $percentage = 0; + $flat = $percentage; + } elseif ( Percentage::SOURCE === $commission_type_old ) { + $flat = 0; + } + $vendor->save_commission_settings( [ - 'percentage' => $commission->get_percentage(), - 'type' => $commission->get_type(), - 'flat' => $commission->get_flat(), + 'type' => $commission_type, + 'flat' => $flat, + 'percentage' => $percentage, 'category_commissions' => $commission->get_category_commissions(), ] ); @@ -113,14 +137,27 @@ private function update_vendors_settings( $vendors ) { private function update_products_settings( $posts ) { foreach ( $posts as $post ) { $commission = dokan()->product->get_commission_settings( $post->ID ); + + $commission_type_old = $commission->get_type(); $commission->set_type( Fixed::SOURCE ); + $commission_type = $commission->get_type(); + $flat = $commission->get_flat(); + $percentage = $commission->get_percentage(); + + if ( Flat::SOURCE === $commission_type_old ) { + $percentage = 0; + $flat = $percentage; + } elseif ( Percentage::SOURCE === $commission_type_old ) { + $flat = 0; + } + dokan()->product->save_commission_settings( $post->ID, [ - 'percentage' => $commission->get_percentage(), - 'type' => $commission->get_type(), - 'flat' => $commission->get_flat(), + 'type' => $commission_type, + 'percentage' => $percentage, + 'flat' => $flat, ] ); } diff --git a/includes/Upgrade/Upgrades/V_3_14_0.php b/includes/Upgrade/Upgrades/V_3_14_0.php index bdaefbc903..69695b314d 100644 --- a/includes/Upgrade/Upgrades/V_3_14_0.php +++ b/includes/Upgrade/Upgrades/V_3_14_0.php @@ -4,6 +4,8 @@ use WeDevs\Dokan\Abstracts\DokanUpgrader; use WeDevs\Dokan\Commission\Formula\Fixed; +use WeDevs\Dokan\Commission\Formula\Flat; +use WeDevs\Dokan\Commission\Formula\Percentage; use WeDevs\Dokan\Upgrade\Upgrades\BackgroundProcesses\V_3_14_0_UpdateCommissions; class V_3_14_0 extends DokanUpgrader { @@ -16,11 +18,21 @@ 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; + + 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; + } update_option( 'dokan_selling', $options ); } } From 27fa0e1b836d7a60d855fb35aa1a1291f6557aae Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Fri, 22 Nov 2024 18:21:07 +0600 Subject: [PATCH 11/14] Fix commission upgrade --- .../V_3_14_0_UpdateCommissions.php | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php b/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php index ce5bfebe45..3d140df258 100644 --- a/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php +++ b/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php @@ -97,26 +97,26 @@ private function update_global_settings( $terms ) { */ private function update_vendors_settings( $vendors ) { foreach ( $vendors as $vendor ) { - $commission = $vendor->get_commission_settings(); + $commission = $vendor->get_commission_settings(); + $commission_type_old = $commission->get_type(); $commission->set_type( Fixed::SOURCE ); - $commission_type = $commission->get_type(); - $flat = $commission->get_flat(); - $percentage = $commission->get_percentage(); + $percentage = $commission->get_percentage(); if ( Flat::SOURCE === $commission_type_old ) { - $percentage = 0; - $flat = $percentage; + $commission->set_percentage( 0 ); + $commission->set_flat( $percentage ); } elseif ( Percentage::SOURCE === $commission_type_old ) { - $flat = 0; + $commission->set_percentage( $percentage ); + $commission->set_flat( 0 ); } $vendor->save_commission_settings( [ - 'type' => $commission_type, - 'flat' => $flat, - 'percentage' => $percentage, + 'type' => $commission->get_type(), + 'flat' => $commission->get_flat(), + 'percentage' => $commission->get_percentage(), 'category_commissions' => $commission->get_category_commissions(), ] ); @@ -141,23 +141,19 @@ private function update_products_settings( $posts ) { $commission_type_old = $commission->get_type(); $commission->set_type( Fixed::SOURCE ); - $commission_type = $commission->get_type(); - $flat = $commission->get_flat(); - $percentage = $commission->get_percentage(); - if ( Flat::SOURCE === $commission_type_old ) { - $percentage = 0; - $flat = $percentage; + $commission->set_flat( $commission->get_percentage() ); + $commission->set_percentage( 0 ); } elseif ( Percentage::SOURCE === $commission_type_old ) { - $flat = 0; + $commission->set_flat( 0 ); } dokan()->product->save_commission_settings( $post->ID, [ - 'type' => $commission_type, - 'percentage' => $percentage, - 'flat' => $flat, + 'type' => $commission->get_type(), + 'percentage' => $commission->get_percentage(), + 'flat' => $commission->get_flat(), ] ); } From 3f3d634fb21c8afa6a745202f1111e3de1978078 Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 25 Nov 2024 19:07:48 +0600 Subject: [PATCH 12/14] Add upgrader --- .../Upugrader/Update_Category_Commission.php | 161 +++++++++++++++ .../Upugrader/Update_Product_Commission.php | 186 ++++++++++++++++++ .../Upugrader/Update_Vendor_Commission.php | 150 ++++++++++++++ includes/Upgrade/Hooks.php | 12 ++ .../V_3_14_0_UpdateCommissions.php | 163 --------------- includes/Upgrade/Upgrades/V_3_14_0.php | 115 ++--------- 6 files changed, 523 insertions(+), 264 deletions(-) create mode 100644 includes/Commission/Upugrader/Update_Category_Commission.php create mode 100644 includes/Commission/Upugrader/Update_Product_Commission.php create mode 100644 includes/Commission/Upugrader/Update_Vendor_Commission.php delete mode 100644 includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php diff --git a/includes/Commission/Upugrader/Update_Category_Commission.php b/includes/Commission/Upugrader/Update_Category_Commission.php new file mode 100644 index 0000000000..182812c7f6 --- /dev/null +++ b/includes/Commission/Upugrader/Update_Category_Commission.php @@ -0,0 +1,161 @@ +schedule_next_batch( 1 ); + } + + /** + * Process a batch of categories + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Current page number + * + * @return void + */ + public function process_batch( $page_number ) { + // Get categories for this batch + $categories = $this->get_categories_batch( $page_number ); + + if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) { + foreach ( $categories as $category ) { + $this->process_single_category( $category ); + } + + // Schedule next batch since we have categories in this batch + $this->schedule_next_batch( $page_number + 1 ); + } + } + + /** + * 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( $page_number ) { + WC()->queue()->add( + self::PROCESS_BATCH_HOOK, + [ $page_number ], + 'category_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( $page_number ) { + $args = [ + 'taxonomy' => 'product_cat', + 'number' => self::BATCH_SIZE, + 'orderby' => 'name', + 'order' => 'ASC', + 'hide_empty' => false, + 'offset' => ( $page_number - 1 ) * self::BATCH_SIZE, + ]; + + return get_terms( $args ); + } + + /** + * Process a single category. + * + * @since DOKAN_PRO_SINCE + * + * @param WP_Term $term Category term object + * + * @return void + */ + protected function process_single_category( $term ) { + error_log( + sprintf( + 'Processing category #%d: %s', + $term->term_id, + $term->name + ) + ); + + $dokan_selling = get_option( 'dokan_selling', [] ); + $category_commission = dokan_get_option( 'commission_category_based_values', 'dokan_selling', [] ); + + $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_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->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..7ec49e4bc1 --- /dev/null +++ b/includes/Commission/Upugrader/Update_Product_Commission.php @@ -0,0 +1,186 @@ +get_total_products(); + + if ( $total_products === 0 ) { + return; + } + + // Schedule the first batch + $this->schedule_next_batch( 0, $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->process_single_product( $product ); + } + + // Calculate next offset + $next_offset = $offset + self::BATCH_SIZE; + + // If there are more products to process, schedule the next batch + if ( $next_offset < $total_products ) { + $this->schedule_next_batch( $next_offset, $total_products ); + } + } + + /** + * 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, + ], 'product_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' => 'publish', + '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 WC_Product $product + * + * @return void + */ + protected function process_single_product( $product ) { + // Example processing - customize this based on your needs + error_log( + sprintf( + 'Processing product #%d: %s', + $product->get_id(), + $product->get_name() + ) + ); + + $commission = dokan()->product->get_commission_settings( $product->get_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->get_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..b20e9cb9ce --- /dev/null +++ b/includes/Commission/Upugrader/Update_Vendor_Commission.php @@ -0,0 +1,150 @@ +schedule_next_batch( 1 ); + } + + /** + * Process a batch of vendors + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Current page number + * + * @return void + */ + public function process_batch( $page_number ) { + // Get vendors for this batch + $vendors = $this->get_vendors_batch( $page_number ); + + if ( ! empty( $vendors ) ) { + foreach ( $vendors as $vendor ) { + $this->process_single_vendor( $vendor ); + } + + // Schedule next batch since we have vendors in this batch + $this->schedule_next_batch( $page_number + 1 ); + } + } + + /** + * Schedule the next batch of vendors + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Next page number to process + * + * @return void + */ + protected function schedule_next_batch( $page_number ) { + WC()->queue()->add( + self::PROCESS_BATCH_HOOK, + [ $page_number ], + 'vendor_processing' + ); + } + + /** + * Get a batch of vendors + * + * @since DOKAN_PRO_SINCE + * + * @param int $page_number Page number to fetch + * + * @return array Array of vendor objects + */ + protected function get_vendors_batch( $page_number ) { + return dokan()->vendor->all( + [ + 'paged' => $page_number, + ] + ); + } + + /** + * Process a single vendor + * Customize this method based on what you need to do with each vendor + * + * @since DOKAN_PRO_SINCE + * + * @param \WeDevs\Dokan\Vendor\Vendor $vendor Vendor object + * + * @return void + */ + protected function process_single_vendor( $vendor ) { + error_log( + sprintf( + 'Processing vendor #%d: %s', + $vendor->get_id(), + $vendor->get_shop_name() + ) + ); + + $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(), + ] + ); + } + + /** + * 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/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 3d140df258..0000000000 --- a/includes/Upgrade/Upgrades/BackgroundProcesses/V_3_14_0_UpdateCommissions.php +++ /dev/null @@ -1,163 +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_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->term_id ] = $category_commission_item; - } - } - - $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_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(), - ] - ); - } - - 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_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( - $post->ID, - [ - 'type' => $commission->get_type(), - 'percentage' => $commission->get_percentage(), - '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 69695b314d..339b67187c 100644 --- a/includes/Upgrade/Upgrades/V_3_14_0.php +++ b/includes/Upgrade/Upgrades/V_3_14_0.php @@ -6,7 +6,9 @@ use WeDevs\Dokan\Commission\Formula\Fixed; use WeDevs\Dokan\Commission\Formula\Flat; use WeDevs\Dokan\Commission\Formula\Percentage; -use WeDevs\Dokan\Upgrade\Upgrades\BackgroundProcesses\V_3_14_0_UpdateCommissions; +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 { @@ -38,115 +40,26 @@ public static function update_global_commission_type() { } /** - * Update global category commisions. + * Update vendor and product comission settings. * * @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 ); - } - - ++$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 vendor 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; + $vendor_scheduler = new Update_Vendor_Commission(); + if ( ! $vendor_scheduler->is_processing() ) { + $vendor_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; + $category_scheduler = new Update_Category_Commission(); + if ( ! $category_scheduler->is_processing() ) { + $category_scheduler->start_processing(); } - - $processor->dispatch_process(); } } From 1e19d0b7c527f36f0fd7d685c9a8a18e9145076a Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 25 Nov 2024 19:40:29 +0600 Subject: [PATCH 13/14] Add upgrader item --- .../Upugrader/Update_Category_Commission.php | 39 +++++++++++-------- .../Upugrader/Update_Product_Commission.php | 37 +++++++++++------- .../Upugrader/Update_Vendor_Commission.php | 32 +++++++++------ 3 files changed, 65 insertions(+), 43 deletions(-) diff --git a/includes/Commission/Upugrader/Update_Category_Commission.php b/includes/Commission/Upugrader/Update_Category_Commission.php index 182812c7f6..6c3e94d3fb 100644 --- a/includes/Commission/Upugrader/Update_Category_Commission.php +++ b/includes/Commission/Upugrader/Update_Category_Commission.php @@ -21,11 +21,18 @@ class Update_Category_Commission { */ const PROCESS_BATCH_HOOK = 'process_category_batch'; + /** + * + * @since DOKAN_PRO_SINCE + */ + const PROCESS_ITEM_HOOK = 'process_category_item'; + /** * Initialize the processor */ public function init_hooks() { add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 1 ); + add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_category' ], 10, 1 ); } /** @@ -55,7 +62,7 @@ public function process_batch( $page_number ) { if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) { foreach ( $categories as $category ) { - $this->process_single_category( $category ); + $this->schedule_cat_item( $category->term_id ); } // Schedule next batch since we have categories in this batch @@ -76,7 +83,15 @@ protected function schedule_next_batch( $page_number ) { WC()->queue()->add( self::PROCESS_BATCH_HOOK, [ $page_number ], - 'category_processing' + 'dokan_updater_category_processing' + ); + } + + private function schedule_cat_item( $term ) { + WC()->queue()->add( + self::PROCESS_ITEM_HOOK, + [ $term ], + 'dokan_updater_category_item_processing' ); } @@ -107,25 +122,17 @@ protected function get_categories_batch( $page_number ) { * * @since DOKAN_PRO_SINCE * - * @param WP_Term $term Category term object + * @param int $term Category term object * * @return void */ - protected function process_single_category( $term ) { - error_log( - sprintf( - 'Processing category #%d: %s', - $term->term_id, - $term->name - ) - ); - + 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->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 ); + $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 = [ @@ -141,7 +148,7 @@ protected function process_single_category( $term ) { $category_commission_item['flat'] = 0; } - $category_commission['items'][ $term->term_id ] = $category_commission_item; + $category_commission['items'][ $term_id ] = $category_commission_item; } $dokan_selling['commission_category_based_values'] = $category_commission; diff --git a/includes/Commission/Upugrader/Update_Product_Commission.php b/includes/Commission/Upugrader/Update_Product_Commission.php index 7ec49e4bc1..3e96f36b34 100644 --- a/includes/Commission/Upugrader/Update_Product_Commission.php +++ b/includes/Commission/Upugrader/Update_Product_Commission.php @@ -23,8 +23,15 @@ class Update_Product_Commission { */ const PROCESS_BATCH_HOOK = 'process_product_batch'; + /** + * + * @since DOKAN_PRO_SINCE + */ + const PROCESS_ITEM_HOOK = 'process_product_item'; + public function init_hooks() { add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 2 ); + add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_product' ], 10, 1 ); } /** @@ -61,7 +68,7 @@ public function process_batch( $offset, $total_products ) { $products = $this->get_products_batch( $offset ); foreach ( $products as $product ) { - $this->process_single_product( $product ); + $this->schedule_item( $product->get_id() ); } // Calculate next offset @@ -88,7 +95,16 @@ protected function schedule_next_batch( $offset, $total_products ) { self::PROCESS_BATCH_HOOK, [ $offset, $total_products, - ], 'product_processing' + ], + 'dokan_updater_product_processing' + ); + } + + private function schedule_item( $item ) { + WC()->queue()->add( + self::PROCESS_ITEM_HOOK, + [ $item ], + 'dokan_updater_product_item_processing' ); } @@ -137,21 +153,12 @@ protected function get_total_products() { * * @since DOKAN_PRO_SINCE * - * @param WC_Product $product + * @param int $product * * @return void */ - protected function process_single_product( $product ) { - // Example processing - customize this based on your needs - error_log( - sprintf( - 'Processing product #%d: %s', - $product->get_id(), - $product->get_name() - ) - ); - - $commission = dokan()->product->get_commission_settings( $product->get_id() ); + 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 ); @@ -164,7 +171,7 @@ protected function process_single_product( $product ) { } dokan()->product->save_commission_settings( - $product->get_id(), + $product_id, [ 'type' => $commission->get_type(), 'percentage' => $commission->get_percentage(), diff --git a/includes/Commission/Upugrader/Update_Vendor_Commission.php b/includes/Commission/Upugrader/Update_Vendor_Commission.php index b20e9cb9ce..d29db8d4a4 100644 --- a/includes/Commission/Upugrader/Update_Vendor_Commission.php +++ b/includes/Commission/Upugrader/Update_Vendor_Commission.php @@ -15,6 +15,12 @@ class Update_Vendor_Commission { */ const PROCESS_BATCH_HOOK = 'process_vendor_batch'; + /** + * + * @since DOKAN_PRO_SINCE + */ + const PROCESS_ITEM_HOOK = 'process_vendor_item'; + /** * Initialize the processor * @@ -22,6 +28,7 @@ class Update_Vendor_Commission { */ public function init_hooks() { add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 1 ); + add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_vendor' ], 10, 1 ); } /** @@ -51,7 +58,7 @@ public function process_batch( $page_number ) { if ( ! empty( $vendors ) ) { foreach ( $vendors as $vendor ) { - $this->process_single_vendor( $vendor ); + $this->schedule_item( $vendor->get_id() ); } // Schedule next batch since we have vendors in this batch @@ -72,7 +79,15 @@ protected function schedule_next_batch( $page_number ) { WC()->queue()->add( self::PROCESS_BATCH_HOOK, [ $page_number ], - 'vendor_processing' + 'dokan_updater_vendor_processing' + ); + } + + private function schedule_item( $item ) { + WC()->queue()->add( + self::PROCESS_ITEM_HOOK, + [ $item ], + 'dokan_updater_vendor_item_processing' ); } @@ -83,7 +98,7 @@ protected function schedule_next_batch( $page_number ) { * * @param int $page_number Page number to fetch * - * @return array Array of vendor objects + * @return \WeDevs\Dokan\Vendor\Vendor[] Array of vendor objects */ protected function get_vendors_batch( $page_number ) { return dokan()->vendor->all( @@ -103,15 +118,8 @@ protected function get_vendors_batch( $page_number ) { * * @return void */ - protected function process_single_vendor( $vendor ) { - error_log( - sprintf( - 'Processing vendor #%d: %s', - $vendor->get_id(), - $vendor->get_shop_name() - ) - ); - + public function process_single_vendor( $vendor_id ) { + $vendor = dokan()->vendor->get( $vendor_id ); $commission = $vendor->get_commission_settings(); $commission_type_old = $commission->get_type(); From 28874d2a284c67abf9a067867b3d60ac4c712381 Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:49:13 +0600 Subject: [PATCH 14/14] Add upgrader queue --- .../Upugrader/Update_Category_Commission.php | 80 +++++++-- .../Upugrader/Update_Product_Commission.php | 52 ++++-- .../Upugrader/Update_Vendor_Commission.php | 167 +++++++++++------- 3 files changed, 212 insertions(+), 87 deletions(-) diff --git a/includes/Commission/Upugrader/Update_Category_Commission.php b/includes/Commission/Upugrader/Update_Category_Commission.php index 6c3e94d3fb..a10d568b4f 100644 --- a/includes/Commission/Upugrader/Update_Category_Commission.php +++ b/includes/Commission/Upugrader/Update_Category_Commission.php @@ -20,6 +20,7 @@ class Update_Category_Commission { * @since DOKAN_PRO_SINCE */ const PROCESS_BATCH_HOOK = 'process_category_batch'; + const PROCESS_BATCH_HOOK_CREATOR = 'process_category_batch_creator'; /** * @@ -31,8 +32,9 @@ class Update_Category_Commission { * Initialize the processor */ public function init_hooks() { - add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 1 ); - add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_category' ], 10, 1 ); + add_action( self::PROCESS_BATCH_HOOK_CREATOR, [ $this, 'process_batch_creator' ] ); + add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ] ); + add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_category' ] ); } /** @@ -43,8 +45,37 @@ public function init_hooks() { * @return void */ public function start_processing() { - // Schedule the first batch with page 1 - $this->schedule_next_batch( 1 ); + WC()->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 ); } /** @@ -56,17 +87,14 @@ public function start_processing() { * * @return void */ - public function process_batch( $page_number ) { + public function process_batch( $offset ) { // Get categories for this batch - $categories = $this->get_categories_batch( $page_number ); + $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 next batch since we have categories in this batch - $this->schedule_next_batch( $page_number + 1 ); } } @@ -79,14 +107,23 @@ public function process_batch( $page_number ) { * * @return void */ - protected function schedule_next_batch( $page_number ) { + protected function schedule_next_batch( $offset ) { WC()->queue()->add( self::PROCESS_BATCH_HOOK, - [ $page_number ], + [ $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, @@ -104,19 +141,36 @@ private function schedule_cat_item( $term ) { * * @return array Array of term objects */ - protected function get_categories_batch( $page_number ) { + protected function get_categories_batch( $offset ) { $args = [ 'taxonomy' => 'product_cat', 'number' => self::BATCH_SIZE, 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, - 'offset' => ( $page_number - 1 ) * self::BATCH_SIZE, + '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. * diff --git a/includes/Commission/Upugrader/Update_Product_Commission.php b/includes/Commission/Upugrader/Update_Product_Commission.php index 3e96f36b34..752e9833f9 100644 --- a/includes/Commission/Upugrader/Update_Product_Commission.php +++ b/includes/Commission/Upugrader/Update_Product_Commission.php @@ -22,6 +22,7 @@ class Update_Product_Commission { * @since DOKAN_PRO_SINCE */ const PROCESS_BATCH_HOOK = 'process_product_batch'; + const PROCESS_BATCH_HOOK_CREATOR = 'process_product_batch_creator'; /** * @@ -30,8 +31,9 @@ class Update_Product_Commission { const PROCESS_ITEM_HOOK = 'process_product_item'; public function init_hooks() { + add_action( self::PROCESS_BATCH_HOOK_CREATOR, [ $this, 'process_batch_creator' ] ); add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 2 ); - add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_product' ], 10, 1 ); + add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_product' ] ); } /** @@ -42,15 +44,37 @@ public function init_hooks() { * @return void */ public function start_processing() { + WC()->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; } - // Schedule the first batch - $this->schedule_next_batch( 0, $total_products ); + $offset = 0; + + do { + $this->schedule_next_batch( $offset, $total_products ); + + // Calculate next offset + $offset = $offset + self::BATCH_SIZE; + } while ( $offset < $total_products ); } /** @@ -70,14 +94,6 @@ public function process_batch( $offset, $total_products ) { foreach ( $products as $product ) { $this->schedule_item( $product->get_id() ); } - - // Calculate next offset - $next_offset = $offset + self::BATCH_SIZE; - - // If there are more products to process, schedule the next batch - if ( $next_offset < $total_products ) { - $this->schedule_next_batch( $next_offset, $total_products ); - } } /** @@ -100,6 +116,15 @@ protected function schedule_next_batch( $offset, $total_products ) { ); } + /** + * 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, @@ -138,12 +163,13 @@ protected function get_products_batch( $offset ) { */ protected function get_total_products() { $args = [ - 'status' => 'publish', - 'limit' => -1, + 'status' => 'any', + 'limit' => -1, 'return' => 'ids', ]; $products = wc_get_products( $args ); + return count( $products ); } diff --git a/includes/Commission/Upugrader/Update_Vendor_Commission.php b/includes/Commission/Upugrader/Update_Vendor_Commission.php index d29db8d4a4..eaacdc9a00 100644 --- a/includes/Commission/Upugrader/Update_Vendor_Commission.php +++ b/includes/Commission/Upugrader/Update_Vendor_Commission.php @@ -7,19 +7,17 @@ use WeDevs\Dokan\Commission\Formula\Percentage; class Update_Vendor_Commission { - /** - * The hook name for processing batches - * - * @since DOKAN_PRO_SINCE + * Hook names for processing */ + const PROCESS_BATCH_HOOK_CREATOR = 'process_vendor_batch_creator'; const PROCESS_BATCH_HOOK = 'process_vendor_batch'; + const PROCESS_ITEM_HOOK = 'process_vendor_item'; /** - * - * @since DOKAN_PRO_SINCE + * Batch size */ - const PROCESS_ITEM_HOOK = 'process_vendor_item'; + const BATCH_SIZE = 10; /** * Initialize the processor @@ -27,8 +25,9 @@ class Update_Vendor_Commission { * @since DOKAN_PRO_SINCE */ public function init_hooks() { - add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 1 ); - add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_vendor' ], 10, 1 ); + add_action( self::PROCESS_BATCH_HOOK_CREATOR, [ $this, 'process_batch_creator' ], 10, 2 ); + add_action( self::PROCESS_BATCH_HOOK, [ $this, 'process_batch' ], 10, 2 ); + add_action( self::PROCESS_ITEM_HOOK, [ $this, 'process_single_vendor' ] ); } /** @@ -39,8 +38,34 @@ public function init_hooks() { * @return void */ public function start_processing() { - // Schedule the first batch with page 1 - $this->schedule_next_batch( 1 ); + WC()->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' + ); + } } /** @@ -49,99 +74,118 @@ public function start_processing() { * @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 ) { - // Get vendors for this batch + 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() ); } - - // Schedule next batch since we have vendors in this batch - $this->schedule_next_batch( $page_number + 1 ); } } /** - * Schedule the next batch of vendors + * Get a batch of vendors * * @since DOKAN_PRO_SINCE * - * @param int $page_number Next page number to process + * @param int $page_number Page number to fetch * - * @return void + * @return \WeDevs\Dokan\Vendor\Vendor[] Array of vendor objects */ - protected function schedule_next_batch( $page_number ) { - WC()->queue()->add( - self::PROCESS_BATCH_HOOK, - [ $page_number ], - 'dokan_updater_vendor_processing' - ); - } - - private function schedule_item( $item ) { - WC()->queue()->add( - self::PROCESS_ITEM_HOOK, - [ $item ], - 'dokan_updater_vendor_item_processing' + protected function get_vendors_batch( $page_number ) { + return dokan()->vendor->all( + [ + 'paged' => $page_number, + 'number' => self::BATCH_SIZE, + ] ); } /** - * Get a batch of vendors + * Schedule an individual vendor for processing * * @since DOKAN_PRO_SINCE * - * @param int $page_number Page number to fetch + * @param int $vendor_id * - * @return \WeDevs\Dokan\Vendor\Vendor[] Array of vendor objects + * @return void */ - protected function get_vendors_batch( $page_number ) { - return dokan()->vendor->all( - [ - 'paged' => $page_number, - ] + private function schedule_item( $vendor_id ) { + WC()->queue()->add( + self::PROCESS_ITEM_HOOK, + [ $vendor_id ], + 'dokan_updater_vendor_item_processing' ); } /** * Process a single vendor - * Customize this method based on what you need to do with each vendor * * @since DOKAN_PRO_SINCE * - * @param \WeDevs\Dokan\Vendor\Vendor $vendor Vendor object + * @param int $vendor_id Vendor ID * * @return void */ public function process_single_vendor( $vendor_id ) { - $vendor = dokan()->vendor->get( $vendor_id ); - $commission = $vendor->get_commission_settings(); + try { + $vendor = dokan()->vendor->get( $vendor_id ); + $commission = $vendor->get_commission_settings(); - $commission_type_old = $commission->get_type(); - $commission->set_type( Fixed::SOURCE ); + $commission_type_old = $commission->get_type(); + $commission->set_type( Fixed::SOURCE ); - $percentage = $commission->get_percentage(); + $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 ); + } - 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() ); } + } - $vendor->save_commission_settings( - [ - 'type' => $commission->get_type(), - 'flat' => $commission->get_flat(), - 'percentage' => $commission->get_percentage(), - 'category_commissions' => $commission->get_category_commissions(), - ] + /** + * 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' ), + ] ); } @@ -153,6 +197,7 @@ public function process_single_vendor( $vendor_id ) { * @return bool */ public function is_processing() { - return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null; + return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null + || WC()->queue()->get_next( self::PROCESS_ITEM_HOOK ) !== null; } }