Skip to content

Commit

Permalink
feat: add dokan commission class (#582)
Browse files Browse the repository at this point in the history
* feat: add few combine commission fields

* feat: add dokan commission class

* feat: add combine commission type

* refactor: category was earning method

* refactor: combine commission description

* refactor: deprecate dokan_get_earning_by_product_function

* refactor: deprecate dokan_get_earning_by_product_function is fixed #568

* refactor: update deprecated functions

* refactor: calculate_commission method

* refactor: vendor earning suggestion on product edit

* refactor: check if method is callable before calling it

* refactor: deprecate seller_id paramter from dokan_get_variable_product_earning function

* refactor: increase commission by order item quantity

* fix: return variable product earning

* feat: add dokan commission test class

* fix: remove normal commission field if combine commission is found

* refactor: return additional_fee from the api response

* feat: add earning column in order listing page

* refactor: use DOKAN_LITE_SINCE instead of hardcoded version and line indentation

* refactor: return earning for an order from the order_table

* refactor: increase admin commission by order quantity for flat rate

* feat: add set order_id and quantity method

* fix: refund calculation for flat commission

* fix: save commission rate, type, fee in order line_items instead of order

* refactor: make Dokan_Commission static to non-static class

* fix: reset static value after each iteration

* if _dokan_admin_fee is found in a order meta, return the saved commission to keep backward compatability

* refactor: get saved shipping and tax fee recipient

* refactor: dokan_get_admin_commission_by function

* fix: hide extra meta data

* refactor: add a hook before calculation

* refactor: ensure the product price is float

* refactor: add backward compatibility when rendering variable product earning
  • Loading branch information
saimonh3 authored and sabbir1991 committed Sep 19, 2019
1 parent 5feaec0 commit 153066f
Show file tree
Hide file tree
Showing 18 changed files with 1,492 additions and 141 deletions.
114 changes: 33 additions & 81 deletions assets/src/js/product-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,96 +838,48 @@
return false;
});

function dokan_show_earning_suggestion() {
var selectedCategoryWrapper = $('select#product_cat').find('option:selected');

if ( selectedCategoryWrapper.data( 'commission' ) != '' ) {
var vendor_percentage = selectedCategoryWrapper.data( 'commission' );
var commission_type = selectedCategoryWrapper.data( 'commission_type' );
} else {
var commission_type = $('span.vendor-earning').attr( 'data-commission_type' );
var vendor_percentage = $('span.vendor-earning').attr( 'data-commission' );
}

if ( commission_type == 'percentage' ) {
if ( $('input.dokan-product-sales-price' ).val() == '' ) {
$( 'span.vendor-price' ).html(
parseFloat( accounting.formatNumber( ( ( $( 'input.dokan-product-regular-price' ).val() * vendor_percentage ) / 100 ), dokan.rounding_precision, '' ) )
.toString()
.replace( '.', dokan.mon_decimal_point )
);
} else {
$( 'span.vendor-price' ).html(
parseFloat( accounting.formatNumber( ( ( $( 'input.dokan-product-sales-price' ).val() * vendor_percentage ) / 100 ), dokan.rounding_precision, '' ) )
.toString()
.replace( '.', dokan.mon_decimal_point )
);
}
} else {
if ( $('input.dokan-product-sales-price' ).val() == '' ) {
$( 'span.vendor-price' ).html(
parseFloat( accounting.formatNumber( ( $( 'input.dokan-product-regular-price' ).val() - vendor_percentage ), dokan.rounding_precision, '' ) )
.toString()
.replace( '.', dokan.mon_decimal_point )
);
} else {
$( 'span.vendor-price' ).html(
parseFloat( accounting.formatNumber( ( $( 'input.dokan-product-sales-price' ).val() - vendor_percentage ), dokan.rounding_precision, '' ) )
.toString()
.replace( '.', dokan.mon_decimal_point )
);
function dokan_show_earning_suggestion( callback ) {
let commission = $('span.vendor-earning').attr( 'data-commission' );
let product_id = $( 'span.vendor-earning' ).attr( 'data-product-id' );
let product_price = $( 'input.dokan-product-regular-price' ).val();
let sale_price = $( 'input.dokan-product-sales-price' ).val();
let earning_suggestion = $('.simple-product span.vendor-price');

earning_suggestion.html( 'Calculating' );

$.get( dokan.ajaxurl, {
action: 'get_vendor_earning',
product_id: product_id,
product_price: product_price,
product_price: sale_price ? sale_price : product_price,
_wpnonce: dokan.nonce
} )
.done( ( response ) => {
earning_suggestion.html( response );

if ( typeof callback === 'function' ) {
callback();
}
}
} );
}

$('select#product_cat').on('change', function() {
dokan_show_earning_suggestion();
$( "input.dokan-product-regular-price, input.dokan-product-sales-price" ).on( 'keyup', _.debounce( () => {
dokan_show_earning_suggestion( function() {

if ( $( '#product_type' ).val() == 'variable' || $( '#product_type' ).val() == undefined ) {
return;
}

if ( Number( $('span.vendor-price').text() ) <= 0 ) {
$( 'input[type=submit]' ).attr( 'disabled', 'disabled' );
$( '.dokan-product-less-price-alert' ).removeClass( 'dokan-hide' );
} else {
$( 'input[type=submit]' ).removeAttr( 'disabled');
$( '.dokan-product-less-price-alert' ).addClass( 'dokan-hide' );
}
});

$( "input.dokan-product-regular-price, input.dokan-product-sales-price" ).on( 'keyup', function () {
dokan_show_earning_suggestion();

if ( $( '#product_type' ).val() == 'simple' || $( '#product_type' ).text() == '' ) {
if ( Number( $('span.vendor-price').text() ) < 0 ) {
$( $('.dokan-product-less-price-alert').removeClass('dokan-hide') );
$( 'input[type=submit]' ).attr( 'disabled', 'disabled' );
$( 'button[type=submit]' ).attr( 'disabled', 'disabled' );
} else {
$( $('.dokan-product-less-price-alert').addClass('dokan-hide') );
$( 'input[type=submit]' ).removeAttr( 'disabled');
$( 'button[type=submit]' ).removeAttr( 'disabled');
}
}

$( '#product_type' ).on( 'change', function() {
var self = $(this);

if ( self.val() == 'variable' || self.val() == 'grouped' ) {
$( 'input[type=submit]' ).removeAttr( 'disabled' );
} else {
if ( Number( $('span.vendor-price').text() ) > 0 ) {
$( 'input[type=submit]' ).removeAttr( 'disabled');
$( '.dokan-product-less-price-alert' ).addClass( 'dokan-hide' );
} else {
$( '.dokan-product-less-price-alert' ).removeClass( 'dokan-hide ');
if ( $( '#product_type' ).val() == 'simple' || $( '#product_type' ).text() == '' ) {
if ( Number( $('.simple-product span.vendor-price').text() ) < 0 ) {
$( $('.dokan-product-less-price-alert').removeClass('dokan-hide') );
$( 'input[type=submit]' ).attr( 'disabled', 'disabled' );
$( 'button[type=submit]' ).attr( 'disabled', 'disabled' );
} else {
$( $('.dokan-product-less-price-alert').addClass('dokan-hide') );
$( 'input[type=submit]' ).removeAttr( 'disabled');
$( 'button[type=submit]' ).removeAttr( 'disabled');
}
}
} );

} ).trigger('keyup');
}, 750 ) );

});

Expand Down
11 changes: 6 additions & 5 deletions classes/admin-user-profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,19 @@ function add_meta_fields( $user ) {
<th><?php esc_html_e( 'Admin Commission Type ', 'dokan-lite' ); ?></th>
<td>
<select id="dokan_admin_percentage_type" name="dokan_admin_percentage_type">
<option value="percentage" <?php selected( $admin_commission_type, 'percentage' ); ?> ><?php esc_html_e( 'Percentage', 'dokan-lite' ); ?></option>
<option value="flat" <?php selected( $admin_commission_type, 'flat' ); ?>><?php esc_html_e( 'Flat', 'dokan-lite' ); ?></option>
<?php foreach ( dokan_commission_types() as $key => $value ) : ?>
<option value="<?php echo wc_clean( $key ); ?>" <?php selected( $admin_commission_type, $key ); ?>"><?php echo $value ?></option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e( 'Set the commmission type admin gets from this seller', 'dokan-lite' ); ?></p>
</td>
</tr>
<tr>
<th><?php esc_html_e( 'Admin Commission ', 'dokan-lite' ); ?></th>
<td>
<input type="text" class="small-text" name="dokan_admin_percentage" value="<?php echo esc_attr( $admin_commission ); ?>">

<p class="description"><?php esc_html_e( 'It will override the default commission admin gets from each sales', 'dokan-lite' ); ?></p>
<input type="number" min="0" class="small-text" id="admin-commission" name="dokan_admin_percentage" placeholder="<?php echo esc_attr( '10' ); ?>" value="<?php echo esc_attr( $admin_commission ); ?>">
<?php do_action( 'dokan_seller_meta_fields_after_admin_commission', $user ); ?>
<p class="combine-commission-description"><?php esc_html_e( 'It will override the default commission admin gets from each sales', 'dokan-lite' ); ?></p>
</td>
</tr>

Expand Down
27 changes: 27 additions & 0 deletions classes/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ function init_ajax() {

add_action( 'wp_ajax_nopriv_dokan_get_login_form', array( $this, 'get_login_form' ) );
add_action( 'wp_ajax_nopriv_dokan_login_user', array( $this, 'login_user' ) );
add_action( 'wp_ajax_get_vendor_earning', [ $this, 'get_vendor_earning' ] );
}

/**
Expand Down Expand Up @@ -911,4 +912,30 @@ public static function login_user() {

wp_send_json_success( $response );
}

/**
* Get vendor earning
*
* @since DOKAN_SINCE
*
* @return void
*/
public function get_vendor_earning() {
check_ajax_referer( 'dokan_reviews' );

$product_id = ! empty( $_GET['product_id'] ) ? absint( $_GET['product_id'] ) : 0;
$product_price = ! empty( $_GET['product_price'] ) ? (float) $_GET['product_price'] : 0;

$vendor = dokan_get_vendor_by_product( $product_id );

if ( ! $vendor ) {
return;
}

$vendor_id = $vendor->get_id();

$vendor_earning = dokan()->commission->calculate_commission( $product_id, $product_price, $vendor_id );

wp_send_json( $vendor_earning );
}
}
2 changes: 2 additions & 0 deletions dokan.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ function includes() {
require_once $inc_dir . 'class-order-manager.php';
require_once $inc_dir . 'class-product-manager.php';
require_once $inc_dir . 'class-dokan-privacy.php';
require_once $inc_dir . 'class-commission.php';

if ( is_admin() ) {
require_once $inc_dir . 'admin/class-settings.php';
Expand Down Expand Up @@ -394,6 +395,7 @@ function init_classes() {
$this->container['registration'] = new Dokan_Registration();
$this->container['orders'] = new Dokan_Order_Manager();
$this->container['api'] = new Dokan_API_Manager();
$this->container['commission'] = Dokan_Commission::instance();

$this->container = apply_filters( 'dokan_get_class_container', $this->container );

Expand Down
39 changes: 33 additions & 6 deletions includes/admin/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,9 @@ function get_settings_sections() {
* @return array settings fields
*/
function get_settings_fields() {
$pages_array = $this->get_post_type( 'page' );

$commission_types = array(
'flat' => __( 'Flat', 'dokan-lite' ),
'percentage' => __( 'Percentage', 'dokan-lite' ),
);
$pages_array = $this->get_post_type( 'page' );
$slider_array = $this->get_post_type( 'dokan_slider' );
$commission_types = dokan_commission_types();

$general_site_options = apply_filters( 'dokan_settings_general_site_options', array(
'site_options' => array(
Expand Down Expand Up @@ -382,6 +379,36 @@ function get_settings_fields() {
$general_site_options,
$general_vendor_store_options
),
'dokan_selling' => array(
'new_seller_enable_selling' => array(
'name' => 'new_seller_enable_selling',
'label' => __( 'New Vendor Product Upload', 'dokan-lite' ),
'desc' => __( 'Allow newly registered vendors to add products', 'dokan-lite' ),
'type' => 'checkbox',
'default' => 'on'
),
'order_status_change' => array(
'name' => 'order_status_change',
'label' => __( 'Order Status Change', 'dokan-lite' ),
'desc' => __( 'Vendor can update order status', 'dokan-lite' ),
'type' => 'checkbox',
'default' => 'on'
),
'disable_product_popup' => array(
'name' => 'disable_product_popup',
'label' => __( 'Disable Product Popup', 'dokan-lite' ),
'desc' => __( 'Disable add new product in popup view', 'dokan-lite' ),
'type' => 'checkbox',
'default' => 'off'
),
'disable_welcome_wizard' => array(
'name' => 'disable_welcome_wizard',
'label' => __( 'Disable Welcome Wizard', 'dokan-lite' ),
'desc' => __( 'Disable welcome wizard for newly registered vendors', 'dokan-lite' ),
'type' => 'checkbox',
'default' => 'off'
),
),
'dokan_selling' => array_merge(
$selling_option_commission,
$selling_option_vendor_capability
Expand Down
2 changes: 2 additions & 0 deletions includes/class-api-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ public function populate_admin_commission( $data, $store ) {
}

$commission = get_user_meta( $store_id, 'dokan_admin_percentage', true );
$additional_fee = get_user_meta( $store_id, 'dokan_admin_additional_fee', true );
$commission_type = get_user_meta( $store_id, 'dokan_admin_percentage_type', true );
$data['admin_commission'] = $commission;
$data['admin_additional_fee'] = $additional_fee;
$data['admin_commission_type'] = $commission_type;

return $data;
Expand Down
Loading

0 comments on commit 153066f

Please sign in to comment.