-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix decimal and thousand separator for category commission. #2440
base: develop
Are you sure you want to change the base?
Changes from 10 commits
cce9ddc
7fd3311
c9458fe
ccd8380
69b9aee
85c3dec
9ab26f5
00bda24
dcb8376
f26fd56
27fa0e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
Comment on lines
+66
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential Logical Inconsistency in Commission Assignments In the $category_commission_item = [
'flat' => $admin_additional_fee,
'percentage' => $commission,
]; However, within the conditional blocks: 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;
} In the Suggested Fix: Ensure that the correct variables are assigned to if ( Flat::SOURCE === $commission_type ) {
$category_commission_item['percentage'] = 0;
- $category_commission_item['flat'] = $commission;
+ $category_commission_item['flat'] = $admin_additional_fee;
} elseif ( Percentage::SOURCE === $commission_type ) {
$category_commission_item['percentage'] = $commission;
$category_commission_item['flat'] = 0;
} This change ensures that |
||
} | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect Commission Value Assignments in In the if ( Flat::SOURCE === $commission_type_old ) {
$percentage = 0;
$flat = $percentage;
} elseif ( Percentage::SOURCE === $commission_type_old ) {
$flat = 0;
} Setting Suggested Fix: Assign the correct commission value to if ( Flat::SOURCE === $commission_type_old ) {
$percentage = 0;
- $flat = $percentage;
+ $flat = $commission->get_flat();
} elseif ( Percentage::SOURCE === $commission_type_old ) {
$flat = 0;
+ $percentage = $commission->get_percentage();
} This ensures that the original commission values are correctly assigned. |
||
$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; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same Issue in The if ( Flat::SOURCE === $commission_type_old ) {
$percentage = 0;
$flat = $percentage;
} elseif ( Percentage::SOURCE === $commission_type_old ) {
$flat = 0;
} Again, setting Suggested Fix: Apply the same correction to ensure accurate commission value assignments: if ( Flat::SOURCE === $commission_type_old ) {
$percentage = 0;
- $flat = $percentage;
+ $flat = $commission->get_flat();
} elseif ( Percentage::SOURCE === $commission_type_old ) {
$flat = 0;
+ $percentage = $commission->get_percentage();
} This correction maintains the integrity of the commission settings based on the old commission type. |
||
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, | ||
] | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add validation for commission values The retrieved values from options should be validated and sanitized before use. Consider adding type casting and validation for - $admin_percentage = isset( $options['admin_percentage'] ) ? $options['admin_percentage'] : 0;
+ $admin_percentage = isset( $options['admin_percentage'] ) ? floatval( $options['admin_percentage'] ) : 0;
+ if ( $admin_percentage < 0 ) {
+ $admin_percentage = 0;
+ } 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+29
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling and number format conversion The commission value updates need proper error handling and should consider number format conversion given the PR's objective to fix decimal/thousand separator issues. if ( Flat::SOURCE === $commission_type ) {
+ try {
+ // Convert any localized number format to standard format
+ $admin_percentage = str_replace(
+ [get_option('thousands_sep'), get_option('decimal_point')],
+ ['', '.'],
+ $admin_percentage
+ );
$options['admin_percentage'] = 0;
$options['additional_fee'] = $admin_percentage;
+ } catch ( Exception $e ) {
+ error_log( 'Dokan commission migration error: ' . $e->getMessage() );
+ return false;
+ }
} elseif ( Percentage::SOURCE === $commission_type ) {
+ try {
$options['admin_percentage'] = $admin_percentage;
$options['additional_fee'] = 0;
+ } catch ( Exception $e ) {
+ error_log( 'Dokan commission migration error: ' . $e->getMessage() );
+ return false;
+ }
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
update_option( 'dokan_selling', $options ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strengthen data validation and initialization.
The current initialization logic has several potential issues:
Consider applying these improvements: