Skip to content
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

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
7 changes: 6 additions & 1 deletion assets/src/js/setup-wizard/commission/AdminCommission.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +64 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Strengthen data validation and initialization.

The current initialization logic has several potential issues:

  1. Loose comparisons might not catch all edge cases
  2. No validation for commission percentage ranges
  3. No handling for decimal/thousand separators

Consider applying these improvements:

-            let commission_category_based_values = 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;
+            // Ensure proper object initialization
+            commission_category_based_values.all = (typeof commission_category_based_values.all === 'object' && !Array.isArray(commission_category_based_values.all)) 
+                ? commission_category_based_values.all 
+                : {};
+            commission_category_based_values.items = (typeof commission_category_based_values.items === 'object' && !Array.isArray(commission_category_based_values.items)) 
+                ? commission_category_based_values.items 
+                : {};
+
+            // Validate commission percentages
+            Object.keys(commission_category_based_values.all).forEach(key => {
+                const value = parseFloat(commission_category_based_values.all[key]);
+                if (isNaN(value) || value < 0 || value > 100) {
+                    commission_category_based_values.all[key] = 0;
+                }
+            });

Committable suggestion skipped: line range outside the PR's diff.

},

methods: {
Expand Down
11 changes: 6 additions & 5 deletions includes/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
10 changes: 9 additions & 1 deletion includes/Commission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions includes/Product/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@
<div class="dokan-store-products-filter-area dokan-clearfix">
<form class="dokan-store-products-ordeby" method="get">
<input type="text" name="product_name" class="product-name-search dokan-store-products-filter-search"
placeholder="<?php esc_attr_e( 'Enter product name', 'dokan-lite' ); ?>" autocomplete="off"

Check warning on line 191 in includes/Product/Hooks.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found precision alignment of 3 spaces.
data-store_id="<?php echo esc_attr( $store_id ); ?>">

Check warning on line 192 in includes/Product/Hooks.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found precision alignment of 3 spaces.
<div id="dokan-store-products-search-result" class="dokan-ajax-store-products-search-result"></div>
<input type="submit" name="search_store_products" class="search-store-products dokan-btn-theme"
value="<?php esc_attr_e( 'Search', 'dokan-lite' ); ?>">

Check warning on line 195 in includes/Product/Hooks.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found precision alignment of 3 spaces.

<?php if ( is_array( $orderby_options['catalogs'] ) && isset( $orderby_options['orderby'] ) ) : ?>
<select name="product_orderby" class="orderby orderby-search"
Expand Down Expand Up @@ -491,7 +491,7 @@

<span class="wrapper">
<input type="hidden" value="fixed" name="_per_product_admin_commission_type">
<input id="admin_commission" class="input-text wc_input_price" type="text" name="_per_product_admin_commission" value="<?php echo $admin_commission; ?>">
<input id="admin_commission" class="input-text wc_input_price" min="0" max="100" type="text" name="_per_product_admin_commission" value="<?php echo wc_format_localized_price( $admin_commission ); ?>">
<span class="additional_fee">
<?php echo esc_html( '% &nbsp;&nbsp; +' ); ?>
<input class="input-text wc_input_price" type="text" name="_per_product_admin_additional_fee" value="<?php echo wc_format_localized_price( $additional_fee ); ?>">
Expand Down Expand Up @@ -540,14 +540,18 @@
}

if ( isset( $_POST['_per_product_admin_commission'] ) ) { // phpcs:ignore
$admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : sanitize_text_field( $_POST['_per_product_admin_commission'] ); // phpcs:ignore
$_per_product_admin_commission = wc_format_decimal( sanitize_text_field( $_POST['_per_product_admin_commission'] ) ); // phpcs:ignore

if ( 0 <= $_per_product_admin_commission && 100 >= $_per_product_admin_commission ) {
$admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : $_per_product_admin_commission; // phpcs:ignore
}
}

if ( isset( $_POST['_per_product_admin_additional_fee'] ) ) { // phpcs:ignore
$additional_fee = ( '' === $_POST['_per_product_admin_additional_fee'] ) ? '' : sanitize_text_field( $_POST['_per_product_admin_additional_fee'] ); // phpcs:ignore
}

update_post_meta( $post_id, '_per_product_admin_commission', wc_format_decimal( $admin_commission ) );
update_post_meta( $post_id, '_per_product_admin_commission', $admin_commission );
update_post_meta( $post_id, '_per_product_admin_additional_fee', wc_format_decimal( $additional_fee ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential Logical Inconsistency in Commission Assignments

In the update_global_settings method, there seems to be a logical inconsistency when assigning commission values based on the commission type. Initially, $category_commission_item is defined as:

$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 Flat case, you set 'flat' to $commission, which might actually represent a percentage value, and in the initial definition, 'flat' was set to $admin_additional_fee. This could lead to incorrect commission calculations.

Suggested Fix:

Ensure that the correct variables are assigned to 'flat' and 'percentage' based on the commission type. Here’s the corrected assignment:

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 'flat' uses $admin_additional_fee when the commission type is Flat.

}
}

Expand All @@ -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;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Incorrect Commission Value Assignments in update_vendors_settings

In the update_vendors_settings method, there is a logical error when adjusting commission values based on the old commission type:

if ( Flat::SOURCE === $commission_type_old ) {
    $percentage = 0;
    $flat       = $percentage;
} elseif ( Percentage::SOURCE === $commission_type_old ) {
    $flat = 0;
}

Setting $flat = $percentage; after $percentage = 0; results in $flat being zero. This likely nullifies the intended flat commission value.

Suggested Fix:

Assign the correct commission value to $flat or $percentage based on the old commission type:

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(),
]
);
Expand All @@ -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;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Same Issue in update_products_settings Method

The update_products_settings method contains a similar logical error:

if ( Flat::SOURCE === $commission_type_old ) {
    $percentage = 0;
    $flat       = $percentage;
} elseif ( Percentage::SOURCE === $commission_type_old ) {
    $flat = 0;
}

Again, setting $flat = $percentage; after $percentage = 0; results in both values being zero, which may not be intended.

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,
]
);
}
Expand Down
16 changes: 14 additions & 2 deletions includes/Upgrade/Upgrades/V_3_14_0.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

- $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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$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;
$options = get_option( 'dokan_selling', [] );
$commission_type = isset( $options['commission_type'] ) ? $options['commission_type'] : Fixed::SOURCE;
$admin_percentage = isset( $options['admin_percentage'] ) ? floatval( $options['admin_percentage'] ) : 0;
if ( $admin_percentage < 0 ) {
$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;
}
Comment on lines +29 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
}
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_sep')],
['', '.'],
$admin_percentage
);
if (!is_numeric($admin_percentage)) {
throw new Exception('Invalid number format for admin percentage');
}
$options['admin_percentage'] = 0;
$options['additional_fee'] = (float) $admin_percentage;
} catch ( Exception $e ) {
error_log( 'Dokan commission migration error: ' . $e->getMessage() );
return false;
}
} elseif ( Percentage::SOURCE === $commission_type ) {
try {
// Convert any localized number format to standard format
$admin_percentage = str_replace(
[get_option('thousands_sep'), get_option('decimal_sep')],
['', '.'],
$admin_percentage
);
if (!is_numeric($admin_percentage)) {
throw new Exception('Invalid number format for admin percentage');
}
$options['admin_percentage'] = (float) $admin_percentage;
$options['additional_fee'] = 0;
} catch ( Exception $e ) {
error_log( 'Dokan commission migration error: ' . $e->getMessage() );
return false;
}
}

update_option( 'dokan_selling', $options );
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/admin/components/CombineInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
71 changes: 54 additions & 17 deletions src/admin/components/Commission/CategoryBasedCommission.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class='flex flex-row'>
<div class='flex flex-row w-1/2 items-center min-h-[3rem] border-0 !border-r-[1px] !border-b-[1px] border-[#e9e9ea] border-solid pl-[5px]'>
<button type='button' class='p-1 d-xs:pl-1 md:pl-4 bg-transparent bg-co border-none cursor-pointer' @click='()=>allCategroyEnabled = !allCategroyEnabled'>
<i class="far" :class='! allCategroyEnabled ? "fa-minus-square text-black" : "fa-plus-square text-[#F05025]"'></i>
<i class="far" :class='! allCategroyEnabled ? "fa-minus-square text-black" : "fa-plus-square text-[#4C19E6]"'></i>
</button>
<p class='d-xs:text-[8px] sm:text-[14px] !m-0'>{{__( 'All Categories', 'dokan-lite' )}}</p>
</div>
Expand All @@ -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;"
/>
Expand All @@ -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;"
/>
Expand All @@ -66,7 +66,7 @@
<i class="far" :class='openRows.includes( Number( item.term_id ) ) ? "fa-minus-square text-black" : "fa-plus-square"'></i>
</button>
<p class='d-xs:text-[8px] sm:text-[14px] text-black !m-0'>
<span :title='item.name'>{{ item.name }}</span>
<span :title='item.name' v-html="item.name"></span>
<span class='d-xs:text-[6px] sm:text-[12px] text-gray-500' :title='__( "Category ID", "dokan" )'>#{{ item.term_id }}</span>
</p>
</div>
Expand All @@ -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;"
/>
Expand All @@ -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;"
/>
</div>
Expand All @@ -108,6 +108,8 @@
</template>

<script>
import Debounce from "debounce";

export default {
name: 'CategoryBasedCommission',
props: {
Expand Down Expand Up @@ -159,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 => {
Expand Down Expand Up @@ -253,10 +257,13 @@
return this.commission.all[comission_type];
},

commissinItemHandler( value, commission_type, term_id, oldValue = '' ) {
if (isNaN( value )) {
value = oldValue
commissinItemHandler: Debounce( function( value, commission_type, term_id, oldValue = '' ) {
if ( 'percentage' === commission_type ) {
value = this.validatePercentage( this.unFormatValue( value ) );
} else {
value = this.unFormatValue( value );
}

let commissions = JSON.parse( JSON.stringify( this.commission.items ) );

let data = JSON.parse( JSON.stringify( this.commission.all ) );
Expand All @@ -277,17 +284,19 @@
}

this.emitComponentChange( JSON.parse( JSON.stringify( this.commission ) ) )
},
}, 700 ),

handleAllCategoryInput( value, commission_type, oldValue = '' ) {
if (isNaN( value )) {
value = oldValue
handleAllCategoryInput: Debounce( function( value, commission_type, oldValue = '' ) {
if ( 'percentage' === commission_type ) {
value = this.validatePercentage( this.unFormatValue( value ) );
} else {
value = this.unFormatValue( value );
}
this.$set( this.commission.all, commission_type, value );
this.$set(this.commission, 'items', {});

this.emitComponentChange( JSON.parse( JSON.stringify( this.commission ) ) )
},
}, 700 ),

deleteDuplicateCategories( items ) {
let self = this;
Expand Down Expand Up @@ -355,7 +364,35 @@
} );

this.$set( this.commission, 'items', children );
}
},

unFormatValue: ( value ) => {
if ( value === '' ) {
return value;
}

return String( accounting.unformat(value, dokan.currency.decimal) );
},

formatValue: ( value ) => {
if ( value === '' ) {
return value;
}

return accounting.formatNumber( value, dokan.currency.precision, dokan.currency.thousand, dokan.currency.decimal );
},

validatePercentage( percentage ) {
if ( percentage === '' ) {
return percentage;
}

if ( Number( percentage ) < 0 || Number( percentage ) > 100 ) {
percentage = '';
}

return percentage;
},
}
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/admin/components/Fields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@
/>
</div>

<p v-if="hasError( fieldData.name )" class="dokan-error">
<p v-if="hasError( fieldData.name )" class="dokan-error pt-0 pl-5 pb-5 m-0">
{{ getError( fieldData.label ) }}
</p>
<p v-if="hasValidationError( fieldData.name )" class="dokan-error">
<p v-if="hasValidationError( fieldData.name )" class="dokan-error pl-5 pb-5">
{{ getValidationErrorMessage( fieldData.name ) }}
</p>
</div>
Expand Down
Loading
Loading