From cce9ddc7a1395262b9c29c893c99af61fcdee2fe Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 18 Nov 2024 11:31:31 +0600 Subject: [PATCH 01/14] Fix decimal and thousand separator for category commission. --- .../Commission/CategoryBasedCommission.vue | 63 ++++++++++++++----- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/src/admin/components/Commission/CategoryBasedCommission.vue b/src/admin/components/Commission/CategoryBasedCommission.vue index 754e0eaf35..a1bd147456 100644 --- a/src/admin/components/Commission/CategoryBasedCommission.vue +++ b/src/admin/components/Commission/CategoryBasedCommission.vue @@ -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;" /> @@ -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 @@ From 7fd331129ea3cfde079e0f59dc201afa5d7a140e Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 18 Nov 2024 12:03:05 +0600 Subject: [PATCH 02/14] Fix decimal and thousand separator for product commission. --- includes/Product/Hooks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Product/Hooks.php b/includes/Product/Hooks.php index 961e19556d..ffc0cb2fc8 100644 --- a/includes/Product/Hooks.php +++ b/includes/Product/Hooks.php @@ -491,7 +491,7 @@ class="woocommerce-help-tip" - + @@ -539,7 +539,7 @@ public static function save_per_product_commission_options( $post_id ) { update_post_meta( $post_id, '_per_product_admin_commission_type', $commission_type ); } - if ( isset( $_POST['_per_product_admin_commission'] ) ) { // phpcs:ignore + if ( isset( $_POST['_per_product_admin_commission'] ) && 0 <= $_POST['_per_product_admin_commission'] && 100 >= $_POST['_per_product_admin_commission'] ) { // phpcs:ignore $admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : sanitize_text_field( $_POST['_per_product_admin_commission'] ); // phpcs:ignore } From c9458fe1ccd550031f940bed23823686c1564c7e Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 18 Nov 2024 13:45:33 +0600 Subject: [PATCH 03/14] Fix decimal and thousand separator for product commission. --- includes/Product/Hooks.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/Product/Hooks.php b/includes/Product/Hooks.php index ffc0cb2fc8..00cd55d102 100644 --- a/includes/Product/Hooks.php +++ b/includes/Product/Hooks.php @@ -539,15 +539,19 @@ public static function save_per_product_commission_options( $post_id ) { update_post_meta( $post_id, '_per_product_admin_commission_type', $commission_type ); } - if ( isset( $_POST['_per_product_admin_commission'] ) && 0 <= $_POST['_per_product_admin_commission'] && 100 >= $_POST['_per_product_admin_commission'] ) { // phpcs:ignore - $admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : sanitize_text_field( $_POST['_per_product_admin_commission'] ); // phpcs:ignore + if ( isset( $_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 ) ); } } From ccd8380466aea8e41965d24a0f55ffbb07b0071c Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:09:06 +0600 Subject: [PATCH 04/14] Fix commission items array, but it should be object. --- assets/src/js/setup-wizard/commission/AdminCommission.vue | 7 ++++++- .../components/Commission/CategoryBasedCommission.vue | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) 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/src/admin/components/Commission/CategoryBasedCommission.vue b/src/admin/components/Commission/CategoryBasedCommission.vue index a1bd147456..c0736df331 100644 --- a/src/admin/components/Commission/CategoryBasedCommission.vue +++ b/src/admin/components/Commission/CategoryBasedCommission.vue @@ -161,8 +161,10 @@ this.commission.all = this.value.all; } - if ( typeof this.value === 'object' && this.value.hasOwnProperty( 'items' ) && typeof this.value.items === 'object') { + if ( typeof this.value === 'object' && this.value.hasOwnProperty( 'items' ) && ! Array.isArray( this.value.items ) ) { this.commission.items = this.value.items; + } else { + this.commission.items = {}; } dokan.api.get('/products/multistep-categories').then( data => { From 69b9aeeb231703680d4ff86bac049a751db17f46 Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:32:19 +0600 Subject: [PATCH 05/14] Update category expand icon color. --- src/admin/components/Commission/CategoryBasedCommission.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/admin/components/Commission/CategoryBasedCommission.vue b/src/admin/components/Commission/CategoryBasedCommission.vue index c0736df331..aef2b933ae 100644 --- a/src/admin/components/Commission/CategoryBasedCommission.vue +++ b/src/admin/components/Commission/CategoryBasedCommission.vue @@ -19,7 +19,7 @@

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

From 85c3dec9c317ba2c9297ebc959d7e2829a99d975 Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:32:32 +0600 Subject: [PATCH 06/14] Fix category based commission validation --- includes/Admin/Settings.php | 11 ++++--- src/admin/components/CombineInput.vue | 2 -- .../Commission/CategoryBasedCommission.vue | 8 ++--- src/admin/pages/Settings.vue | 33 ++++++++++++++++--- 4 files changed, 38 insertions(+), 16 deletions(-) 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/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 aef2b933ae..c380948270 100644 --- a/src/admin/components/Commission/CategoryBasedCommission.vue +++ b/src/admin/components/Commission/CategoryBasedCommission.vue @@ -66,7 +66,7 @@

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

@@ -284,7 +284,7 @@ } this.emitComponentChange( JSON.parse( JSON.stringify( this.commission ) ) ) - }, 500 ), + }, 700 ), handleAllCategoryInput: Debounce( function( value, commission_type, oldValue = '' ) { if ( 'percentage' === commission_type ) { @@ -296,7 +296,7 @@ this.$set(this.commission, 'items', {}); this.emitComponentChange( JSON.parse( JSON.stringify( this.commission ) ) ) - }, 500 ), + }, 700 ), deleteDuplicateCategories( items ) { let self = this; @@ -371,7 +371,7 @@ return value; } - return accounting.unformat(value, dokan.currency.decimal); + return String( accounting.unformat(value, dokan.currency.decimal) ); }, formatValue: ( value ) => { 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 { From 9ab26f596c8344342c54d150714edcd6a6594a19 Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:27:07 +0600 Subject: [PATCH 07/14] Fix vendor commission amount unformation. --- src/admin/pages/VendorSingle.vue | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) 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 = ''; } }, From 00bda244dd3f2f18fe3475e4c59e39e3cd3d382e Mon Sep 17 00:00:00 2001 From: Aunshon <32583103+Aunshon@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:58:29 +0600 Subject: [PATCH 08/14] Fix padding category error --- src/admin/components/Fields.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ) }}

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; } }