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

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.

Related

Override standard request processing for min and max price in WooCommerce filter to get products by price from custom field

The price of the item is recorded in the custom field $ custom_price of the table in the database, which has the item ID and price. Accordingly, when filtering by price on the category page, it filters only by the standard WooCommerce price field. I need to filter products in category by min and max price by custom price field, what is in custom table wp_product_prices. For example: I filtered products by price using woocommerce filter and got results on category page. The probem is: products don't showing because I using not standard field for price in my products https://prnt.sc/w60uj8 Found only the file https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-query.php, it has request processing by price, function price_filter_post_clauses ($ args, $ wp_query). Maybe there is some kind of hook for overriding or how you can implement it?
My custom prices table to products https://prnt.sc/w60t5p The field "product_id" is equal to product "ID" in wp_posts table.
for change query for min & max price use action "woocommerce_product_query" and deactivate standard filter 'posts_clauses'. Like this:
add_action('woocommerce_product_query', 'min_max_price_handler');
function min_max_price_handler($q) {
global $wpdb, $wp_query;
if (! $wp_query->is_main_query() || ( ! isset($_GET['max_price']) && ! isset($_GET['min_price']))) {
return;
}
// Remove default Woocommerce min/max price request handler
remove_filter('posts_clauses', [WC()->query, 'price_filter_post_clauses'], 10);
$current_min_price = isset($_GET['min_price']) ? floatval(wp_unslash($_GET['min_price'])) : 0;
$current_max_price = isset($_GET['max_price']) ? floatval(wp_unslash($_GET['max_price'])) : PHP_INT_MAX;
$sql = "SELECT DISTINCT product_id FROM wp_product_prices WHERE country=%s AND price >= %f AND price <= %f";
$results = $wpdb->get_results(
$wpdb->prepare(
$sql,
$customer_country,
$current_min_price,
$current_max_price
),
ARRAY_A
);
$results = array_column($results, 'product_id');
if (is_array($results)) {
$q->set('post__in', $results);
}
}

How to convert the date integer value of ACF plugin in WordPress?

I am using the jQuery Date Picker of WordPress ACF Plugin to make a custom Meta Box of a custom POST.
Now, when this Date Picker value is saved to the database table called xxx_postmeta I see that value is:
20190630
So then when I get that meta value using below code:
<?php echo date('F j, Y ', get_post_meta( get_the_ID(), 'article_1_pub_date', true)) ; ?>
then the date is showing wrong, Like this:
August 22, 1970
Is there any wrong or ACF issue?
I assume the date you saved is June 30, 2019. You can’t use PHP date() function on this string.
Convert to a date object and print:
$date = DateTime::createFromFormat('Ymd', '20190630');
echo $date->format('F j, Y ');

woocommerce booking subtract weeks or days from date picked

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();
}

How do I get the timestamp for my date

The current code below returns my current local time in wordpress and the result looks like this. 2013-07-29 13:45:42
I need to convert this to a timestamp format. What is the answer please?
echo date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
The PHP strtotime() function might work.
$timestamp = strtotime('2013-07-29 13:45:42');
EDIT
Here is your use case
$current_date = date('Y-m-d H:i:s'); ## Get current date
$timestamp = strtotime($current_date); ## Get timestamp of current date
echo $timestamp; ## Print timestamp
Come to find out I really just needed the actual date and not a timestamp to achieve my goal. So using this gives you wordpress current local time that is set on the settings page in the admin panel.
date_i18n('Y-m-d');

Format dates in a WordPress plugin

I'm modifying the Recent-Changes WordPress plugin to display dates. I can echo the date but can't format it; e.g., mm/dd/yyyy.
I'd like the post_modified date to be in mm/dd/yyyy.
I've tried--
echo '<li>'.$RecentChange->post_modified('m/d/Y').
-- but that caused the plugin to stop displaying posts, and generally broke the site.
Below is the relevant chunk from the plugin--
/* define full SQL request */
$rc_sql = "SELECT post_date, post_modified, post_title, ID FROM wp_posts WHERE ".$rc_content_sql." ORDER BY post_modified DESC LIMIT ".$rc_number;
global $wpdb;
echo $before_widget;
echo $before_title.$rc_title.$after_title.'<ul>';
$RecentChanges = $wpdb->get_results($rc_sql);
if ($RecentChanges)
foreach ($RecentChanges as $RecentChange) :
$rc_url = get_page_link($RecentChange->ID);
echo '<li>'.$RecentChange->post_modified.' <a href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
endforeach;
echo '</ul>'.$after_widget;
$wpdb->flush();
}
Try
<?php
mysql2date('m/d/Y', $RecentChange->post_modified);
?>
See reference.
Assuming RecentChanges->post_modified is a PHP date or time, you could wrap it in PHP's date function and format it how you want.
date("m/d/Y", $RecentChanges->post_modified);
So, your line would look like this:
echo '<li>'.date("m/d/Y", $RecentChanges->post_modified).' <a href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
It's likely that your code is breaking WordPress because the post_modified function is just a getter and doesn't take parameters.

Resources