Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function calculate_transaction_charge_for_vendor_order_item($order_item_id, $vendor_id, $commission_rule) {
- $transacton_charge_meta_key = '_wcfmmp_vendor_only_transaction_charge';
- if($transaction_charge = wc_get_order_item_meta( $order_item_id, $transacton_charge_meta_key, true )) {
- return $transaction_charge;
- }
- $line_item = new WC_Order_Item_Product($order_item_id);
- $line_item_total = $line_item->get_total() + $line_item->get_total_tax();
- $order = $line_item->get_order();
- $line_items = $order->get_items( 'line_item' );
- $vendors = array();
- foreach ($line_items as $item) {
- $order_item = new WC_Order_Item_Product($item);
- $product_id = $order_item->get_product_id();
- $store_id = wcfm_get_vendor_id_by_post($product_id);
- if ($store_id && !in_array($store_id, $vendors)) {
- $vendors[] = $store_id;
- }
- }
- $shipping_items = $order->get_items( 'shipping' );
- $package_qty = 1;
- foreach ( $shipping_items as $shipping_item ) {
- $order_item_shipping = new WC_Order_Item_Shipping( $shipping_item );
- $shipping_vendor_id = $order_item_shipping->get_meta( 'vendor_id', true );
- if ( ( $shipping_vendor_id > 0 ) && ( $shipping_vendor_id == $vendor_id ) ) {
- $shipping_item_total = $order_item_shipping->get_total() + $order_item_shipping->get_total_tax();
- $package_qty = $order_item_shipping->get_meta('package_qty', true);
- if($package_qty > 0 && $shipping_item_total > 0) {
- $line_item_total += $shipping_item_total / $package_qty;
- }
- }
- }
- $total_transaction_charge = 0;
- if( ( $commission_rule['transaction_charge_type'] == 'percent' ) || ( $commission_rule['transaction_charge_type'] == 'percent_fixed' ) ) {
- $total_transaction_charge += $line_item_total * ( (float)$commission_rule['transaction_charge_percent'] / 100 );
- }
- if( ( $commission_rule['transaction_charge_type'] == 'fixed' ) || ( $commission_rule['transaction_charge_type'] == 'percent_fixed' ) ) {
- $total_transaction_charge += (float)$commission_rule['transaction_charge_fixed'] / ( count($vendors) * $package_qty );
- }
- $transaction_charge = round( $total_transaction_charge, 2 );
- if( !$order->get_meta('_wcfmmp_vendor_transacton_charge_adjusted_' . $vendor_id) ) {
- $order->update_meta_data( '_wcfmmp_vendor_transacton_charge_adjusted_'.$vendor_id, 'yes' );
- $order->save();
- }
- wc_update_order_item_meta( $order_item_id, $transacton_charge_meta_key, $transaction_charge );
- return $transaction_charge;
- }
- //Transaction charge fully bear by vendor. Admin will not contribute
- add_filter( 'wcfmmp_commission_deducted_transaction_charge', function($transaction_charge, $vendor_id, $product_id, $order_id, $total_commission, $commission_rule, $order_item_id ) {
- return calculate_transaction_charge_for_vendor_order_item($order_item_id, $vendor_id, $commission_rule);
- }, 10, 7 );
- //Delete order meta keys on Order reset
- add_action( 'wcfm_manual_order_processed', function( $order_id, $order_posted, $order = '' ) {
- global $wpdb;
- if ( ! $order_id ) return;
- if ( ! $order ) $order = wc_get_order( $order_id );
- if ( ! is_a( $order, 'WC_Order' ) ) return;
- $marketplace_orders = $wpdb->get_results( $wpdb->prepare( "SELECT ID, item_id from {$wpdb->prefix}wcfm_marketplace_orders WHERE order_id = %d", $order_id ) );
- foreach( $marketplace_orders as $marketplace_order ) {
- wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_vendor_only_transaction_charge' );
- }
- }, 10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement