palsushobhan

wcfm-commission-calculation-modifications.php

Jun 14th, 2021 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.33 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.             $commission_amount = $WCFMmp->wcfmmp_commission->wcfmmp_get_order_item_commission( $order_id, $vendor_id, $product_id, $variation_id, $item_price, $item_qty, $commission_rule );
  73.             $admin_commission_amount = $item_price - $commission_amount;
  74.  
  75.             wc_update_order_item_meta( $item_id, $commission_meta_key, $admin_commission_amount );
  76.  
  77.             return $admin_commission_amount;
  78.         }
  79.     }
  80.     return $commission_tax;
  81. }
  82.  
  83. //Commission tax only on admin commission + total transaction charge. Need to test when commission consider shipping and/or tax amount
  84. add_filter( 'wcfmmp_commission_deducted_tax', function( $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $total_commission, $commission_rule, $item_id ) {
  85.     global $WCFMmp;
  86.     if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
  87.         remove_filter( 'wcfmmp_commission_deducted_tax', array($WCFMmp->wcfmmp_commission, 'wcfmmp_commission_deducted_tax_admin_mode_handler' ), 100 );
  88.         $admin_commission_amount = calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule);
  89.         $admin_commission_amount += calculate_transaction_charge_for_vendor_order_item($item_id, $vendor_id, $commission_rule);
  90.         $commission_tax = $admin_commission_amount * ( (float)$commission_rule['tax_percent'] / 100 );
  91.     }
  92.     return $commission_tax;
  93. }, 99, 8 );
  94.  
  95. //Delete order meta keys on Order reset
  96. add_action( 'wcfm_manual_order_processed', function( $order_id, $order_posted, $order = '' ) {
  97.     global $wpdb;
  98.     if ( ! $order_id ) return;
  99.     if ( ! $order ) $order = wc_get_order( $order_id );
  100.     if ( ! is_a( $order, 'WC_Order' ) ) return;
  101.  
  102.     $marketplace_orders = $wpdb->get_results(  $wpdb->prepare( "SELECT ID, item_id from {$wpdb->prefix}wcfm_marketplace_orders WHERE order_id = %d", $order_id ) );
  103.     foreach( $marketplace_orders as $marketplace_order ) {
  104.         wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_vendor_only_transaction_charge' );
  105.         wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_commission_tax_only_on_admin_commission' );
  106.     }
  107. }, 10, 3 );
  108.  
Add Comment
Please, Sign In to add comment