
The video:
https://www.youtube.com/watch?v=X7KbIP4Qm-Y
The function code to determine if a product is on sale, and then return the value 'true'. The function checks simple, variable and grouped products. This is then used in the conditional content to display or hide the content.
function by_check_prod_sale() {
global $product;
// get products on sale
$product_ids_on_sale = wc_get_product_ids_on_sale();
// crrent product id
$product_id = $product->get_id();
// get product meta to check if grouped product
$product = wc_get_product($product_id);
if( $product->is_type( 'grouped' ) ){
// get products in grouped product
$children = $product->get_children();
// check if child products on sale
foreach($children as $child){
if( in_array( $child, $product_ids_on_sale)) {
return 'true';
}}}
// check if product id in array of products on sale
if ( in_array( $product_id, $product_ids_on_sale) ) {
return 'true';
}
}
This function checks to see the date a product is published. If less than the number of days specified the value of 'true' is returned. This is then used in the conditional content condition to show the new badge.
function by_check_date_created() {
global $product;
// Get the date for the product published and current date
$datetime_created = $product->get_date_created(); // Get product created datetime
$timestamp_created = $datetime_created->getTimestamp(); // product created timestamp
$datetime_now = new WC_DateTime(); // Get now datetime (from Woocommerce datetime object)
$timestamp_now = $datetime_now->getTimestamp(); // Get now timestamp
$time_difference = $timestamp_now - $timestamp_created; // Difference in seconds
$day_published = 10 * 24 * 60 * 60; // 10 days in seconds
// If the difference is less than 60, apply "NEW" label to product archive.
if ( $time_difference < $day_published ) {
return 'true';
}
}