Skip to content

Commit

Permalink
fix(commission): Price earning suggession in product edit page
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbir1991 committed Apr 22, 2018
1 parent 2e6f41f commit e419d5f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 30 deletions.
36 changes: 31 additions & 5 deletions assets/js/dokan.js
Original file line number Diff line number Diff line change
Expand Up @@ -1321,37 +1321,63 @@ jQuery(function($) {
});

function dokan_show_earning_suggestion() {
if ( dokan.commission_type == 'percentage' ) {
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() * dokan.vendor_percentage ) / 100 ), dokan.rounding_precision, '' ) )
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() * dokan.vendor_percentage ) / 100 ), dokan.rounding_precision, '' ) )
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() - dokan.vendor_percentage ), dokan.rounding_precision, '' ) )
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() - dokan.vendor_percentage ), dokan.rounding_precision, '' ) )
parseFloat( accounting.formatNumber( ( $( 'input.dokan-product-sales-price' ).val() - vendor_percentage ), dokan.rounding_precision, '' ) )
.toString()
.replace( '.', dokan.mon_decimal_point )
);
}
}
}

$('select#product_cat').on('change', function() {
dokan_show_earning_suggestion();

if ( $( '#product_type' ).val() == 'variable' ) {
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' );
}
});

//product popup product price error notice
$( 'body' ).on( 'keyup', 'input.dokan-product-regular-price, input.dokan-product-sales-price', function() {
dokan_show_earning_suggestion();
Expand Down
36 changes: 31 additions & 5 deletions assets/src/js/product-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,37 +864,63 @@
});

function dokan_show_earning_suggestion() {
if ( dokan.commission_type == 'percentage' ) {
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() * dokan.vendor_percentage ) / 100 ), dokan.rounding_precision, '' ) )
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() * dokan.vendor_percentage ) / 100 ), dokan.rounding_precision, '' ) )
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() - dokan.vendor_percentage ), dokan.rounding_precision, '' ) )
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() - dokan.vendor_percentage ), dokan.rounding_precision, '' ) )
parseFloat( accounting.formatNumber( ( $( 'input.dokan-product-sales-price' ).val() - vendor_percentage ), dokan.rounding_precision, '' ) )
.toString()
.replace( '.', dokan.mon_decimal_point )
);
}
}
}

$('select#product_cat').on('change', function() {
dokan_show_earning_suggestion();

if ( $( '#product_type' ).val() == 'variable' ) {
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' );
}
});

//product popup product price error notice
$( 'body' ).on( 'keyup', 'input.dokan-product-regular-price, input.dokan-product-sales-price', function() {
dokan_show_earning_suggestion();
Expand Down
52 changes: 36 additions & 16 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ function dokan_get_seller_earnings_by_order( $order, $seller_id ) {
*
* @return int
*/
function dokan_get_seller_percentage( $seller_id = 0, $product_id = 0 ) {
function dokan_get_seller_percentage( $seller_id = 0, $product_id = 0, $category_id = 0 ) {

// Seller will get 100 percent if ( any_input_val < 0 || percentage_input_val > 100 )
$commission_val = 100;
Expand Down Expand Up @@ -466,11 +466,11 @@ function dokan_get_seller_percentage( $seller_id = 0, $product_id = 0 ) {
if ( $product_id ) {

//category wise percentage
$category_commission = dokan_get_category_wise_seller_commission( $product_id );
$category_commission = dokan_get_category_wise_seller_commission( $product_id, $category_id );

if ( $category_commission != '' && is_numeric( $category_commission ) && $category_commission >= 0 ) {

$category_commission_type = dokan_get_category_wise_seller_commission_type( $product_id );
$category_commission_type = dokan_get_category_wise_seller_commission_type( $product_id, $category_id );

if ( 'percentage' == $category_commission_type ) {

Expand Down Expand Up @@ -516,22 +516,28 @@ function dokan_get_seller_percentage( $seller_id = 0, $product_id = 0 ) {
*
* @return String $type
*/
function dokan_get_commission_type( $seller_id = 0, $product_id = 0 ) {
function dokan_get_commission_type( $seller_id = 0, $product_id = 0, $category_id = 0 ) {
//return product wise percentage
if ( $product_id ) {

$_per_product_commission = get_post_meta( $product_id, '_per_product_admin_commission', true );
if ( $_per_product_commission != '' ) {
$type = get_post_meta( $product_id, '_per_product_admin_commission_type', true );
$type = empty( $type ) ? 'percentage' : $type;
return $type;

if ( 'flat' == $type || ( 'percentage' == $type && $_per_product_commission <= 100 ) ) {
return $type;
}
}

$category_commission = dokan_get_category_wise_seller_commission( $product_id );
if ( !empty( $category_commission ) ) {
$type = dokan_get_category_wise_seller_commission_type( $product_id );
$category_commission = dokan_get_category_wise_seller_commission( $product_id, $category_id );
if ( !empty( $category_commission ) && $category_commission ) {
$type = dokan_get_category_wise_seller_commission_type( $product_id, $category_id );
$type = empty( $type ) ? 'percentage' : $type;
return $type;

if ( 'flat' == $type || ( 'percentage' == $type && $category_commission <= 100 ) ) {
return $type;
}
}
}

Expand All @@ -541,7 +547,10 @@ function dokan_get_commission_type( $seller_id = 0, $product_id = 0 ) {
if ( $admin_commission != '' ) {
$type = get_user_meta( $seller_id, 'dokan_admin_percentage_type', true );
$type = empty( $type ) ? 'percentage' : $type;
return $type;

if ( 'flat' == $type || ( 'percentage' == $type && $admin_commission <= 100 ) ) {
return $type;
}
}
}

Expand Down Expand Up @@ -2259,14 +2268,21 @@ function dokan_set_is_home_false_on_store() {
* @return int $commission_rate
*
*/
function dokan_get_category_wise_seller_commission( $product_id ){
function dokan_get_category_wise_seller_commission( $product_id, $category_id = 0 ){

$terms = get_the_terms( $product_id, 'product_cat' );
$term_id = $terms[0]->term_id;

$category_commision = null;

if ( $category_id ) {
$terms = get_term( $category_id );
$term_id = $terms->term_id;
}

if ( $terms ) {
if ( 1 == count( $terms ) ) {
$category_commision = get_woocommerce_term_meta( $terms[0]->term_id, 'per_category_admin_commission', true );
$category_commision = get_woocommerce_term_meta( $term_id, 'per_category_admin_commission', true );
}
}

Expand All @@ -2287,22 +2303,26 @@ function dokan_get_category_wise_seller_commission( $product_id ){
* @return int $commission_rate
*
*/
function dokan_get_category_wise_seller_commission_type( $product_id ){
function dokan_get_category_wise_seller_commission_type( $product_id, $category_id = 0 ){

$terms = get_the_terms( $product_id, 'product_cat' );
$term_id = $terms[0]->term_id;
$category_commision = '';

if ( $category_id ) {
$terms = get_term( $category_id );
$term_id = $terms->term_id;
}

if ( $terms ) {
if ( 1 == count( $terms ) ) {
$category_commision = get_woocommerce_term_meta( $terms[0]->term_id, 'per_category_admin_commission_type', true );
$category_commision = get_woocommerce_term_meta( $term_id, 'per_category_admin_commission_type', true );
}
}

return $category_commision;
}

//Dokan Cache helper functions

/**
* Keep record of keys by group name
*
Expand Down
32 changes: 30 additions & 2 deletions lib/class.category-walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
include_once DOKAN_LIB_DIR.'/class.taxonomy-walker.php';

class DokanCategoryWalker extends DokanTaxonomyWalker {


private $post_id = '';

function __construct( $post_id ) {
// fetch the list of term ids for the given post
$this->post_id = $post_id;
}

/**
* @see Walker::$db_fields
* @var array
*/
var $db_fields = array('parent' => 'parent', 'id' => 'term_id');


function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {

$commission_val = dokan_get_seller_percentage( dokan_get_current_user_id(), $this->post_id, $category->term_id );
$commission_type = dokan_get_commission_type( dokan_get_current_user_id(), $this->post_id, $category->term_id );

$cat_name = apply_filters( 'list_cats', $category->name, $category );
$output .= "\t<option class=\"level-$depth\" value=\"" . $category->term_id . "\"";
$output .= ' data-commission="' . $commission_val . '" data-commission_type="' . $commission_type . '"';
if ( $category->term_id == $args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$output .= $cat_name;
$output .= "</option>\n";
}

}

2 changes: 1 addition & 1 deletion lib/class.tag-walker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
include_once DOKAN_LIB_DIR.'/class.taxonomy-walker.php';

class Dokan_Walker_Tag_Multi extends DokanTaxonomyWalker {

}

4 changes: 3 additions & 1 deletion templates/products/new-product-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@

<div class="content-half-part regular-price">
<label for="_regular_price" class="form-label"><?php _e( 'Price', 'dokan-lite' ); ?>
<span class="vendor-earning">( <?php _e( ' You Earn : ', 'dokan-lite' ) ?><?php echo get_woocommerce_currency_symbol() ?><span class="vendor-price">0.00</span> )</span>
<span class="vendor-earning" data-commission="<?php echo dokan_get_seller_percentage( dokan_get_current_user_id(), $post_id ); ?>" data-commission_type="<?php echo dokan_get_commission_type( dokan_get_current_user_id(), $post_id ); ?>">( <?php _e( ' You Earn : ', 'dokan-lite' ) ?><?php echo get_woocommerce_currency_symbol() ?><span class="vendor-price">0.00</span> )</span>
</label>
<div class="dokan-input-group">
<span class="dokan-input-group-addon"><?php echo get_woocommerce_currency_symbol(); ?></span>
Expand Down Expand Up @@ -267,6 +267,7 @@
if ( $term ) {
$product_cat = reset( $term );
}
include_once DOKAN_LIB_DIR.'/class.category-walker.php';

$category_args = array(
'show_option_none' => __( '- Select a category -', 'dokan-lite' ),
Expand All @@ -279,6 +280,7 @@
'class' => 'product_cat dokan-form-control dokan-select2',
'exclude' => '',
'selected' => $product_cat,
'walker' => new DokanCategoryWalker( $post_id )
);

wp_dropdown_categories( apply_filters( 'dokan_product_cat_dropdown_args', $category_args ) );
Expand Down

0 comments on commit e419d5f

Please sign in to comment.