Advertisement
palsushobhan

Add commission to product price

Dec 13th, 2021
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.79 KB | None | 0 0
  1. function get_modified_price($price, $commisson_rule) {
  2.     if( !empty( $commisson_rule ) && is_array( $commisson_rule ) ) {
  3.         if(isset($commisson_rule['percent'])) {
  4.             return $price + ($price * ((float)$commisson_rule['percent']/100));
  5.         } elseif(isset($commisson_rule['fixed'])) {
  6.             return $price + (float)$commisson_rule['fixed'];
  7.         }
  8.     }
  9.     return $price;
  10. }
  11.  
  12. //patch for mini cart bug
  13. add_action( 'woocommerce_before_mini_cart', function() {
  14.     WC()->cart->calculate_totals();
  15. } );
  16.  
  17. add_filter('woocommerce_get_price_html', function($price_html, $product) {
  18.     global $WCFMmp;
  19.     if ( is_admin() ) return $price_html;
  20.     if ( '' === $product->get_price() ) return $price_html;
  21.     $vendor_id = wcfm_get_vendor_id_by_post( $product->get_id() );
  22.     if( $vendor_id ) {
  23.         $commisson_rule = $WCFMmp->wcfmmp_product->wcfmmp_get_product_commission_rule( $product->get_id() );
  24.         if($product->is_type('variable') && $product->is_on_sale()) {
  25.             $min_regular_price = $product->get_variation_regular_price( 'min' );
  26.             $mod_regular_price = get_modified_price($min_regular_price, $commisson_rule);
  27.             $min_sale_price = $product->get_variation_sale_price( 'min' );
  28.             $mod_sale_price = get_modified_price( $min_sale_price, $commisson_rule );
  29.             return wc_format_sale_price( $mod_regular_price, $mod_sale_price ) . $product->get_price_suffix();
  30.         }
  31.         if($product->is_on_sale()) {
  32.             $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
  33.             $mod_regular_price = get_modified_price($regular_price, $commisson_rule);
  34.             $sale_price = wc_get_price_to_display( $product );
  35.             $mod_sale_price = get_modified_price( $sale_price, $commisson_rule );
  36.             return wc_format_sale_price( $mod_regular_price, $mod_sale_price ) . $product->get_price_suffix();
  37.         }
  38.         $price = wc_get_price_to_display( $product );
  39.         return wc_price(get_modified_price($price, $commisson_rule));
  40.     }
  41.     return $price_html;
  42. }, 50, 2);
  43.  
  44. add_action( 'woocommerce_before_calculate_totals', function($cart) {
  45.     global $WCFMmp;
  46.     if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
  47.     if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
  48.     foreach ( $cart->get_cart() as $cart_item ) {
  49.         $product = $cart_item['data'];
  50.         $vendor_id = wcfm_get_vendor_id_by_post( $product->get_id() );
  51.         if($vendor_id) {
  52.             $commisson_rule = $WCFMmp->wcfmmp_product->wcfmmp_get_product_commission_rule( $product->get_id() );
  53.             $price = $product->get_price();
  54.             $cart_item['data']->set_price( get_modified_price($price, $commisson_rule) );
  55.         }
  56.     }
  57. }, 9999 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement