0

I'm trying to change the price for bookable products using the Woocommerce Bookings plugin. The price needs to be set to 0 if a user has a specific role. I'm using both the hook 'woocommerce_before_calculate_totals' and the filter 'booking_form_calculated_booking_cost' to try and achieve this, but the price keeps obtaining it's original value in the cart.

add_action( 'woocommerce_before_calculate_totals', [ $this, 'calculate_price' ], 10, 3 );
add_filter( 'booking_form_calculated_booking_cost', [ $this, 'set_booking_price' ], 10, 3 );

public function calculate_price( $cart ) {
    if (!is_user_logged_in()) 
        return;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $user = wp_get_current_user();

    if( in_array('trusted_customer', (array)$user->roles ) ) {
        foreach ( $cart->get_cart() as $cart_item )
        {
            if( isset($cart_item['booking']) ) {
                $cart_item['data']->set_price(0);
            }
        }
    }
}

public function set_booking_price( $booking_cost, $booking, $posted )
{
    if (!is_user_logged_in()) return;

    $user = wp_get_current_user();

    if( in_array('trusted_customer', (array)$user->roles ) ) {
        $booking_cost = 0;
    }

    return $booking_cost;
}

Has anyone experienced this issue before and been able to resolve this? i've tried using the filter 'woocommerce_add_cart_item_data' aswell but this hasen't changed anything either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.