Posted on Leave a comment

Woocommerce Role Pricing with Tickets for Woocommerce

If you are using Tickets for Woocommerce with The Event Calendar to “sell events”, and you want to apply discounts according to the user role ( Woocommerce Role Pricing), now this is possible.

You need to add this code to your functions.php theme file:


add_filter( 'tribe_get_event_meta', 'my_tribe_get_event_meta', 10, 4 );

function my_tribe_get_event_meta ( $output, $postID, $meta, $single ) {

	if ( $meta == "_EventCost" ) {
		if ( is_array( $output ) ) {
			
			$type = get_option( "wrp-method", "rate" );
			$result = $output[0];
			
			$user = wp_get_current_user();
			$user_roles = $user->roles;
			$user_role = array_shift($user_roles);
			
			$discount = 0;
			if ( $user_role !== null ) {
				if ( get_option( "wrp-" . $user_role, "-1" ) !== "-1" ) {
					$discount = get_option( "wrp-" . $user_role );
				}
			}
			if ( $discount ) {
				$method = get_option( "wrp-method", "rate" );
				if ( $method == "rate" ) {
					$discount = WooRolePricing::bcsub ( 1, $discount, WOO_ROLE_PRICING_DECIMALS );
					// for security reasons, set 0
					if ( $discount < 0 ) {
						$discount = 0;
					}
				}
			}

			if ($type == "rate") {
				$result = WooRolePricing::bcmul($result, $discount, WOO_ROLE_PRICING_DECIMALS);
			} else {
				$result = WooRolePricing::bcsub($result, $discount, WOO_ROLE_PRICING_DECIMALS);
			}

			$output[0] = $result;
		}
	}
	return $output;
}
Leave a Reply

Your email address will not be published. Required fields are marked *