Guest User

Untitled

a guest
Jan 22nd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
  2. function add_custom_price( $cart_object ) {
  3.  
  4. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  5. return;
  6.  
  7. if (WC()->cart->get_cart_contents_count() > 0):
  8.  
  9. // Licence values chosen from omsetype.co buying module e.g. 1:10000:2500 must have at least one value
  10. $multiplier_desktop = ! empty(WC()->session->get('licence')) ? WC()->session->get('licence') : 0;
  11. $multiplier_web = ! empty(WC()->session->get('licence_web')) ? WC()->session->get('licence_web') : 0;
  12. $multiplier_app = ! empty(WC()->session->get('licence_app')) ? WC()->session->get('licence_app') : 0;
  13.  
  14. foreach ( $cart_object->cart_contents as $key => $value ):
  15. if ($value['data']->is_type('variation')):
  16. $price = mttd_return_price($multiplier_desktop, $multiplier_web, $multiplier_app, $value['data']);
  17. $value['data']->set_price($price);
  18. endif;
  19. endforeach;
  20.  
  21. endif;
  22. }
  23.  
  24.  
  25.  
  26. /**
  27. * Return price
  28. **/
  29. function mttd_return_price($multiplier_desktop, $multiplier_web, $multiplier_app, $product) {
  30. $price = 0;
  31. $price_base = $product->get_price();
  32. $price_division_web = get_field('price_division_web', 'options'); // 100
  33. $price_division_app = get_field('price_division_app', 'options'); // 50
  34.  
  35. if (!empty($multiplier_desktop) && $multiplier_desktop > 0):
  36. $price += $price_base * sqrt(floatval($multiplier_desktop));
  37. endif;
  38. if (!empty($multiplier_web) && $multiplier_web > 0):
  39. $price += ($price_base * sqrt(floatval($multiplier_web))) / $price_division_web;
  40. endif;
  41. if (!empty($multiplier_app) && $multiplier_app > 0):
  42. $price += ($price_base * sqrt(floatval($multiplier_app))) / $price_division_app;
  43. endif;
  44.  
  45. return $price;
  46. }
Add Comment
Please, Sign In to add comment