palsushobhan

commission-tax-on-admin-commission-only.php

Jun 25th, 2021 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. //Commission tax only on admin commission amount.
  2. add_filter( 'wcfmmp_commission_deducted_tax', function( $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $total_commission, $commission_rule, $item_id ) {
  3. global $WCFMmp;
  4. if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
  5. remove_filter( 'wcfmmp_commission_deducted_tax', array($WCFMmp->wcfmmp_commission, 'wcfmmp_commission_deducted_tax_admin_mode_handler' ), 100 );
  6. $admin_commission_amount = calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule);
  7. $commission_tax = $admin_commission_amount * ( (float)$commission_rule['tax_percent'] / 100 );
  8. }
  9. return $commission_tax;
  10. }, 99, 8 );
  11.  
  12. function calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule) {
  13. global $WCFMmp;
  14. if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
  15. $commission_meta_key = '_wcfmmp_commission_tax_only_on_admin_commission';
  16. if($item_commission_tax = wc_get_order_item_meta( $item_id, $commission_meta_key, true )) {
  17. return $item_commission_tax;
  18. }
  19.  
  20. if( isset( $commission_rule['tax_enable'] ) && ( $commission_rule['tax_enable'] == 'yes' ) ) {
  21. $line_item = new WC_Order_Item_Product($item_id);
  22. $order = $line_item->get_order();
  23. $quantity = $line_item->get_quantity();
  24. $refunded_qty = $item_price = $item_qty = 0;
  25. if ( $refunded_amount = $order->get_total_refunded_for_item( absint( $item_id ) ) ) {
  26. $refunded_qty = $order->get_qty_refunded_for_item( absint( $item_id ) );
  27. $refunded_qty = $refunded_qty * -1;
  28. }
  29. $item_qty = $quantity - $refunded_qty;
  30. if( $WCFMmp->wcfmmp_vendor->is_vendor_deduct_discount( $vendor_id, $order_id ) ) {
  31. $item_price = $line_item->get_total() - $refunded_amount;
  32. } else {
  33. $item_price = $line_item->get_subtotal() - $refunded_amount;
  34. }
  35. if( apply_filters( 'wcfmmp_is_allow_commission_on_tax', false ) ) {
  36. $item_price += $line_item->get_total_tax();
  37. }
  38. if( apply_filters( 'wcfmmp_is_allow_commission_on_shipping', false ) ) {
  39. $shipping_items = $order->get_items( 'shipping' );
  40. $package_qty = 1;
  41. foreach ( $shipping_items as $shipping_item_id => $shipping_item ) {
  42. $order_item_shipping = new WC_Order_Item_Shipping( $shipping_item_id );
  43. $shipping_vendor_id = $order_item_shipping->get_meta( 'vendor_id', true );
  44. if ( ( $shipping_vendor_id > 0 ) && ( $shipping_vendor_id == $vendor_id ) ) {
  45. $shipping_item_total = $order_item_shipping->get_total() + $order_item_shipping->get_total_tax();
  46. $package_qty = $order_item_shipping->get_meta('package_qty', true);
  47. $item_price += $shipping_item_total / $package_qty;
  48. }
  49. }
  50. }
  51. $commission_amount = $WCFMmp->wcfmmp_commission->wcfmmp_get_order_item_commission( $order_id, $vendor_id, $product_id, $variation_id, $item_price, $item_qty, $commission_rule );
  52. $admin_commission_amount = $item_price - $commission_amount;
  53.  
  54. wc_update_order_item_meta( $item_id, $commission_meta_key, $admin_commission_amount );
  55.  
  56. return $admin_commission_amount;
  57. }
  58. }
  59. return $commission_tax;
  60. }
  61.  
  62. //Delete order meta keys on Order reset
  63. add_action( 'wcfm_manual_order_processed', function( $order_id, $order_posted, $order = '' ) {
  64. global $wpdb;
  65. if ( ! $order_id ) return;
  66. if ( ! $order ) $order = wc_get_order( $order_id );
  67. if ( ! is_a( $order, 'WC_Order' ) ) return;
  68.  
  69. $marketplace_orders = $wpdb->get_results( $wpdb->prepare( "SELECT ID, item_id from {$wpdb->prefix}wcfm_marketplace_orders WHERE order_id = %d", $order_id ) );
  70. foreach( $marketplace_orders as $marketplace_order ) {
  71. wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_commission_tax_only_on_admin_commission' );
  72. }
  73. }, 10, 3 );
Add Comment
Please, Sign In to add comment