Advertisement
xah

multiplier function v0.0.8

xah
Jun 10th, 2020 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.35 KB | None | 0 0
  1. // Cost price field
  2. add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
  3. function wc_cost_product_field() {
  4.     woocommerce_wp_text_input( array( 'id' => 'cost_price', 'class' => 'wc_input_price short', 'label' => __( 'Cost Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
  5. }
  6.  
  7. add_action( 'save_post', 'wc_cost_save_product' );
  8. function wc_cost_save_product( $product_id ) {
  9.  
  10.      // stop the quick edit interferring as this will stop it saving properly, when a user uses quick edit feature
  11.      if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
  12.         return;
  13.  
  14.     // If this is a auto save do nothing, we only save when update button is clicked
  15.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  16.         return;
  17.     if ( isset( $_POST['cost_price'] ) ) {
  18.         if ( is_numeric( $_POST['cost_price'] ) )
  19.             update_post_meta( $product_id, 'cost_price', $_POST['cost_price'] );
  20.     } else{
  21. update_post_meta( $product_id, '', 'cost_price' );}
  22. }
  23.  
  24. // Utility function to change the prices with a multiplier (number)
  25. function get_price_multiplier() {
  26.     return 0.917; // Modify as wish
  27. }
  28.  
  29. // Simple, grouped and external products
  30. add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
  31. add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
  32. add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
  33. add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
  34.  
  35. // --Simple product price change
  36. function custom_price( $price, $product ) {
  37.  
  38. $user = wp_get_current_user();
  39. global $post;
  40. $cost_price = get_post_meta( $post->ID, 'cost_price', true );
  41.  
  42. if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
  43.     if ( $cost_price != '' && $cost_price != 0){$profit = $price / 2 - $cost_price / 2; $price = $price - $profit;}
  44.  
  45.     else{$price = $price * get_price_multiplier();}}else{
  46.     $price = $price;}
  47.  
  48.     return (float) $price;
  49. }
  50.  
  51. // --Variable product price change
  52. add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
  53. add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
  54. function custom_variable_price( $price, $variation, $product ) {
  55.     wc_delete_product_transients($variation->get_id());
  56.  
  57. $user = wp_get_current_user();
  58.  
  59. if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
  60.     $price = $price * get_price_multiplier();}else{
  61.     $price = $price;}
  62.  
  63.     return (float) $price;
  64. }
  65.  
  66. //Hide Price Range for WooCommerce Variable Products
  67. add_filter( 'woocommerce_variable_sale_price_html',
  68. 'variable_product_price', 10, 2 );
  69. add_filter( 'woocommerce_variable_price_html',
  70. 'variable_product_price', 10, 2 );
  71.  
  72. function variable_product_price( $v_price, $v_product ) {
  73.  
  74. // Product Price
  75. $prod_prices = array( $v_product->get_variation_price( 'min', true ),
  76.                             $v_product->get_variation_price( 'max', true ) );
  77. $prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__('%1$s', 'woocommerce'),
  78.                        wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );
  79.  
  80. // Regular Price
  81. $user = wp_get_current_user();
  82.  
  83. if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
  84. $regular_prices = array( $v_product->get_variation_regular_price( 'min', true ),
  85.                           $v_product->get_variation_regular_price( 'max', true ) );
  86. $og_price = $regular_prices[0] / get_price_multiplier();
  87. sort( $regular_prices );
  88.  
  89. $regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('%1$s','woocommerce')
  90.                       , wc_price( $og_price ) ) : wc_price( $og_price );
  91. }else{
  92. $regular_prices = array( $v_product->get_variation_regular_price( 'min', true ),
  93.                           $v_product->get_variation_regular_price( 'max', true ) );
  94. sort( $regular_prices );
  95.  
  96. $regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('%1$s','woocommerce')
  97.                       , wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );
  98. }
  99.  
  100. if ( $prod_price !== $regular_price ) {
  101. $prod_price = '<del>'.$regular_price.$v_product->get_price_suffix() . '</del> <ins>' .
  102.                        $prod_price . $v_product->get_price_suffix() . '</ins>';
  103. }
  104. return $prod_price;
  105. }
  106.  
  107. // Simple Products
  108. add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
  109.  
  110. function filter_woocommerce_get_price_html( $price, $instance ) {
  111.     global $product;
  112.  
  113. $user = wp_get_current_user();
  114. global $post;
  115. $cost_price = get_post_meta( $post->ID, 'cost_price', true );
  116.  
  117. if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
  118. if($product->is_type('simple')){
  119. if($cost_price != '' && $cost_price != 0){
  120. $regular_price=$product->get_regular_price('min');
  121. $profit=$regular_price / 2 - $cost_price / 2;
  122. $sale_price=$regular_price - $profit;
  123. $sale_price=round($sale_price);
  124. }else{
  125. $regular_price=$product->get_regular_price('min');
  126. $sale_price=$regular_price * get_price_multiplier();
  127. $sale_price=round($sale_price);}
  128.  
  129. if ( $sale_price !== $regular_price ) {
  130. $price = '<del>$'.$regular_price .'&nbsp;</del><ins>$' .
  131.                        $sale_price . '</ins>';
  132. }}}
  133.  
  134.     return $price;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement