Woocommerce update variations sale price per user role

Question

(first post, please don’t be harsh),

I am trying to change regular and sales prices for simple and variable (with different prices per variation) products, by user role. Everything works fine for simple products (both regular and sales price display properly). But for variable products, when I try to display Sales Price (not regular price), things get messed up.

To proceed with the code, I used the common filters provided around the web with some tweaks and additions:

    /*
    *
    * Eveything works great for simple products. If a regular price is added (no sales price),
    * it is displayed fine. Moreover, when a sales price is added, the regular price
    * gets "deleted" with strikethrough as normal, displayed the sales price next to it.
    *
    */

    add_filter('woocommerce_product_get_price', 'ui_custom_price_role_sale', 99, 2 );
    add_filter('woocommerce_product_get_regular_price', 'ui_custom_price_role', 99, 2 );
    add_filter('woocommerce_product_get_sale_price', 'ui_custom_price_role_sale', 99, 2 );
    
    
    function ui_custom_price_role( $price, $product ) {
      $price = ui_custom_price_handling( $price, $product );  
      return $price;
    }
    
    function ui_custom_price_handling($price, $product) {
    
        wc_delete_product_transients($product->get_id());
      
      $current_user = wp_get_current_user();
      
      //using administrator for implementation purposes - will be changed to final role later
      if ( isset( $current_user->roles[0] ) && '' != $current_user->roles[0] && in_array( 'administrator',  $current_user->roles ) ) {
        
        $custom_price = get_post_meta( $product->get_id(), 'wholesale_price', true );
        
        if ( ! empty($custom_price) ) {
          $price = $custom_price;
        }
      }
      return $price;
    }
    
    
    function ui_custom_price_role_sale( $price, $product ) {
      $price = ui_custom_price_handling_sale( $price, $product );  
      return $price;
    }
    
    function ui_custom_price_handling_sale($price, $product) {
       //wc_delete_product_transients($variation->get_id());
      
      $current_user = wp_get_current_user();
      
      if ( isset( $current_user->roles[0] ) && '' != $current_user->roles[0] && in_array( 'administrator',  $current_user->roles ) ) {
        
        $custom_price = get_post_meta( $product->get_id(), 'wholesale_sale_price', true );
        
        //if there is a custom price, apply it
        if ( ! empty($custom_price) ) {
          $price = $custom_price;
        }
      }
      
      return $price;
    }

    /*
    *
    * For variable products, when the regular price is changed, eveything still works well.
    * But when sales price is added, problems begin. Either the regular price does not 
    * get displayed properly, or if I manage to display the sales price in products 
    * with sales price added, then those who do not have sales price (only regular price),
    * also break.
    *
    */

    //had to use all 3 more filters than the simple products - this would be nice to explain why also if someone knows
    add_filter('woocommerce_product_variation_get_price', 'ui_custom_price_role', 99, 3 );
    add_filter('woocommerce_product_variation_get_regular_price', 'ui_custom_price_role', 99, 3 );
    add_filter('woocommerce_product_variation_get_sale_price', 'ui_custom_price_role_sale', 99, 3 );
    add_filter('woocommerce_variation_prices_price', 'ui_custom_variable_price', 99, 3 );
    add_filter('woocommerce_variation_prices_regular_price', 'ui_custom_variable_price', 99, 3 );
    add_filter('woocommerce_variation_prices_sale_price', 'ui_custom_variable_price_sale', 99, 3 );

function ui_custom_variable_price( $price, $variation, $product ) {
  $price = ui_custom_price_handling_variation_kass( $price, $variation, $product  );  
    return $price;
}

function ui_custom_variable_price_sale( $price, $variation, $product ) {
  $price = ui_custom_price_handling_sale_variation_kass( $price, $variation, $product );  
    return $price;
}
 
function ui_custom_price_handling_variation_kass($price, $variation, $product ) {

    wc_delete_product_transients($variation->get_id());
  
  $current_user = wp_get_current_user();
  
  if ( isset( $current_user->roles[0] ) && '' != $current_user->roles[0] && in_array( 'administrator',  $current_user->roles ) ) {
    
    $custom_price = get_post_meta( $variation->get_id(), 'wholesale_price', true );
    
    if ( ! empty($custom_price) ) {
      $price = $custom_price;
    }
  }
  return $price;
}
 
function ui_custom_price_handling_sale_variation_kass($price, $variation, $product) {
  
  $current_user = wp_get_current_user();
  
  if ( isset( $current_user->roles[0] ) && '' != $current_user->roles[0] && in_array( 'administrator',  $current_user->roles ) ) {
    
    $custom_price = get_post_meta( $variation->get_id(), 'wholesale_sale_price', true );
    
    //if there is a custom price, apply it
    if ( ! empty($custom_price) ) {
      $price = $custom_price;
    }
  }
  
  return $price;
}

To conclude, the problem here is that when changing the sales price with the above filters, when variable products have different prices per variation, things dont work out. The final goal here is to show regular price deleted and sales price added just like the native behaviour, as well as deleted regular price range and new sales price range on product loop pages.

Anyone has any ideas? Thanks for reading, regards!

0
PureMagic 2 months 2022-12-06T22:08:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse