Advertisement
palsushobhan

wcfm-commission-calculation-modifications-v2.php

Jun 14th, 2021 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.16 KB | None | 0 0
  1. //Work with WCFM marketplace v3.4.9 (latest) or later
  2. function calculate_transaction_charge_for_vendor_order_item($order_item_id, $vendor_id, $commission_rule) {
  3.     $transacton_charge_meta_key = '_wcfmmp_vendor_only_transaction_charge';
  4.     if($transaction_charge = wc_get_order_item_meta( $order_item_id, $transacton_charge_meta_key, true )) {
  5.         return $transaction_charge;
  6.     }
  7.    
  8.     $line_item = new WC_Order_Item_Product($order_item_id);
  9.     $line_item_total = $line_item->get_total() + $line_item->get_total_tax();
  10.     $order = $line_item->get_order();
  11.     $shipping_items = $order->get_items( 'shipping' );
  12.     $vendors = array();
  13.     $package_qty = 0;
  14.     foreach ( $shipping_items as $shipping_item_id => $shipping_item ) {
  15.         $order_item_shipping = new WC_Order_Item_Shipping( $shipping_item_id );
  16.         $shipping_vendor_id = $order_item_shipping->get_meta( 'vendor_id', true );
  17.         if($shipping_vendor_id && !in_array($shipping_vendor_id, $vendors)) {
  18.             $vendors[] = $shipping_vendor_id;
  19.         }
  20.         if ( ( $shipping_vendor_id > 0 ) && ( $shipping_vendor_id == $vendor_id ) ) {
  21.             $shipping_item_total = $order_item_shipping->get_total() + $order_item_shipping->get_total_tax();
  22.             $package_qty = $order_item_shipping->get_meta('package_qty', true);
  23.             $line_item_total += $shipping_item_total / $package_qty;
  24.         }
  25.     }
  26.  
  27.     $total_transaction_charge = 0;
  28.     if( ( $commission_rule['transaction_charge_type'] == 'percent' ) || ( $commission_rule['transaction_charge_type'] == 'percent_fixed' ) ) {
  29.         $total_transaction_charge  += $line_item_total * ( (float)$commission_rule['transaction_charge_percent'] / 100 );
  30.     }
  31.     if( ( $commission_rule['transaction_charge_type'] == 'fixed' ) || ( $commission_rule['transaction_charge_type'] == 'percent_fixed' ) ) {
  32.         $total_transaction_charge  += (float)$commission_rule['transaction_charge_fixed'] / ( count($vendors) * $package_qty );
  33.     }
  34.     $transaction_charge = round( $total_transaction_charge, 2 );
  35.    
  36.     if( !get_post_meta( $order->get_id(), '_wcfmmp_vendor_transacton_charge_adjusted_'.$vendor_id, true ) ) {
  37.         update_post_meta( $order->get_id(), '_wcfmmp_vendor_transacton_charge_adjusted_'.$vendor_id, 'yes' );
  38.     }
  39.  
  40.     wc_update_order_item_meta( $order_item_id, $transacton_charge_meta_key, $transaction_charge );
  41.     return $transaction_charge;
  42. }
  43.  
  44. //Transaction charge fully bear by vendor. Admin will not contribute
  45. add_filter( 'wcfmmp_commission_deducted_transaction_charge', function($transaction_charge, $vendor_id, $product_id, $order_id, $total_commission, $commission_rule, $order_item_id ) {
  46.     return calculate_transaction_charge_for_vendor_order_item($order_item_id, $vendor_id, $commission_rule);
  47. }, 10, 7 );
  48.  
  49. function calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule) {
  50.     global $WCFMmp;
  51.     if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {        
  52.         $commission_meta_key = '_wcfmmp_commission_tax_only_on_admin_commission';
  53.         if($item_commission_tax = wc_get_order_item_meta( $item_id, $commission_meta_key, true )) {
  54.             return $item_commission_tax;
  55.         }
  56.  
  57.         if( isset( $commission_rule['tax_enable'] ) && ( $commission_rule['tax_enable'] == 'yes' ) ) {
  58.             $line_item = new WC_Order_Item_Product($item_id);
  59.             $order = $line_item->get_order();
  60.             $quantity = $line_item->get_quantity();
  61.             $refunded_qty = $item_price = $item_qty = 0;
  62.             if ( $refunded_amount = $order->get_total_refunded_for_item( absint( $item_id ) ) ) {
  63.                 $refunded_qty = $order->get_qty_refunded_for_item( absint( $item_id ) );
  64.                 $refunded_qty = $refunded_qty * -1;
  65.             }
  66.             $item_qty = $quantity - $refunded_qty;
  67.             if( $WCFMmp->wcfmmp_vendor->is_vendor_deduct_discount( $vendor_id, $order_id ) ) {
  68.                 $item_price = $line_item->get_total() - $refunded_amount;
  69.             } else {
  70.                 $item_price = $line_item->get_subtotal() - $refunded_amount;
  71.             }
  72.             if( apply_filters( 'wcfmmp_is_allow_commission_on_tax', false ) ) {
  73.                 $item_price += $line_item->get_total_tax();
  74.             }
  75.             if( apply_filters( 'wcfmmp_is_allow_commission_on_shipping', false ) ) {
  76.                 $shipping_items = $order->get_items( 'shipping' );
  77.                 $package_qty = 0;
  78.                 foreach ( $shipping_items as $shipping_item_id => $shipping_item ) {
  79.                     $order_item_shipping = new WC_Order_Item_Shipping( $shipping_item_id );
  80.                     $shipping_vendor_id = $order_item_shipping->get_meta( 'vendor_id', true );
  81.                     if ( ( $shipping_vendor_id > 0 ) && ( $shipping_vendor_id == $vendor_id ) ) {
  82.                         $shipping_item_total = $order_item_shipping->get_total() + $order_item_shipping->get_total_tax();
  83.                         $package_qty = $order_item_shipping->get_meta('package_qty', true);
  84.                         $item_price += $shipping_item_total / $package_qty;
  85.                     }
  86.                 }
  87.             }
  88.             $commission_amount = $WCFMmp->wcfmmp_commission->wcfmmp_get_order_item_commission( $order_id, $vendor_id, $product_id, $variation_id, $item_price, $item_qty, $commission_rule );
  89.             $admin_commission_amount = $item_price - $commission_amount;
  90.  
  91.             wc_update_order_item_meta( $item_id, $commission_meta_key, $admin_commission_amount );
  92.  
  93.             return $admin_commission_amount;
  94.         }
  95.     }
  96.     return $commission_tax;
  97. }
  98.  
  99. //Commission tax only on admin commission
  100. add_filter( 'wcfmmp_commission_deducted_tax', function( $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $total_commission, $commission_rule, $item_id ) {
  101.     global $WCFMmp;
  102.     if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
  103.         remove_filter( 'wcfmmp_commission_deducted_tax', array($WCFMmp->wcfmmp_commission, 'wcfmmp_commission_deducted_tax_admin_mode_handler' ), 100 );
  104.         $admin_commission_amount = calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule);
  105.         $commission_tax = $admin_commission_amount * ( (float)$commission_rule['tax_percent'] / 100 );
  106.     }
  107.     return $commission_tax;
  108. }, 99, 8 );
  109.  
  110. //Delete order meta keys on Order reset
  111. add_action( 'wcfm_manual_order_processed', function( $order_id, $order_posted, $order = '' ) {
  112.     global $wpdb;
  113.     if ( ! $order_id ) return;
  114.     if ( ! $order ) $order = wc_get_order( $order_id );
  115.     if ( ! is_a( $order, 'WC_Order' ) ) return;
  116.  
  117.     $marketplace_orders = $wpdb->get_results(  $wpdb->prepare( "SELECT ID, item_id from {$wpdb->prefix}wcfm_marketplace_orders WHERE order_id = %d", $order_id ) );
  118.     foreach( $marketplace_orders as $marketplace_order ) {
  119.         wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_vendor_only_transaction_charge' );
  120.         wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_commission_tax_only_on_admin_commission' );
  121.     }
  122. }, 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement