14 December 2021

Adding For Sale and New badges to the repeater in Oxygen Builder

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';
    }
}
BYoung Design
CONTACT
Newsletter Form (#17)

Subscribe to our newsletter

Welcome to our Newsletter Subscription Center. Sign up in the newsletter form below to receive the latest news and updates.


Want to rank higher in search engines?

There are a number of factors that can affect how high your website ranks in search engines such as Google. These factors include the keywords you optimize your site for, your domain's age, keyword density, and the amount of, quality, backlinks to your site. In order to rank your site higher in search engines, you will need to work on gaining the proper backlinks and targeting high traffic keywords.

We can help you rank better.
Back to Top
envelopephonemenuchevron-upcross-circle