Advertisement
palsushobhan

deduct-coupon-percent-for-fixed-commission

Mar 3rd, 2022
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.85 KB | None | 0 0
  1. add_filter('wcfmmp_order_item_commission', function( $item_commission, $vendor_id, $product_id, $variation_id, $item_price, $quantity, $commission_rule, $order_id ) {
  2.     if( isset( $commission_rule['mode'] ) && $commission_rule['mode'] == 'fixed' ) {
  3.         $order = wc_get_order($order_id);
  4.         $items = $order->get_items( 'line_item' );
  5.         $item_total = 0;
  6.         $item_subtotal = 0;
  7.         if( !empty( $items ) ) {
  8.             foreach( $items as $item_id => $item ) {
  9.                 $line_item = new WC_Order_Item_Product( $item );
  10.                 $item_product_id = $line_item->get_product_id();
  11.                 $item_variation_id = $line_item->get_variation_id();
  12.                 if( $item_variation_id && $item_variation_id != $variation_id || $item_product_id != $product_id ) continue;
  13.                 $item_line_item = $line_item;
  14.                 $item_total = $item_line_item->get_total();
  15.                 $item_subtotal = $item_line_item->get_subtotal();
  16.                 if($item_total == $item_subtotal) {
  17.                     return $item_commission; //if no coupon applied commission unchanged
  18.                 }
  19.                 break;
  20.             }
  21.         }
  22.         if( apply_filters( 'wcfmmp_is_allow_commission_fixed_per_unit', true ) ) {
  23.             $item_commission = (float) $commission_rule['fixed'] * $quantity;
  24.         } else {
  25.             $item_commission = (float) $commission_rule['fixed'];
  26.         }
  27.         $item_commission = ( $item_commission * $item_total ) / $item_subtotal;
  28.         if( (float) $item_price < (float) $item_commission ) {
  29.             $item_commission = $item_price;
  30.         }
  31.         $admin_fee_mode = apply_filters( 'wcfm_is_admin_fee_mode', false );
  32.         if( $admin_fee_mode ) {
  33.             $item_commission = (float) $item_price - (float) $item_commission;
  34.         }
  35.     }
  36.     return $item_commission;
  37. }, 10, 8);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement