woocommerce booking subtract weeks or days from date picked - woocommerce

I am using Woothemes Woocommerce Bookings. I want users to be able to enter a date in the product booking date picker and have the system make the booking three weeks prior to that date.
e.g. customer uses the date picker to select 28/04/2018 (28th April 2018) but the booking is made for 07/04/2018 (7th April 2018).
All bookings are made in blocks of 5 weeks (35 days) but they enter in a date that results in their booking being 3 weeks (21 days) before that date, and 2 weeks (14 days) after. Customers only select one date, there is no start and end date for them to select.

The documentation references a hook called woocommerce_new_booking that you can use in order to modify the booking date before it's saved to the database. This code will modify the booking start and end date to 3 weeks prior to the date they select. Add this code to your functions.php file.
add_action( 'woocommerce_new_booking', 'modify_woocommerce_booking_date');
function modify_woocommerce_booking_date( $booking_id ) {
$booking = new WC_Booking( $booking_id );
$booking_start = $booking->get_start();
$booking_end = $booking->get_end();
$new_booking_start = strtotime ( '-3 week' , $booking_start ) ;
$new_booking_end = strtotime ( '-3 week' , $booking_end ) ;
$booking->set_start( $new_booking_start );
$booking->set_end($new_booking_end);
$booking->save();
}

Related

Adding DATE and TIME not working when TIME is midnight

I have created a table using the following DAX.
Date Selection =
ADDCOLUMNS (
CROSSJOIN(
CALENDAR ( MIN ( 'Date'[Date] ), MAX ( 'Date'[Date] )),
SELECTCOLUMNS(GENERATESERIES(0,23.5,.5),"Time",TIME([Value],([value]-INT([value]))*60,0))
),
"Visual Date", FORMAT([Date],"dd/mm/yy") + [Time],
"Order By", FORMAT([Date],"yyyy") & "/" & FORMAT([Date],"MM"),
"Type", "Half Hourly",
"Order", 5
)
So my issue is when I add my DATE and TIME column together to create a column called Visual Date, everything looks fine but when it reaches to the time 00:00:00 it does not seem to add that time to the corresponding day, it leaves it just as it is.
Is there something that I need to include in order to see 01/01/2021 00:00:00 ?
This is just a visual formatting issue. In the tab go to the Format field and select whatever you want to see.

Sql query to calculate active count for aparticular date of every month

I have data for the user regarding his subscription transaction date.
suppose if the user has a monthly subscription and the subscription date in on 4th of every month.
Then how can I count that user as an active user for 4th of every month
Data Available:
Data Required:
Since subscription begins from October.
I believe that the following will do what you wish :-
WITH potential_dates(d) AS
(
SELECT date('2019-10-01' /* Start date */)
UNION ALL
SELECT date(d,'+1 days')
FROM potential_dates
WHERE d < '2020-01-01' /* End date */
LIMIT 1000 /* secondary/fail safe end of recursion */
)
SELECT
subscriptiondate||strftime('/%m/%Y',d) AS Date,
count() AS userCount
FROM potential_dates
JOIN subscription ON subscriptiondate = 0 + substr(d,9)
GROUP BY date
ORDER BY d
;
based upon the given data, the above results in :-
If there data were :-
Then the result would be :-

Get if the current user has an active subscription with trial period

I'm developing a website in WordPress with WooCommerce. I am using Woocommerce Subscription. I have a product with a trial period. Now I want to change the next payment date based on the trial period. How can I get the trial period date and Next payment date? So that I can change the Second payment date. I tried using the below-mentioned code but didn't find any solution.
<?php if (has_woocommerce_subscription('','','active') && WC_Subscriptions_Product::get_trial_length( $product_id ) > 10) {
$trial_end = WC_Subscription::get_date( 'trial_end');
echo $trial_end;
}
?>
<?php WC_Subscription::get_date( $date_type, $timezone ) ?>
$date_type
(string) (required) The type of date to get, can be 'start', 'trial_end', 'next_payment', 'last_payment' or 'end'. Default: None
$timezone
(string) (optional) The timezone to use for the returned date, either ‘gmt’ or ‘site’. Default ‘gmt’.
You will get complete document here.
https://docs.woocommerce.com/document/subscriptions/develop/functions/
Update :
$product = wc_get_product($product_id);
$period = WC_Subscriptions_Product::get_period($product);
var_dump($product);
Add output here in question. You will find start and end date as well as next payment date.

How to create trigger based on time period it has to do sum function

I want to create a trigger in oracle 11g - every 5 minutes it has to sum the values of price column in order table. How would i ?
The correct way to do this would be to have a materialized view which calculates the total.
create table t23 (id number primary key , qty number);
create materialized view log on t23;
create materialized view t23_total
refresh force
start with sysdate + 5/(60*24)
next sysdate + 5/(60*24)
as
select sum(qty) as total_qty
from t23
/
This is the most efficient way to main a total. This kicks off a refresh every five minutes. If you wanted to maintain the total in real time you could just use
create materialized view t23_total
refresh on commit
as
select sum(qty) as total_qty
from t23
/
You may create a scheduler job to run every 5 minutes.
declare
v_anon_block varchar2(500) :=
q'{begin
insert into mylog(log_date,total_price) select SYSDATE, SUM(price)
from order;
commit;
end;}';
begin
dbms_scheduler.create_job (
job_name => 'SUM_PRICE_JOB',
job_type => 'PLSQL_BLOCK',
job_action => v_anon_block,
start_date => SYSDATE,
repeat_interval => 'FREQ=MINUTELY;INTERVAL=5;',
enabled => TRUE,
comments => '5 minute Refresh');
end;
/
Why do you want to work with "old" data? I mean, what's the purpose in calculating that value every 5 minutes? Have it available in time, live, per request. Create a view or a function that does that, such as
create or replace view v_sum_price as
select sum(price)
from orders_table;
or
create or replace function f_sum_price
return number
as
retval number;
begin
select sum(price)
into retval
from orders_table;
return retval;
end;
Both of these most probably lack some additional info (order ID? Item ID? Something else?). Furthermore, I can't see a purpose in summing price value in orders table. How do you use it? For example, for a restaurant: ORDERS table contains the following prices:
ITEM PRICE QUANTITY
Coke 1.5 2
Burger 4.0 2
Wine 2.3 1
Salad 0.8 1
and now you know that sum of the PRICE column equals 8.6. What useful information does it contain? Absolutely none (or I can't see it).
Maybe it would help if you described what problem you're trying to solve.

Query/return records with dates between Sunday and Saturday?

I've searched this site and others, but couldn't find this exact scenario. I need to add a gridview to my page where the sqldatasource is based on a query that returns records that fall between Sunday and Saturday of the current week. (Each record has one date field) The records are for payroll purposes and the payroll week runs from Sunday to Saturday. I need to find all records that fall in the current pay week. Can anyone point me in the right direction to get started? I'm coding in VB.
This is a SQL question so you should tag it accordingly with your dbms.
Assuming you're using SQL-Server, DATEPART can be used to get the weekday as int of a datetime field and DATENAME can be used to get the name of the weekday.
For example(assuming Sunday to Saturday actually means from Monday to Friday):
SELECT t.*
FROM YourTable t
WHERE DATEPART(weekday, YourDateField) BETWEEN 2 AND 6
Note that it depends on regional settings what is the first day of the week.
Edit: If you want to select records from the current week.
SELECT t.*
FROM YourTable t
WHERE YourDateField >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
AND YourDateField < DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE()), 0)
http://msdn.microsoft.com/en-us/library/ms186819.aspx
I suppose you are searching a way to get the working time of an employee between two date
(from sunday to saturday). You need the exact date of Sunday before today.
This could be done via this function
Public Function FirstWeekDay(d as DateTime) As DateTime
While(d.DayOfWeek <> DayOfWeek.Sunday)
d.AddDays(-1)
End While
return d
End Function
The date returned is your startDate while your endDate depends where you want to stop your search.
According to your question this date is the following Saturday. Simply add 6 days to the startDate.
Now it's only a matter to pass the correct parameters to your query/storedprocedure:
SELECT w.* FROM workTime w WHERE w.empID = #empID AND w.workDate BETWEEN #startDate AND #endDate

Resources