Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
- function add_custom_price( $cart_object ) {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- if (WC()->cart->get_cart_contents_count() > 0):
- // Licence values chosen from omsetype.co buying module e.g. 1:10000:2500 must have at least one value
- $multiplier_desktop = ! empty(WC()->session->get('licence')) ? WC()->session->get('licence') : 0;
- $multiplier_web = ! empty(WC()->session->get('licence_web')) ? WC()->session->get('licence_web') : 0;
- $multiplier_app = ! empty(WC()->session->get('licence_app')) ? WC()->session->get('licence_app') : 0;
- foreach ( $cart_object->cart_contents as $key => $value ):
- if ($value['data']->is_type('variation')):
- $price = mttd_return_price($multiplier_desktop, $multiplier_web, $multiplier_app, $value['data']);
- $value['data']->set_price($price);
- endif;
- endforeach;
- endif;
- }
- /**
- * Return price
- **/
- function mttd_return_price($multiplier_desktop, $multiplier_web, $multiplier_app, $product) {
- $price = 0;
- $price_base = $product->get_price();
- $price_division_web = get_field('price_division_web', 'options'); // 100
- $price_division_app = get_field('price_division_app', 'options'); // 50
- if (!empty($multiplier_desktop) && $multiplier_desktop > 0):
- $price += $price_base * sqrt(floatval($multiplier_desktop));
- endif;
- if (!empty($multiplier_web) && $multiplier_web > 0):
- $price += ($price_base * sqrt(floatval($multiplier_web))) / $price_division_web;
- endif;
- if (!empty($multiplier_app) && $multiplier_app > 0):
- $price += ($price_base * sqrt(floatval($multiplier_app))) / $price_division_app;
- endif;
- return $price;
- }
Add Comment
Please, Sign In to add comment