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 = 0;
- 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( !get_post_meta( $order->get_id(), '_wcfmmp_vendor_transacton_charge_adjusted_'.$vendor_id, true ) ) {
- update_post_meta( $order->get_id(), '_wcfmmp_vendor_transacton_charge_adjusted_'.$vendor_id, 'yes' );
- }
- 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 );
- function calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule) {
- global $WCFMmp;
- if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
- $commission_meta_key = '_wcfmmp_commission_tax_only_on_admin_commission';
- if($item_commission_tax = wc_get_order_item_meta( $item_id, $commission_meta_key, true )) {
- return $item_commission_tax;
- }
- if( isset( $commission_rule['tax_enable'] ) && ( $commission_rule['tax_enable'] == 'yes' ) ) {
- $line_item = new WC_Order_Item_Product($item_id);
- $order = $line_item->get_order();
- $quantity = $line_item->get_quantity();
- $refunded_qty = $item_price = $item_qty = 0;
- if ( $refunded_amount = $order->get_total_refunded_for_item( absint( $item_id ) ) ) {
- $refunded_qty = $order->get_qty_refunded_for_item( absint( $item_id ) );
- $refunded_qty = $refunded_qty * -1;
- }
- $item_qty = $quantity - $refunded_qty;
- if( $WCFMmp->wcfmmp_vendor->is_vendor_deduct_discount( $vendor_id, $order_id ) ) {
- $item_price = $line_item->get_total() - $refunded_amount;
- } else {
- $item_price = $line_item->get_subtotal() - $refunded_amount;
- }
- if( apply_filters( 'wcfmmp_is_allow_commission_on_tax', false ) ) {
- $item_price += $line_item->get_total_tax();
- }
- if( apply_filters( 'wcfmmp_is_allow_commission_on_shipping', false ) ) {
- $shipping_items = $order->get_items( 'shipping' );
- $package_qty = 0;
- foreach ( $shipping_items as $shipping_item_id => $shipping_item ) {
- $order_item_shipping = new WC_Order_Item_Shipping( $shipping_item_id );
- $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);
- $item_price += $shipping_item_total / $package_qty;
- }
- }
- }
- $commission_amount = $WCFMmp->wcfmmp_commission->wcfmmp_get_order_item_commission( $order_id, $vendor_id, $product_id, $variation_id, $item_price, $item_qty, $commission_rule );
- $admin_commission_amount = $item_price - $commission_amount;
- wc_update_order_item_meta( $item_id, $commission_meta_key, $admin_commission_amount );
- return $admin_commission_amount;
- }
- }
- return $commission_tax;
- }
- //Commission tax only on admin commission + total transaction charge.
- add_filter( 'wcfmmp_commission_deducted_tax', function( $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $total_commission, $commission_rule, $item_id ) {
- global $WCFMmp;
- if( apply_filters( 'wcfm_is_admin_fee_mode', false ) ) {
- remove_filter( 'wcfmmp_commission_deducted_tax', array($WCFMmp->wcfmmp_commission, 'wcfmmp_commission_deducted_tax_admin_mode_handler' ), 100 );
- $admin_commission_amount = calculate_commission_tax_on_admin_commission($item_id, $commission_tax, $vendor_id, $product_id, $variation_id, $order_id, $commission_rule);
- $admin_commission_amount += calculate_transaction_charge_for_vendor_order_item($item_id, $vendor_id, $commission_rule);
- $commission_tax = $admin_commission_amount * ( (float)$commission_rule['tax_percent'] / 100 );
- }
- return $commission_tax;
- }, 99, 8 );
- //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' );
- wc_delete_order_item_meta( $marketplace_order->item_id, '_wcfmmp_commission_tax_only_on_admin_commission' );
- }
- }, 10, 3 );
- add_action( 'wcfm_order_details_after_line_total_head', function($order) {
- global $WCFM;
- remove_action( 'wcfm_order_details_after_line_total_head', array( $WCFM->wcfm_marketplace, 'wcfmmp_after_line_total_head' ) );
- if( wcfm_vendor_has_capability( $WCFM->wcfm_marketplace->vendor_id, 'view_commission' ) ) {
- $admin_fee_mode = apply_filters( 'wcfm_is_admin_fee_mode', false );
- if( $admin_fee_mode ) {
- ?>
- <th class="line_cost"><?php _e( 'Commission', 'wc-frontend-manager' ); ?></th>
- <?php } else { ?>
- <th class="line_cost"><?php _e( 'Earning', 'wc-frontend-manager' ); ?></th>
- <?php
- }
- }
- }, 9 );
- add_action('wcfm_order_totals_after_total', function ($order_id) {
- global $WCFM, $wpdb, $WCFMmp;
- remove_action('wcfm_order_totals_after_total', array($WCFM->wcfm_marketplace, 'wcfmmp_order_total_commission'));
- $admin_fee_mode = apply_filters('wcfm_is_admin_fee_mode', false);
- $gross_sale_order = $WCFM->wcfm_vendor_support->wcfm_get_gross_sales_by_vendor($WCFM->wcfm_marketplace->vendor_id, '', '', $order_id);
- $order = wc_get_order($order_id);
- $order_currency = $order->get_currency();
- $order_status = sanitize_title($order->get_status());
- $td_style = '';
- $sql = "
- SELECT GROUP_CONCAT(ID) as commission_ids,
- GROUP_CONCAT(item_id) as order_item_ids,
- SUM(commission_amount) as line_total,
- SUM(total_commission) as total_commission,
- SUM(item_total) as item_total,
- SUM(item_sub_total) as item_sub_total,
- SUM(shipping) as shipping,
- SUM(tax) as tax,
- SUM(shipping_tax_amount) as shipping_tax_amount,
- SUM(refunded_amount) as refunded_amount,
- SUM(discount_amount) as discount_amount
- FROM {$wpdb->prefix}wcfm_marketplace_orders
- WHERE order_id = " . $order_id . "
- AND `vendor_id` = " . $WCFM->wcfm_marketplace->vendor_id . "
- AND `is_refunded` != 1";
- $order_due = $wpdb->get_results($sql);
- if (!$order_due || !isset($order_due[0])) return;
- $total = 0;
- $subtotal = 0;
- $calculated_total = 0;
- $total_tax = 0;
- $total_shipping = 0;
- $shipping_tax = 0;
- $refund_total = 0;
- $discount_total = 0;
- $commission_tax = 0;
- $aff_commission = 0;
- $transaction_charge = 0;
- $commission_rule = array();
- $total = $order_due[0]->total_commission;
- if ($WCFMmp->wcfmmp_vendor->is_vendor_deduct_discount($WCFM->wcfm_marketplace->vendor_id, $order_id)) {
- $calculated_total = $order_due[0]->item_total;
- } else {
- $calculated_total = $order_due[0]->item_sub_total;
- }
- // WC Refund Support - 3.0.4
- $commission_ids = explode(",", $order_due[0]->commission_ids);
- foreach ($commission_ids as $commission_id) {
- if (method_exists($WCFMmp->wcfmmp_commission, 'wcfmmp_get_commission_meta')) {
- $total_tax += (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta($commission_id, 'gross_tax_cost');
- $total_shipping += (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta($commission_id, 'gross_shipping_cost');
- $shipping_tax += (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta($commission_id, 'gross_shipping_tax');
- $commission_tax += (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta($commission_id, 'commission_tax');
- $aff_commission += (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta($commission_id, '_wcfm_affiliate_commission');
- $transaction_charge += (float) $WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta($commission_id, 'transaction_charge');
- $commission_rule = unserialize($WCFMmp->wcfmmp_commission->wcfmmp_get_commission_meta($commission_id, 'commission_rule'));
- }
- }
- $calculated_total += (float) $total_tax;
- $calculated_total += (float) apply_filters('wcfmmmp_gross_sales_shipping_cost', $total_shipping, $WCFM->wcfm_marketplace->vendor_id);
- $calculated_total += (float) $shipping_tax;
- $refund_total = $order_due[0]->refunded_amount;
- $discount_total = $order_due[0]->discount_amount;
- $get_shipping = $WCFMmp->wcfmmp_vendor->is_vendor_get_shipping($WCFM->wcfm_marketplace->vendor_id);
- $get_tax = $WCFMmp->wcfmmp_vendor->is_vendor_get_tax($WCFM->wcfm_marketplace->vendor_id);
- ?>
- <?php do_action('wcfm_vendor_order_details_before_subtotal', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Subtotal', 'wc-frontend-manager'); ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- $subtotal = $calculated_total - ((float) $refund_total + (float) $order_due[0]->shipping + (float) $order_due[0]->tax + (float) $order_due[0]->shipping_tax_amount);
- if ($refund_total && (round($subtotal, 2) != round($order_due[0]->item_sub_total, 2))) {
- echo "<del>" . wc_price($order_due[0]->item_sub_total, array('currency' => $order_currency)) . "</del>";
- echo "<ins>" . wc_price($subtotal, array('currency' => $order_currency)) . "</ins>";
- } else {
- echo wc_price($order_due[0]->item_sub_total, array('currency' => $order_currency));
- }
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_subtotal', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php if ($get_shipping && $order->get_formatted_shipping_address()) { ?>
- <?php do_action('wcfm_vendor_order_details_before_shipping', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Shipping', 'wc-frontend-manager'); ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- echo wc_price(apply_filters('wcfmmmp_gross_sales_shipping_cost', $total_shipping, $WCFM->wcfm_marketplace->vendor_id), array('currency' => $order_currency));
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_shipping', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php } ?>
- <?php if (apply_filters('wcfm_is_allow_vendor_order_details_tax_breakup_total', true)) { ?>
- <?php do_action('wcfm_vendor_order_details_before_tax', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php do_action('wcfm_vendor_order_details_before_shipping_tax', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>">
- <?php _e('Total Tax Collected', 'wc-frontend-manager'); ?>:
- </th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- echo wc_price($total_tax + $shipping_tax, array('currency' => $order_currency));
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_shipping_tax', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php do_action('wcfm_vendor_order_details_after_tax', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php } ?>
- <?php if ($refund_total) { ?>
- <?php do_action('wcfm_vendor_order_details_before_refund', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label refunded-total" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Refunded', 'wc-frontend-manager'); ?>:</th>
- <td class="total refunded-total" style="text-align:center; <?php echo $td_style; ?>">-<?php echo wc_price($refund_total, array('currency' => $order_currency)); ?></td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_before_refund', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php } ?>
- <?php if ($discount_total && $WCFMmp->wcfmmp_vendor->is_vendor_deduct_discount($WCFM->wcfm_marketplace->vendor_id, $order_id)) { ?>
- <?php do_action('wcfm_vendor_order_details_before_discount', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label discount-total" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Discount', 'wc-frontend-manager'); ?>:</th>
- <td class="total discount-total" style="text-align:center; <?php echo $td_style; ?>"><?php echo wc_price($discount_total, array('currency' => $order_currency)); ?></td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_discount', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php } ?>
- <?php if (apply_filters('wcfm_is_allow_gross_total', true)) { ?>
- <?php do_action('wcfm_vendor_order_details_before_gross_total', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr class="total_cost">
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Gross Total', 'wc-frontend-manager'); ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- if ($refund_total) {
- echo "<del>" . wc_price($calculated_total, array('currency' => $order_currency)) . "</del>";
- echo "<ins>" . wc_price($gross_sale_order, array('currency' => $order_currency)) . "</ins>";
- } else {
- echo wc_price($calculated_total, array('currency' => $order_currency));
- }
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_gross_total', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php } ?>
- <?php do_action('wcfm_vendor_order_details_before_admin_commission', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr class="total_cost">
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Commission', 'wc-frontend-manager'); ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- echo wc_price($order_due[0]->item_total - $order_due[0]->line_total, array('currency' => $order_currency));
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_admin_commission', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php
- if (apply_filters('wcfm_is_allow_order_details_commission_breakup_gross_earning', true) && wcfm_vendor_has_capability($WCFM->wcfm_marketplace->vendor_id, 'view_commission') && !in_array($order_status, array('failed', 'cancelled', 'refunded', 'request', 'proposal', 'proposal-sent', 'proposal-expired', 'proposal-rejected', 'proposal-canceled', 'proposal-accepted'))) {
- if (!$admin_fee_mode || ($admin_fee_mode && apply_filters('wcfm_is_allow_admin_fee_mode_commission_breakup', true))) {
- // Show Transaction Charges
- if ($transaction_charge && apply_filters('wcfm_is_allow_view_transaction_charge', true) && isset($commission_rule['transaction_charge_type']) && ($commission_rule['transaction_charge_type'] != 'no')) {
- ?>
- <?php do_action('wcfm_vendor_order_details_before_transaction_charge', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Merchant Fees', 'wc-frontend-manager'); ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- echo wc_price($transaction_charge, array('currency' => $order_currency));
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_transaction_charge', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php
- }
- if (isset($commission_rule['tax_enable']) && ($commission_rule['tax_enable'] == 'yes')) {
- ?>
- <?php do_action('wcfm_vendor_order_details_before_commission_tax', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php echo 'Tax on fees'; ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- echo wc_price($commission_tax, array('currency' => $order_currency));
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_commission_tax', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php
- }
- ?>
- <?php do_action('wcfm_vendor_order_details_before_total_earning', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <tr>
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Seller Earnings', 'wc-frontend-manager'); ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- echo wc_price($total, array('currency' => $order_currency));
- if (apply_filters('wcfm_is_allow_earning_in_words', false)) {
- echo "<br/>" . wcfm_number_to_words($total);
- }
- ?>
- </div>
- </td>
- </tr>
- <?php do_action('wcfm_vendor_order_details_after_total_earning', $order_id, $WCFM->wcfm_marketplace->vendor_id); ?>
- <?php
- }
- if (apply_filters('wcfm_is_allow_order_details_admin_fee', true)) {
- do_action('wcfm_vendor_order_details_before_admin_fee', $order_id, $WCFM->wcfm_marketplace->vendor_id);
- ?>
- <tr>
- <th class="label" colspan="2" style="text-align:right; <?php echo $td_style; ?>"><?php _e('Admin Fee', 'wc-frontend-manager'); ?>:</th>
- <td class="total" style="text-align:center; <?php echo $td_style; ?>">
- <div class="view">
- <?php
- echo wc_price(($calculated_total - $total), array('currency' => $order_currency));
- if (apply_filters('wcfm_is_allow_earning_in_words', false)) {
- echo "<br/>" . wcfm_number_to_words(($gross_sale_order - $total));
- }
- ?>
- </div>
- </td>
- </tr>
- <?php
- do_action('wcfm_vendor_order_details_after_admin_fee', $order_id, $WCFM->wcfm_marketplace->vendor_id);
- }
- }
- }, 9);
Add Comment
Please, Sign In to add comment