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

Merged
merged 14 commits into from
Nov 26, 2024
Merged
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
168 changes: 168 additions & 0 deletions includes/Commission/Upugrader/Update_Category_Commission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace WeDevs\Dokan\Commission\Upugrader;
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

Typo in namespace 'Upugrader' should be 'Upgrader'

The namespace WeDevs\Dokan\Commission\Upugrader appears to have a typo. It should be Upgrader instead of Upugrader.

Apply this diff to correct the namespace:

-namespace WeDevs\Dokan\Commission\Upugrader;
+namespace WeDevs\Dokan\Commission\Upgrader;
📝 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
namespace WeDevs\Dokan\Commission\Upugrader;
namespace WeDevs\Dokan\Commission\Upgrader;


use WeDevs\Dokan\Commission\Formula\Flat;
use WeDevs\Dokan\Commission\Formula\Percentage;
use WP_Term;

class Update_Category_Commission {
/**
* The batch size for processing categories
*
* @since DOKAN_PRO_SINCE
*/
const BATCH_SIZE = 20;

/**
* The hook name for processing batches
*
* @since DOKAN_PRO_SINCE
*/
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 );
}

/**
* Start the batch processing
*
* @since DOKAN_PRO_SINCE
*
* @return void
*/
public function start_processing() {
// Schedule the first batch with page 1
$this->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->schedule_cat_item( $category->term_id );
}

// 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 ],
'dokan_updater_category_processing'
);
}

private function schedule_cat_item( $term ) {
WC()->queue()->add(
self::PROCESS_ITEM_HOOK,
[ $term ],
'dokan_updater_category_item_processing'
);
}

/**
* Get a batch of categories.
*
* @since DOKAN_PRO_SINCE
*
* @param int $page_number Page number to fetch
*
* @return array Array of term objects
*/
protected function get_categories_batch( $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 int $term Category term object
*
* @return void
*/
public function process_single_category( $term_id ) {
Comment on lines +179 to +183
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

Inconsistent parameter name and description in docblock

The docblock parameter is $term, described as 'Category term object', but the method process_single_category accepts $term_id, which is an integer term ID. Please update the docblock to match the method signature and clarify the parameter's purpose.

Apply this diff to correct the docblock:

- * @param int $term Category term object
+ * @param int $term_id Term ID to process
📝 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
* @param int $term Category term object
*
* @return void
*/
public function process_single_category( $term_id ) {
* @param int $term_id Term ID to process
*
* @return void
*/
public function process_single_category( $term_id ) {

$dokan_selling = get_option( 'dokan_selling', [] );
$category_commission = dokan_get_option( 'commission_category_based_values', 'dokan_selling', [] );

$commission_type = get_term_meta( $term_id, 'per_category_admin_commission_type', true );
$admin_additional_fee = get_term_meta( $term_id, 'per_category_admin_additional_fee', true );
$commission = get_term_meta( $term_id, 'per_category_admin_commission', true );

if ( ! empty( $commission_type ) ) {
$category_commission_item = [
'flat' => $admin_additional_fee,
'percentage' => $commission,
];

if ( Flat::SOURCE === $commission_type ) {
$category_commission_item['percentage'] = 0;
$category_commission_item['flat'] = $commission;
} elseif ( Percentage::SOURCE === $commission_type ) {
$category_commission_item['percentage'] = $commission;
$category_commission_item['flat'] = 0;
}

$category_commission['items'][ $term_id ] = $category_commission_item;
}

$dokan_selling['commission_category_based_values'] = $category_commission;
update_option( 'dokan_selling', $dokan_selling );
}

/**
* Check if processing is currently running.
*
* @since DOKAN_PRO_SINCE
*
* @return bool
*/
public function is_processing() {
return WC()->queue()->get_next( self::PROCESS_BATCH_HOOK ) !== null;
}
}
Loading
Loading