-
Notifications
You must be signed in to change notification settings - Fork 205
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
Enhancement: product commission bulk edit #2464
Changes from all commits
aa5c9ed
c17068f
e9452e0
185b21d
6917eb8
834a9b3
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 | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
namespace WeDevs\Dokan\Product; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
use WeDevs\Dokan\Commission\Formula\Fixed; | ||||||||||||||||||||||||||||
use WeDevs\Dokan\ProductCategory\Helper; | ||||||||||||||||||||||||||||
use WC_Product; | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -188,11 +189,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" | ||||||||||||||||||||||||||||
data-store_id="<?php echo esc_attr( $store_id ); ?>"> | ||||||||||||||||||||||||||||
<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' ); ?>"> | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
<?php if ( is_array( $orderby_options['catalogs'] ) && isset( $orderby_options['orderby'] ) ) : ?> | ||||||||||||||||||||||||||||
<select name="product_orderby" class="orderby orderby-search" | ||||||||||||||||||||||||||||
|
@@ -521,37 +522,45 @@ | |||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||
* @since 2.4.12 | ||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||
* @param integer $post_id | ||||||||||||||||||||||||||||
* @param integer $post_id | ||||||||||||||||||||||||||||
* @param array $data | ||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||
* @return void | ||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||
public static function save_per_product_commission_options( $post_id ) { | ||||||||||||||||||||||||||||
public static function save_per_product_commission_options( $post_id, $data = [] ) { | ||||||||||||||||||||||||||||
if ( ! current_user_can( 'manage_woocommerce' ) ) { | ||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
$commission_type = ''; | ||||||||||||||||||||||||||||
$commission_type = Fixed::SOURCE; | ||||||||||||||||||||||||||||
Comment on lines
+530
to
+535
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 Refactor to Avoid Direct Access to Accessing Consider refactoring to use public static function save_per_product_commission_options( $post_id, $data = [] ) {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
- $data = empty( $data ) ? $_POST : $data; // phpcs:ignore
+ if ( empty( $data ) ) {
+ $data = filter_input_array( INPUT_POST, [
+ '_per_product_admin_commission_type' => FILTER_SANITIZE_STRING,
+ '_per_product_admin_commission' => FILTER_SANITIZE_STRING,
+ '_per_product_admin_additional_fee' => FILTER_SANITIZE_STRING,
+ ] );
+ }
|
||||||||||||||||||||||||||||
$admin_commission = ''; | ||||||||||||||||||||||||||||
$additional_fee = ''; | ||||||||||||||||||||||||||||
$data = empty( $data ) ? $_POST : $data; // phpcs:ignore | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if ( isset( $_POST['_per_product_admin_commission_type'] ) ) { // phpcs:ignore | ||||||||||||||||||||||||||||
$commission_type = ! empty( $_POST['_per_product_admin_commission_type'] ) ? sanitize_text_field( $_POST['_per_product_admin_commission_type'] ) : 'percentage'; // phpcs:ignore | ||||||||||||||||||||||||||||
update_post_meta( $post_id, '_per_product_admin_commission_type', $commission_type ); | ||||||||||||||||||||||||||||
if ( isset( $data['_per_product_admin_commission_type'] ) ) { | ||||||||||||||||||||||||||||
$commission_type = ! empty( $data['_per_product_admin_commission_type'] ) ? sanitize_text_field( $data['_per_product_admin_commission_type'] ) : Fixed::SOURCE; | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
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 ( isset( $data['_per_product_admin_commission'] ) ) { | ||||||||||||||||||||||||||||
$_per_product_admin_commission = wc_format_decimal( sanitize_text_field( $data['_per_product_admin_commission'] ) ); | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
if ( 0 <= $_per_product_admin_commission && 100 >= $_per_product_admin_commission ) { | ||||||||||||||||||||||||||||
$admin_commission = ( '' === $_POST['_per_product_admin_commission'] ) ? '' : $_per_product_admin_commission; // phpcs:ignore | ||||||||||||||||||||||||||||
$admin_commission = ( '' === $data['_per_product_admin_commission'] ) ? '' : $_per_product_admin_commission; | ||||||||||||||||||||||||||||
Comment on lines
+545
to
+548
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. Validate Commission Percentage Input When processing the commission percentage, consider adding validation to ensure the input is numeric and within the expected range (0 to 100). Currently, non-numeric input could cause unintended behavior. Add a check to validate the input: $_per_product_admin_commission = wc_format_decimal( sanitize_text_field( $data['_per_product_admin_commission'] ) );
+ if ( ! is_numeric( $_per_product_admin_commission ) ) {
+ $_per_product_admin_commission = 0;
+ }
if ( 0 <= $_per_product_admin_commission && 100 >= $_per_product_admin_commission ) {
$admin_commission = ( '' === $data['_per_product_admin_commission'] ) ? '' : $_per_product_admin_commission;
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||
if ( isset( $data['_per_product_admin_additional_fee'] ) ) { | ||||||||||||||||||||||||||||
$additional_fee = ( '' === $data['_per_product_admin_additional_fee'] ) ? '' : sanitize_text_field( $data['_per_product_admin_additional_fee'] ); | ||||||||||||||||||||||||||||
$additional_fee = wc_format_decimal( $additional_fee ); | ||||||||||||||||||||||||||||
Comment on lines
+552
to
+554
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 validation for additional fee The additional fee should be validated to prevent negative values. if ( isset( $data['_per_product_admin_additional_fee'] ) ) {
$additional_fee = ( '' === $data['_per_product_admin_additional_fee'] ) ? '' : sanitize_text_field( $data['_per_product_admin_additional_fee'] );
$additional_fee = wc_format_decimal( $additional_fee );
+ if ( $additional_fee < 0 ) {
+ $additional_fee = 0;
+ }
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
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 ) ); | ||||||||||||||||||||||||||||
dokan()->product->save_commission_settings( | ||||||||||||||||||||||||||||
$post_id, | ||||||||||||||||||||||||||||
[ | ||||||||||||||||||||||||||||
'type' => $commission_type, | ||||||||||||||||||||||||||||
'percentage' => $admin_commission, | ||||||||||||||||||||||||||||
'flat' => $additional_fee, | ||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/** | ||
* Template Name: Dokan commission setting bulk product edit | ||
* | ||
* @since DOKAN_SINCE | ||
* | ||
* @package Dokan | ||
*/ | ||
|
||
use WeDevs\Dokan\Commission\Formula\Fixed; | ||
?> | ||
|
||
<div class="inline-edit-col" style="float: left"> | ||
<h4><?php esc_html_e( 'Commission settings', 'dokan-lite' ); ?></h4> | ||
<div class="inline-edit-group"> | ||
<label class="alignleft"> | ||
<span class="title"><?php esc_html_e( 'Commission', 'dokan-lite' ); ?></span> | ||
<span class="input-text-wrap"> | ||
<select class="dokan_override_bulk_product_commission change_to" name="dokan_override_bulk_product_commission"> | ||
<option value=""><?php esc_html_e( '— No change —', 'dokan-lite' ); ?></option> | ||
<option value="1"><?php esc_html_e( 'Change to:', 'dokan-lite' ); ?></option> | ||
</select> | ||
</span> | ||
</label> | ||
<div class="change-input inline-edit-group dokan-admin-bulk-product-commission-data-box" style=""> | ||
<div> | ||
<label for="admin_commission"> | ||
<?php esc_html_e( 'Fixed', 'dokan-lite' ); ?> | ||
</label> | ||
|
||
<span class="input-text-wrap" style="display: flex"> | ||
<input type="hidden" name="_per_product_admin_commission_type" value="<?php echo esc_attr( Fixed::SOURCE ); ?>"> | ||
<input class="input-text wc_input_price" min="0" max="100" type="text" name="_per_product_admin_commission" value=""/> | ||
<span style="display: flex; align-items: center">% +</span> | ||
<input type="text" name="_per_product_admin_additional_fee" class="input-text wc_input_price" value=""> | ||
</span> | ||
</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
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.
🛠️ Refactor suggestion
Avoid Direct Use of
$_REQUEST
and Suppressing PHPCS WarningsDirectly accessing
$_REQUEST
and ignoring PHPCS warnings is discouraged. Instead, sanitize and validate the input properly without suppressing coding standards.Refactor to sanitize inputs and adhere to coding standards: