Wednesday, December 7, 2022
HomeWordPress DevelopmentWoocommerce replace variations sale worth per person position

Woocommerce replace variations sale worth per person position


(first publish, please do not be harsh),

I’m attempting to vary common and gross sales costs for easy and variable (with totally different costs per variation) merchandise, by person position. Every part works positive for easy merchandise (each common and gross sales worth show correctly). However for variable merchandise, when I attempt to show Gross sales Value (not common worth), issues get tousled.

To proceed with the code, I used the frequent filters offered across the internet with some tweaks and additions:

    /*
    *
    * Eveything works nice for easy merchandise. If an everyday worth is added (no gross sales worth),
    * it's displayed positive. Furthermore, when a gross sales worth is added, the common worth
    * will get "deleted" with strikethrough as regular, displayed the gross sales worth subsequent 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 );
    
    
    operate ui_custom_price_role( $worth, $product ) {
      $worth = ui_custom_price_handling( $worth, $product );  
      return $worth;
    }
    
    operate ui_custom_price_handling($worth, $product) {
    
        wc_delete_product_transients($product->get_id());
      
      $current_user = wp_get_current_user();
      
      //utilizing administrator for implementation functions - will probably be modified to ultimate position 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) ) {
          $worth = $custom_price;
        }
      }
      return $worth;
    }
    
    
    operate ui_custom_price_role_sale( $worth, $product ) {
      $worth = ui_custom_price_handling_sale( $worth, $product );  
      return $worth;
    }
    
    operate ui_custom_price_handling_sale($worth, $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's a customized worth, apply it
        if ( ! empty($custom_price) ) {
          $worth = $custom_price;
        }
      }
      
      return $worth;
    }

    /*
    *
    * For variable merchandise, when the common worth is modified, eveything nonetheless works properly.
    * However when gross sales worth is added, issues start. Both the common worth doesn't 
    * get displayed correctly, or if I handle to show the gross sales worth in merchandise 
    * with gross sales worth added, then those that do not need gross sales worth (solely common worth),
    * additionally break.
    *
    */

    //had to make use of all 3 extra filters than the straightforward merchandise - this is able to be good to clarify why additionally if somebody is aware of
    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 );

operate ui_custom_variable_price( $worth, $variation, $product ) {
  $worth = ui_custom_price_handling_variation_kass( $worth, $variation, $product  );  
    return $worth;
}

operate ui_custom_variable_price_sale( $worth, $variation, $product ) {
  $worth = ui_custom_price_handling_sale_variation_kass( $worth, $variation, $product );  
    return $worth;
}
 
operate ui_custom_price_handling_variation_kass($worth, $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) ) {
      $worth = $custom_price;
    }
  }
  return $worth;
}
 
operate ui_custom_price_handling_sale_variation_kass($worth, $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's a customized worth, apply it
    if ( ! empty($custom_price) ) {
      $worth = $custom_price;
    }
  }
  
  return $worth;
}

To conclude, the issue right here is that when altering the gross sales worth with the above filters, when variable merchandise have totally different costs per variation, issues dont work out. The ultimate objective right here is to point out common worth deleted and gross sales worth added similar to the native behaviour, in addition to deleted common worth vary and new gross sales worth vary on product loop pages.

Anybody has any concepts? Thanks for studying, regards!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments