Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Work with WCFM marketplace v3.4.9 (latest) or later
- function calculate_transaction_charge_for_vendor_order_item($order_item_id, $vendor_id, $commission_rule) {
- $transacton_charge_meta_key = '_wcfmmp_vendor_only_transaction_charge';
- if($transaction_charge = wc_get_order_item_meta( $order_item_id, $transacton_charge_meta_key, true )) {
- return $transaction_charge;
- }
- $line_item = new WC_Order_Item_Product($order_item_id);
- $line_item_total = $line_item->get_total() + $line_item->get_total_tax();
- $order = $line_item->get_order();
- $shipping_items = $order->get_items( 'shipping' );
- $vendors = array();
- $package_qty = 0;
- foreach ( $shipping_items as $shipping_item_id => $shipping_item ) {
- $order_item_shipping = new WC_Order_Item_Shipping( $shipping_item_id );
- $shipping_vendor_id = $order_item_shipping->get_meta( 'vendor_id', true );
- if($shipping_vendor_id && !in_array($shipping_vendor_id, $vendors)) {
- $vendors[] = $shipping_vendor_id;
- }
- if ( ( $shipping_vendor_id > 0 ) && ( $shipping_vendor_id == $vendor_id ) ) {
- $shipping_item_total = $order_item_shipping->get_total() + $order_item_shipping->get_total_tax();
- $package_qty = $order_item_shipping->get_meta('package_qty', true);
- $line_item_total += $shipping_item_total / $package_qty;
- }
- }
- $total_transaction_charge = 0;
- if( ( $commission_rule['transaction_charge_type'] == 'percent' ) || ( $commission_rule['transaction_charge_type'] == 'percent_fixed' ) ) {
- $total_transaction_charge += $line_item_total * ( (float)$commission_rule['transaction_charge_percent'] / 100 );
- }
- if( ( $commission_rule['transaction_charge_type'] == 'fixed' ) || ( $commission_rule['transaction_charge_type'] == 'percent_fixed' ) ) {
- $total_transaction_charge += (float)$commission_rule['transaction_charge_fixed'] / ( count($vendors) * $package_qty );
- }
- $transaction_charge = round( $total_transaction_charge, 2 );
- if( !get_post_meta( $order->get_id(), '_wcfmmp_vendor_transacton_charge_adjusted_'.$vendor_id, true ) ) {
- update_post_meta( $order->get_id(), '_wcfmmp_vendor_transacton_charge_adjusted_'.$vendor_id, 'yes' );
- }
- wc_update_order_item_meta( $order_item_id, $transacton_charge_meta_key, $transaction_charge );
- return $transaction_charge;
- }
- //Transaction charge fully bear by vendor. Admin will not contribute
- add_filter( 'wcfmmp_commission_deducted_transaction_charge', function($transaction_charge, $vendor_id, $product_id, $order_id, $total_commission, $commission_rule, $order_item_id ) {
- return calculate_transaction_charge_for_vendor_order_item($order_item_id, $vendor_id, $commission_rule);
- }, 10, 7 );
- function calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule) {
- global $WCFMmp;
- if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
- $commission_meta_key = '_wcfmmp_commission_tax_only_on_admin_commission';
- if($item_commission_tax = wc_get_order_item_meta( $item_id, $commission_meta_key, true )) {
- return $item_commission_tax;
- }
- if( isset( $commission_rule['tax_enable'] ) && ( $commission_rule['tax_enable'] == 'yes' ) ) {
- $line_item = new WC_Order_Item_Product($item_id);
- $order = $line_item->get_order();
- $quantity = $line_item->get_quantity();
- $refunded_qty = $item_price = $item_qty = 0;
- if ( $refunded_amount = $order->get_total_refunded_for_item( absint( $item_id ) ) ) {
- $refunded_qty = $order->get_qty_refunded_for_item( absint( $item_id ) );
- $refunded_qty = $refunded_qty * -1;
- }
- $item_qty = $quantity - $refunded_qty;
- if( $WCFMmp->wcfmmp_vendor->is_vendor_deduct_discount( $vendor_id, $order_id ) ) {
- $item_price = $line_item->get_total() - $refunded_amount;
- } else {
- $item_price = $line_item->get_subtotal() - $refunded_amount;
- }
- $commission_amount = $WCFMmp->wcfmmp_commission->wcfmmp_get_order_item_commission( $order_id, $vendor_id, $product_id, $variation_id, $item_price, $item_qty, $commission_rule );
- $admin_commission_amount = $item_price - $commission_amount;
- wc_update_order_item_meta( $item_id, $commission_meta_key, $admin_commission_amount );
- return $admin_commission_amount;
- }
- }
- return $commission_tax;
- }
- //Commission tax only on admin commission + total transaction charge. Need to test when commission consider shipping and/or tax amount
- add_filter( 'wcfmmp_commission_deducted_tax', function( $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $total_commission, $commission_rule, $item_id ) {
- global $WCFMmp;
- if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
- remove_filter( 'wcfmmp_commission_deducted_tax', array($WCFMmp->wcfmmp_commission, 'wcfmmp_commission_deducted_tax_admin_mode_handler' ), 100 );
- $admin_commission_amount = calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule);
- $admin_commission_amount += calculate_transaction_charge_for_vendor_order_item($item_id, $vendor_id, $commission_rule);
- $commission_tax = $admin_commission_amount * ( (float)$commission_rule['tax_percent'] / 100 );
- }
- return $commission_tax;
- }, 99, 8 );
- //Delete order meta keys on Order reset
- add_action( 'wcfm_manual_order_processed', function( $order_id, $order_posted, $order = '' ) {
- global $wpdb;
- if ( ! $order_id ) return;
- if ( ! $order ) $order = wc_get_order( $order_id );
- if ( ! is_a( $order, 'WC_Order' ) ) return;
- $marketplace_orders = $wpdb->get_results( $wpdb->prepare( "SELECT ID, item_id from {$wpdb->prefix}wcfm_marketplace_orders WHERE order_id = %d", $order_id ) );
- foreach( $marketplace_orders as $marketplace_order ) {
- wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_vendor_only_transaction_charge' );
- wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_commission_tax_only_on_admin_commission' );
- }
- }, 10, 3 );
Add Comment
Please, Sign In to add comment