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

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. ?

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

Related

How to get date of two month old date from current date

I am trying to pull data (some transaction related data) from DB. To pull the data, I am passing start date and end date as an argument to the query.
Here I need to pull the data of last 2 months. i.e., Start time would be Jun 01, 2022 and End time would be Aug 01, 2022.
Below is the script:
#!/usr/bin/perl
use strict;
use warnings;
use DateTime;
use Date::Format;
use Date::Parse;
my $nowdate = DateTime->now(time_zone => 'local');
my ($month, $year) = ($nowdate->month, $nowdate->year);
my $date = DateTime->new(
year => $year,
month => $month,
day => 1,
);
my $end_time = str2time($date);
print "END:$end_time\n";
print time2str("%d-%m-%Y %T", $end_time)."\n"; #printing just to see in human readable format
my $start_time = $date->clone;
$start_time->add( months => 1 )->subtract( days => 92 );
$start_time = str2time($start_time);
print "START:$start_time\n";
print time2str("%d-%m-%Y %T", $start_time)."\n"; #printing just to see in human readable format
I have two issues over here:
I am using DateTime object two times to get end time. Can it be done in one shot?
$start_time->add( months => 1 )->subtract( days => 92 ); In this line of code, I have to explicitly mention subtract 92 days, which wouldn't be right always. Since some months have 30 days or 31 days or even 29 days. How can I get 2 month's beginning day date?
Another example: Lets assume if we are in September 2022, then Start time and End time would be Jul 01, 2022 and Sep 01, 2022 respectively.
Note: Perl version is 5.16.3
It would be good If I can do it with Core modules which comes with 5.16.3
You could simplify it by using truncate(to => 'month') to get to the first in the current month:
my $end = DateTime->now(time_zone => 'local')
->truncate(to => 'month');
This may however fail on a day without a midnight so this may be an option:
my $end = DateTime->now(time_zone => 'local')
->set_time_zone('floating')
->truncate(to => 'month');
Then subtract the number of months to get the start date:
my $start = $end->clone->subtract( months => 2 );
Then:
my $start_time = str2time($start);
my $end_time = str2time($end);
print "START:$start_time\n";
print time2str("%d-%m-%Y %T", $start_time)."\n";
print "END:$end_time\n";
print time2str("%d-%m-%Y %T", $end_time)."\n";
Possible output:
START:1654034400
01-06-2022 00:00:00
END:1659304800
01-08-2022 00:00:00

Time loop for WooCommerce checkout select option

I need your help please.
I have time picker on my WordPress site, it looks like this:
woocommerce_form_field( 'time', array(
'type' => 'select',
'label' => __('Delivery Time', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => 'true',
'options' => array(
'9 AM - 10 AM' => __('9 AM - 10 AM', 'woocommerce' ),
'10 AM - 11 AM' => __('10 AM - 11 AM', 'woocommerce' ),
'11 AM - 12 PM' => __('11 AM - 12 PM', 'woocommerce' ),
'12 PM - 1 PM' => __('12 PM - 1 PM', 'woocommerce' ),
'1 PM - 2 PM' => __('1 PM - 2 PM', 'woocommerce' ),
'2 PM - 3 PM' => __('2 PM - 3 PM', 'woocommerce' ),
'3 PM - 4 PM' => __('3 PM - 4 PM', 'woocommerce' ),
'4 PM - 5 PM' => __('4 PM - 5 PM', 'woocommerce' ),
'5 PM - 6 PM' => __('5 PM - 6 PM', 'woocommerce' ),
'6 PM - 7 PM' => __('6 PM - 7 PM', 'woocommerce' ),
'7 PM - 8 PM' => __('7 PM - 8 PM', 'woocommerce' ),
'8 PM - 9 PM' => __('8 PM - 9 PM', 'woocommerce' ),
'9 PM - 10 PM' => __('9 PM - 10 PM', 'woocommerce' ),
'10 PM - 11 PM' => __('10 PM - 11 PM', 'woocommerce' ),
'11 PM - 12 AM' => __('11 PM - 12 AM', 'woocommerce' )
),
), $checkout->get_value( 'time' ));
However I want options are based on some rules:
1. If client choose todays date on date picker, no options will be avalible.
2. If client will choose tomorrows date, it will show options(Current Time + 24hours)
3. If client will choose date after tomorrow, all options will be avalible.
Please, I need your help.
Thank you.

wordpress how to 3 columns post list and columns wrap

I am trying to load a WordPress blog list with the following structure.
this is my code
enter image description here
(div class="left-wrap)
post-1
post-4
post-7
(div class="right-wrap)
post-2
post-3
post-5
post-6
(/div)
POST 1 | POST 2 , POST 3
POST 4 | POST 5 , POST 6
POST 7 | POST 8 , POST 7
Thank you for your help.
Populate the post array like the below and separately loop through for the left and right section
<?php
$posts = ["one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten"];
$left_array = [];
$right_array = [];
foreach($posts as $key => $post){
if(($key+1) % 3 == 1){
$left_array[] = $post;
}
else{
$right_array[] = $post;
}
}
print_r($left_array);
print_r($right_array);
?>
OUTPUT
LEFT ARRAY
Array
(
[0] => one
[1] => four
[2] => seven
[3] => ten
)
RIGHT ARRAY
Array
(
[0] => two
[1] => three
[2] => five
[3] => six
[4] => eight
[5] => nine
)

WordPress | WP_Query | Get latest port from category and sub-categories

I have write a theme for WordPress and I like to have the latest posts posted in category and it's sub-categories to be displayed on top of any other post.
An example. Lets say I have the following categories:
Cat 1
Cat 1 - 1
Cat 1 - 2
Cat 1 - 2 - 1
And then I create the following posts :
Post #5 | Cat 1 - 2 | Date 2013
Post #4 | Cat 1 - 1 | Date 2012
Post #3 | Cat 1 - 2 | Date 2011
Post #2 | Cat 1 | Date 2010
Post #1 | Cat 1 - 1 - 2 | Date 2009
In the front end, when I navigate to Cat 1 I do not get as latest post the Post #5 that belong to Cat 1 - 2 where it is sub category of the Cat 1, But instead I am getting the Post #2.
Currently I am using this code :
$categoryID = get_query_var('cat');
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'category__in' => array($categoryID),
'post_status' => 'publish'
);
$eiPost = new WP_Query($args);
The problem is that this code return the latest post only from the top level category and not from the sub categories. How can I modify this code, in order to retrieve the latest posts from all the sub-categories and the top category ?
Is there any solution to this problem ?
'category__in' only displays posts from that category, not children categories.
Try using 'cat' => $categoryID insted. So your $args would be:
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'cat' => $categoryID,
'post_status' => 'publish'
);

kendo ui dataviz chart aggregate includes data from wrong month

I'm building a basic line chart in Kendo UI Dataviz and I'm having a problem with the aggregation method. The goal is to group my sales data by month and aggregate the Sum(Amount) which seems to work in my chart but I'm experiencing a bug that causes data from the first day of October to be included in the sum for September. Oct 2 shows in October but Oct 1 is included in September's total.
The data is:
10/1/2013 12:00:00 AM, 22964.5000
10/2/2013 12:00:00 AM, 6762.9400
Html.Kendo().Chart(Model)
.Name("revenue")
.Title("Revenue Activity by Month")
.Legend(legend => legend
.Position(ChartLegendPosition.Bottom)
)
.Series(series =>
{
series.Area(s => s.TotalRevenue, categoryExpression: model => model.Date).Aggregate(ChartSeriesAggregate.Sum).Name("Total Revenue").Color("#73c100");
series.Line(s => s.RevenueSubscriber, categoryExpression: model => model.Date).Aggregate(ChartSeriesAggregate.Sum).Name("Subscriber Revenue");
series.Line(s => s.RevenueNonSubscriber, categoryExpression: model => model.Date).Aggregate(ChartSeriesAggregate.Sum).Name("Non-Subscriber Revenue");
})
.CategoryAxis(axis => axis.Date()
.BaseUnit(ChartAxisBaseUnit.Months)
)
.ValueAxis(axis => axis
.Numeric("revenue")
.Labels(labels => labels.Format("{0:C}"))
.Line(line => line.Visible(false))
)
.Tooltip(tooltip => tooltip
.Visible(true)
.Format("{0:C}")
)
What I receive is a chart with two points on the X Axis. The first point is Sept 2013 and includes $22,964. The second point is Oct 2013 and includes $6,762.

Resources