Advertisement
xah

multiplier function v0.0.3

xah
Jun 10th, 2020 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.19 KB | None | 0 0
  1. <?php
  2.  
  3. // Utility function to change the prices with a multiplier (number)
  4. function get_price_multiplier() {
  5.     return 0.917; // x2 for testing
  6. }
  7.  
  8. // Simple, grouped and external products
  9. add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
  10. add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
  11. // Variations
  12. add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
  13. add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
  14. function custom_price( $price, $product ) {
  15.  
  16. $user = wp_get_current_user();
  17.  
  18. if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
  19.     $price = $price * get_price_multiplier();}else{
  20.     $price = $price;}
  21.  
  22.     return (float) $price;
  23. }
  24.  
  25. // Variable (price range)
  26. add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
  27. add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
  28. function custom_variable_price( $price, $variation, $product ) {
  29.     // Delete product cached price  (if needed)
  30.     wc_delete_product_transients($variation->get_id());
  31.  
  32. $user = wp_get_current_user();
  33.  
  34. if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
  35.     $price = $price * get_price_multiplier();}else{
  36.     $price = $price;}
  37.  
  38.     return (float) $price;
  39. }
  40.  
  41. //Hide Price Range for WooCommerce Variable Products
  42. add_filter( 'woocommerce_variable_sale_price_html',
  43. 'variable_product_price', 10, 2 );
  44. add_filter( 'woocommerce_variable_price_html',
  45. 'variable_product_price', 10, 2 );
  46.  
  47. function variable_product_price( $v_price, $v_product ) {
  48.  
  49. // Product Price
  50. $prod_prices = array( $v_product->get_variation_price( 'min', true ),
  51.                             $v_product->get_variation_price( 'max', true ) );
  52. $prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__('%1$s', 'woocommerce'),
  53.                        wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );
  54.  
  55. // Regular Price
  56. $user = wp_get_current_user();
  57.  
  58. if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
  59. $regular_prices = array( $v_product->get_variation_regular_price( 'min', true ),
  60.                           $v_product->get_variation_regular_price( 'max', true ) );
  61. $og_price = $regular_prices[0] / 0.917;
  62. sort( $regular_prices );
  63.  
  64. $regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('%1$s','woocommerce')
  65.                       , wc_price( $og_price ) ) : wc_price( $og_price );
  66. }else{
  67. $regular_prices = array( $v_product->get_variation_regular_price( 'min', true ),
  68.                           $v_product->get_variation_regular_price( 'max', true ) );
  69. sort( $regular_prices );
  70.  
  71. $regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('%1$s','woocommerce')
  72.                       , wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );
  73. }
  74.  
  75. if ( $prod_price !== $regular_price ) {
  76. $prod_price = '<del>'.$regular_price.$v_product->get_price_suffix() . '</del> <ins>' .
  77.                        $prod_price . $v_product->get_price_suffix() . '</ins>';
  78. }
  79. return $prod_price;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement