Home / Admin / Override the commission calculation priority order
Duplicate Snippet

Embed Snippet on Your Site

Override the commission calculation priority order

Code Preview
php
<?php
/**
  * Given the provided list of vendor_ids in the vendors array, override the commission to use the vendor store commission.
  * Requires: WC Vendors Pro 1.8.6 or above. 
 */
add_filter( 'wcvendors_commission_args', 'wcv_store_vendor_override', 10, 4 );
function wcv_store_vendor_override(  $commission_args, $product_id, $product_price, $vendor_id ){
	
	$vendors = array( 3 );
	$vendor_id             = get_post_field( 'post_author', $product_id );
	// If the vendor is in the list, override the commission args to always be the vendor's store commission rate.
	if ( in_array( $vendor_id, $vendors ) ){ 
		$product                  = wc_get_product( $product_id );
		$store_commission_type = get_user_meta( $vendor_id, '_wcv_commission_type', true );
		$product_sales   = $product->get_total_sales();
		if ( in_array( $store_commission_type, array( 'product_sales', 'vendor_sales', 'product_price' ), true ) ) {
			if ( 'product_sales' === $store_commission_type ) {
				$tier = wcv_commission_get_tier( $product_sales, 'vendor', 'product_sales', $vendor_id );
			} elseif ( 'vendor_sales' === $store_commission_type ) {
				$vendor_sales = WCVendors_Pro_Vendor_Controller::get_vendor_sales_count( $vendor_id );
				$tier         = wcv_commission_get_tier( $vendor_sales, 'vendor', 'vendor_sales', $vendor_id );
			} else {
				$tier = wcv_commission_get_tier( $product_price, 'vendor', 'product_price', $vendor_id );
			}
			$commission_args['type']    = $tier['type'];
			$commission_args['percent'] = (float) $tier['percent'];
			$commission_args['amount']  = (float) $tier['amount'];
			$commission_args['fee']     = (float) $tier['fee'];
		} else {
			$commission_args['type']    = $store_commission_type;
			$commission_args['percent'] = (float) get_user_meta( $vendor_id, '_wcv_commission_percent', true );
			$commission_args['amount']  = (float) get_user_meta( $vendor_id, '_wcv_commission_amount', true );
			$commission_args['fee']     = (float) get_user_meta( $vendor_id, '_wcv_commission_fee', true );
		}
	}
	return $commission_args;
}

Comments

Add a Comment