I am trying to: Only display a view if a woocommerce purchase was made after 24 hours. If a user purchased within the last 24 hours, then display a message instead
Link to a page where the issue can be seen:
You will need login privileges
I expected to see:
To see a woocommerce product available as the purchase was done 3 days ago
Instead, I got: A message saying I cannot buy because I bought less than 24 hours agp
Setup:
Content Selection: Orders
Order by post date.
Limit 1
Query Filter: Date Condition
Select Posts with 'published date' 'between those'
Hour: current one
past one: 24 hours
Custom field filter: _customer_user
Use shortcode attribute: userid
Check status: wc-pending, wc-processing, wc-completed
View
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
<div class="alert alert-danger" role="alert">
You already placed an order today. You can order again in 24 hours. See you then!
</div>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpv-view name="woocommerce-items"]
[/wpv-no-items-found]
[wpv-layout-end]
Hi, I see that this View is included in the Content Template "Check Order", but I'm not clear how to see it on the front-end of your site. Is it currently anywhere visible? Also, can you provide login credentials for a User with a recent purchase, or is it okay for me to create a test User and test purchase? Private reply enabled again.
Actually I am wonering if I have the above correct? How does Woocommerce display the order date? Above I assumed it may be the post date, but now I am wondering if it uses a custom field for this, but I cannot find which field this may be. Already looked under Hidden custom fields but there is nothing indicating an Order Date there.
I think the problem with the View as it is set up now is that it's only checking the hour of publication without considering the date as well - so the filter isn't behaving as expected. Rather than try to build this type of query in Views, I think you'll have more precise results using a code-based date query. Try this:
Delete the date filter from your View
In your child theme's functions.php file, add the following code:
add_filter( 'wpv_filter_query', 'featured_products', 99, 3 );
function featured_products( $query_args,$views_settings, $view_id) {
if ($view_id == 12345){
$query_args['date_query'] = array(
array(
'after' => '24 hours ago'
),
);
}
return $query_args;
}
Replace 12345 with the numeric ID of this View
And then how do I display the view? Do I leave it all intact as it is right now but simply disable the date field? Also, this must be the the current logged in user only (who has made the purchase). I will test this soon as we are moving servers now, but please let me know 🙂 Thanks buddy.
And then how do I display the view? Do I leave it all intact as it is right now but simply disable the date field?
Delete the Post Date filter from your Query Filter, leave the other filters in place, and place the View just like you would normally place it in your post or template.
Also, this must be the the current logged in user only (who has made the purchase).
Assuming you are passing "userid" into the shortcode correctly, this should work. The filter changes we implemented in PHP will extend the user ID filter you have applied in the View editor, not replace them.