Show discounted price in Woocommerce Bookings

816 Views Asked by At

I want to show the discounted price(price after adding the range) along with the base price of a bookable product what i am doing is changing the code below in the \woocommerce-bookings\includes\adminclass-wc-bookings-ajax.php file

// Build the output
        
        $before = $product->get_price();
        $after = wc_price( $display_price ) ;
        $discount = $product->get_price() - wc_price( $display_price );
        $output = apply_filters( 'woocommerce_bookings_booking_cost_string', __( 'Booking cost', 'woocommerce-bookings' ), $product ) .$discount ': <strong>' .$discount . $price_suffix . '</strong>';

Is this the right way or can you suggest anything?

1

There are 1 best solutions below

0
On

Overwriting Woocommerce Bookings core code is something that you should absolutely not do for many different reasons that I am not going to explain.

Now as you can see in that source code, developers have added a filter hook to allow you to make changes to the $output variable.

Now there is not any discounted price in WooCommerce bookable products as there is on WooCommerce other products.

So first place back the original plugin file.

Then you can use the filter as follow (where you will add your own code):

add_filter( 'woocommerce_bookings_booking_cost_string', 'change_bookings_booking_cost_string', 10, 2 );
function change_bookings_booking_cost_string( $cost_string, $product ) {
    $raw_price       = $product->get_price();
    $display_price   = wc_get_price_to_display($product);
    $price_suffix    = $product->get_price_suffix();
    $formatted_price = wc_price($display_price) . $price_suffix;
    
    $additional_output = 'Here comes your code'; // Replace by your code variables 
    
    return $cost_string . ' ' . $additional_output; // Always return (never echo)
}

Code goes in functions.php file of your active child theme (or active theme).