Reputation: 21
I have a woocommerce site that I am currently adding functionality to, I have products which have expiry dates and would like to display all products which have an expiry date of today on a particular page. In addition to this I would also like to have all products which have an expiry date of tomorrow displayed on another page.
I found the following documentation online: Adding Custom Parameter Support
I have added this to my site via the snippets plugin and modified it for my expiry date attribute
/** * Handle a custom 'expirydate' query var to get products with the 'expirydate' meta.
* @param array $query - Args for WP_Query.
* @param array $query_vars - Query vars from WC_Product_Query.
* @return array modified $query
*/
function handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['expirydate'] ) ) {
$query['meta_query'][] = array(
'key' => 'expirydate',
'value' => esc_attr( $query_vars['expirydate'] ),
);
}
return $query;
}
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
I then have another snippet added which is to create the function to call for the products which have the expiry date of today and to this I've added a shortcode to easily call this on my site on the pages I want it to be displayed.
// Display products with expiry date today
function expiry_Date_Today()
{
$todaysdate = date(d/m/Y);
$products = wc_get_products( array( 'expirydate' => 'todaysdate' ) );
}
add_shortcode( 'expirydatetoday', 'expiry_Date_Today');
Now my issue here is that when I use the shortcode on my page I get nothing in return. Just a blank empty space. I assume this is because I need to request the output in a format that would show the products. I would preferably want this to appear as an image, title and the add to cart button similar to what you you would generally see when viewing the contents of a category page.
I have searched online but cannot find this information so could someone point me in the right direction here or if there appears to be something obviously wrong with my code could you assist?
Many thanks, TMS.
Upvotes: 2
Views: 1569
Reputation: 254271
Update
There are multiple mistakes in your code and your expired date should be always set in 'Y-m-d'
compatible date format instead, to be able to compare dates if required.
Try the following, that will get expired products list from today date:
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handle_custom_query_var', 10, 2 );
function handle_custom_query_var( $query, $query_vars ) {
if ( ! empty( $query_vars['expirydate'] ) ) {
$query['meta_query'][] = array(
'key' => 'expirydate',
'value' => esc_attr( $query_vars['expirydate'] ),
'compare' => '=',
'type' => 'DATE'
);
}
return $query;
}
function get_products_expired_today($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'date' => date('Y-m-d'), // default date is current date in 'd/m/Y' format
), $atts, 'expirydatetoday' ) );
$products = wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'expirydate' => $date,
) );
$html = '<ul class="expired-products">';
// Products loop
foreach( $products as $product ) {
$html .= '<li><a href="' . $product->get_permalink() . '">' . $product->get_name() . '</a></li>';
}
return $html . '</ul>';
}
add_shortcode( 'expirydatetoday', 'get_products_expired_today');
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Now if you want to get all expired products instead of the "today" products, you will need first to change the date format on your expirydate
custom field value for all products to "YYYY`MM-DD".
You will have to change:
'compare' => '=',
to:
'compare' => '<=',
It also works.
Upvotes: 1