Check if there are products on sale in Wordpress with Woocoomerce

Asked

Viewed 169 times

1

I am building a Wordpress site with Woocommerce and want to make the check that if there are products on sale with the shortcode, if exist returns me the title + products, if there is no product on sale the block some. I had some ideas and one of them was:

<?php
    //Produtos em promoção
    //Armazena o shortcode ( mas acredito estar errado ) 
    $produto_promocao = do_shortcode('[sale_products limit="4" columns="4" orderby="popularity" class="quick-sale" on_sale="true" ]'); 
    //Verifica se existe os produtos, se existir, exibe o titulo e os produtos por 
    //meio do shortcode, caso ao contrario, retorna vazio 
    if($produto_promocao != false){
        _e( 'Promotional Products', 'wordpress' ); //Exibe titulo
        echo $produto_promocao; //Exibe produtos
    } else { 
    return ''; //Retorna vazio
    }

?>

But it didn’t work, and I can only imagine why. I imagine I need some identifier for the products on sale, or make some other logic.. I accept constructive criticism, I am training ! I apologize beforehand already if I have not graduated here right, I am beginner. Hugs

2 answers

1


Talk buddy, all right?

So man, if I understand correctly, you can display your products on sale this way below:

<?php if( woo_have_onsale_products() ) { ?>
<?php echo do_shortcode('[products limit="4"  class="teste" columns="4" orderby="popularity" on_sale="true" ]'); ?>
<?php } else { ?>
    <div>Nenhum produto em promoção no momento.</div>
<?php } ?>

If you do not find it necessary to make the comparison with the Else, simply remove and leave only the if.

Then for your logic to work perfectly, insert this code below in the functions.php of your theme:

function woo_have_onsale_products() {

global $woocommerce;
// Get products on sale
$product_ids_on_sale = array_filter( wc_get_product_ids_on_sale() ); // woocommerce_get_product_ids_on_sale() if WC < 2.1
if( !empty( $product_ids_on_sale ) ) {
    return true;
} else {
    return false;
}

}

Take a test there and give me a return.

Take a look also at the Woocommerce documentation page. There you will have several other display options to apply in your theme.

https://docs.woocommerce.com/document/woocommerce-shortcodes/

Thanks!

  • Man, thank you so much ! Super worked, I just added the title just above the echo shortcode. Please, could you explain your line of thought ? I would like to understand how you thought to reach this solution ! Hugs

  • Dude, basically I created a function by defining a variable and filtering the ids of the products on sale through the event "wc_get_product_ids_on_sale", which comes from Woocommerce, to be able to make the comparison right below. Go to this page ( https://docs.woocommerce.com/wc-apidocs/package-WooCommerce.Functions.html ), and check out the available methods.

0

Exactly, and in a slightly more simplified way I think it could look like this:

if(woo_have_onsale_products()) {
  echo do_shortcode('[products limit="4"  class="teste" columns="4" orderby="popularity" on_sale="true" ]');
} else {
  echo '<div>Nenhum produto em promoção no momento.</div>';
}

and the function to check if there are products on-sale would be:

function woo_have_onsale_products() {
  return !empty(wc_get_product_ids_on_sale());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.