Search code examples
wordpresswoocommercehook-woocommerce

Calculate price of rental with different periods of pricing wordpress & woocommerce


Hello I'm working on a woocommerce car rental shop and I have the following issue that I'm trying to fix. Given the following case: A client comes to rent a car with a date period of October 29 11:00 to November 12 11:00 with a total of 15 days of rentals. The base price of a car for rent is 17 euros/day with an extra fee of 5 euros/day for October and 17 euros/day with an extra fee of 10 euros/day for November. So now I have the following dump that shows the same example :

Base Price: 17
Array
(
    [10] => Array
        (
            [fee] => 5.00
            [days] => 3
        )

    [11] => Array
        (
            [fee] => 10.00
            [days] => 12
        )

)

When I do the math I'm getting 66 euros for first period 29 October to 31 October and 324 euros for 1 November - 12 November period with a total of 390 euro. If I were to do an average price 390 / 15 and then set that as price for the car the total would be incorrect. How could I change the way wocommerce calculates total to calcule the price for the car + other options for rental. ?


Solution

  • Here is a realy basic example

    $price = '17.00';
    $period_range = array(
        '10' => array(
            'fee' => 5.00,
            'days' => 3
        ),
        '11' => array(
            'fee' => 10.00,
            'days' => 12
        )
    );
    $charge = array();
    foreach($period_range as $range) {
        $charge[] = round($range['fee'] * $range['days']);
    }
    
    $total = array_sum($charge) + $price;
    echo $total;
    //this will return 152