Skip Navigation

[Resolved] Woocommerce ‘ is featured product ‘ as conditional output

This thread is resolved. Here is a description of the problem and solution.

Problem:
The issue here is that the user wanted to check if a product is featured and display some special output for featured products.

Solution:
Actually you should be able to tell if a product is feature or not by using this shortcode.

// Add Shortcode
function featured_product( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'product_id' => '',
        ),
        $atts
    );
 
    $product = wc_get_product($atts['product_id']);
    return $product->is_featured();
 
}
add_shortcode( 'featured_product', 'featured_product' );

Add it to your functions.php file and you can use it by doing this [featured_product product_id='[wpv-post-id]'] and it will return 1 or 0 if the product is featured or not.

Just add featured_product to the 3rd party shortcode arguments for this to work in our views conditionals.

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Charlie 6 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#632249

I have a table displaying products owned by the logged in user and would like to add a cell <td> for each row which displays the word 'Featured' if a product is a featured product.

Are you able to supply the correct conditional code along the lines of:

[wpv-conditional if="( my_custom_function() eq 'hello' )"]
This function returns hello[/wpv-conditional]

or

[wpv-conditional if="( MyClass::my_method() eq 'hello' )"]
This function returns hello[/wpv-conditional]

#632357

I thought this might work but it breaks the view:

<td>[wpv-conditional if="( WC_Product::is_featured() )"]Featured[/wpv-conditional]</td>

I registered the class WC_Product::is_featured in Toolset > Settings > Front-end Content > Functions inside conditional evaluations

#632536

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Charlie,

Thank you for contacting our support forum.

Actually you should be able to tell if a product is feature or not by using this shortcode.

// Add Shortcode
function featured_product( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'product_id' => '',
		),
		$atts
	);

	$product = wc_get_product($atts['product_id']);
	return $product->is_featured();

}
add_shortcode( 'featured_product', 'featured_product' );

Add it to your functions.php file and you can use it by doing this [featured_product product_id='[wpv-post-id]'] and it will return 1 or 0 if the product is featured or not.

Just add featured_product to the 3rd party shortcode arguments for this to work in our views conditionals.

Thanks,
Shane

#636123

Thank you Shane. Your solution worked. I can now display whether product is featured using this conditional output:

<td>[wpv-conditional if="( '[featured_product product_id = '[wpv-post-id]']' eq '1' )"]Featured[/wpv-conditional]</td>