Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_modified_price($price, $commisson_rule) {
- if( !empty( $commisson_rule ) && is_array( $commisson_rule ) ) {
- if(isset($commisson_rule['percent'])) {
- return $price + ($price * ((float)$commisson_rule['percent']/100));
- } elseif(isset($commisson_rule['fixed'])) {
- return $price + (float)$commisson_rule['fixed'];
- }
- }
- return $price;
- }
- //patch for mini cart bug
- add_action( 'woocommerce_before_mini_cart', function() {
- WC()->cart->calculate_totals();
- } );
- add_filter('woocommerce_get_price_html', function($price_html, $product) {
- global $WCFMmp;
- if ( is_admin() ) return $price_html;
- if ( '' === $product->get_price() ) return $price_html;
- $vendor_id = wcfm_get_vendor_id_by_post( $product->get_id() );
- if( $vendor_id ) {
- $commisson_rule = $WCFMmp->wcfmmp_product->wcfmmp_get_product_commission_rule( $product->get_id() );
- if($product->is_type('variable') && $product->is_on_sale()) {
- $min_regular_price = $product->get_variation_regular_price( 'min' );
- $mod_regular_price = get_modified_price($min_regular_price, $commisson_rule);
- $min_sale_price = $product->get_variation_sale_price( 'min' );
- $mod_sale_price = get_modified_price( $min_sale_price, $commisson_rule );
- return wc_format_sale_price( $mod_regular_price, $mod_sale_price ) . $product->get_price_suffix();
- }
- if($product->is_on_sale()) {
- $regular_price = wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) );
- $mod_regular_price = get_modified_price($regular_price, $commisson_rule);
- $sale_price = wc_get_price_to_display( $product );
- $mod_sale_price = get_modified_price( $sale_price, $commisson_rule );
- return wc_format_sale_price( $mod_regular_price, $mod_sale_price ) . $product->get_price_suffix();
- }
- $price = wc_get_price_to_display( $product );
- return wc_price(get_modified_price($price, $commisson_rule));
- }
- return $price_html;
- }, 50, 2);
- add_action( 'woocommerce_before_calculate_totals', function($cart) {
- global $WCFMmp;
- if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
- if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
- foreach ( $cart->get_cart() as $cart_item ) {
- $product = $cart_item['data'];
- $vendor_id = wcfm_get_vendor_id_by_post( $product->get_id() );
- if($vendor_id) {
- $commisson_rule = $WCFMmp->wcfmmp_product->wcfmmp_get_product_commission_rule( $product->get_id() );
- $price = $product->get_price();
- $cart_item['data']->set_price( get_modified_price($price, $commisson_rule) );
- }
- }
- }, 9999 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement