Advertisement
palsushobhan

commission-tax-on-admin-commission-and-shipping

Oct 17th, 2022
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.69 KB | None | 0 0
  1. add_filter( 'wcfmmp_commission_deducted_tax', function( $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $total_commission, $commission_rule ) {
  2.     global $WCFMmp;
  3.     if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
  4.         remove_filter( 'wcfmmp_commission_deducted_tax', array($WCFMmp->wcfmmp_commission, 'wcfmmp_commission_deducted_tax_admin_mode_handler' ), 100 );
  5.        
  6.         $commission_meta_key = '_wcfmmp_vendor_commission_tax_'.$vendor_id.'_'.$product_id;
  7.         if($variation_id) $commission_meta_key .= '_'.$variation_id;
  8.  
  9.         if($commission_tax = get_post_meta( $order_id, $commission_meta_key, true )) {
  10.             return $commission_tax;
  11.         }
  12.  
  13.         if( isset( $commission_rule['tax_enable'] ) && ( $commission_rule['tax_enable'] == 'yes' ) ) {
  14.             $order = wc_get_order( $order_id );
  15.             $admin_commission_amount = 0;
  16.             $items = $order->get_items('line_item');
  17.             foreach ($items as $item) {
  18.                 $line_item     = new WC_Order_Item_Product($item);
  19.                 $order_item_id = $item->get_id();
  20.                 $pproduct_id   = $line_item->get_product_id();
  21.                 $pvariation_id = $line_item->get_variation_id();
  22.                 $quantity      = $line_item->get_quantity();
  23.                 $pvendor_id = wcfm_get_vendor_id_by_post( $pproduct_id );
  24.                 if( ( $pvariation_id && $variation_id && ( $variation_id == $pvariation_id ) ) || ( !$pvariation_id && $pproduct_id && ( $product_id == $pproduct_id ) ) ) {
  25.                     if( $pvendor_id && ( $pvendor_id == $vendor_id ) ) {
  26.                         $refunded_qty = $item_price = $item_qty = 0;
  27.                         if ( $refunded_amount = $order->get_total_refunded_for_item( absint( $order_item_id ) ) ) {
  28.                             $refunded_qty = $order->get_qty_refunded_for_item( absint( $order_item_id ) );
  29.                             $refunded_qty = $refunded_qty * -1;
  30.                         }
  31.                         $item_qty = $quantity - $refunded_qty;
  32.                         if( $WCFMmp->wcfmmp_vendor->is_vendor_deduct_discount( $vendor_id, $order_id ) ) {
  33.                             $item_price = $line_item->get_total() - $refunded_amount;
  34.                         } else {
  35.                             $item_price = $line_item->get_subtotal() - $refunded_amount;
  36.                         }
  37.                         $commission_amount = $WCFMmp->wcfmmp_commission->wcfmmp_get_order_item_commission( $order_id, $vendor_id, $product_id, $variation_id, $item_price, $item_qty, $commission_rule );
  38.                         $admin_commission_amount += $item_price - $commission_amount;
  39.                     }
  40.                     break;
  41.                 }
  42.             }
  43.  
  44.             $shipping_items = $order->get_items('shipping');
  45.             foreach ($shipping_items as $shipping_item_id => $shipping_item) {
  46.                 $order_item_shipping = new WC_Order_Item_Shipping($shipping_item_id);
  47.                 $shipping_vendor_id  = $order_item_shipping->get_meta('vendor_id', true);
  48.                 $package_qty = $order_item_shipping->get_meta('package_qty', true);
  49.                 if( $shipping_vendor_id && $shipping_vendor_id == $vendor_id ) {
  50.                     $shipping_item_total = $order_item_shipping->get_total();
  51.                     $shipping_item_total = ($shipping_item_total/$package_qty) * $quantity ;
  52.                     $admin_commission_amount += $shipping_item_total;
  53.                 }
  54.             }
  55.            
  56.             $commission_tax = $admin_commission_amount * ( (float)$commission_rule['tax_percent'] / 100 );
  57.  
  58.             $commission_charge_processed = (array) get_post_meta($order_id, '_wcfmmp_vendors_commission_charge_meta_key', true);
  59.             $commission_charge_processed[] = $commission_meta_key;
  60.             update_post_meta( $order_id, '_wcfmmp_vendors_commission_charge_meta_key', $commission_charge_processed );
  61.  
  62.             update_post_meta( $order_id, $commission_meta_key, $commission_tax );
  63.         }
  64.     }
  65.     return $commission_tax;
  66. }, 99, 7 );
  67.  
  68. //Delete order meta keys on Order reset
  69. add_action( 'wcfm_manual_order_processed', function( $order_id, $order_posted, $order = '' ) {
  70.     if ( ! $order_id ) return;
  71.     if ( ! $order ) $order = wc_get_order( $order_id );
  72.     if ( ! is_a( $order, 'WC_Order' ) ) return;
  73.  
  74.     $commission_charge_processed = (array) get_post_meta($order_id, '_wcfmmp_vendors_commission_charge_meta_key', true);
  75.     foreach($commission_charge_processed as $commission_charge_meta_key) {
  76.         delete_post_meta( $order_id, $commission_charge_meta_key );
  77.     }
  78.     delete_post_meta( $order_id, '_wcfmmp_vendors_commission_charge_meta_key' );
  79. }, 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement