Skip to content
Advertisement

How to restrict a div for on sale products in woocommerce

I have made a div with a specific class that shows the sale percentage of a product that is on sale. But i want to remove this div when the product is not on sale. I don`t know how to write the php code. please help me.

This is my div with php code that works well with on sale products:

<div class="pdoffpercent"> <?php echo $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); ?> %</div>

Advertisement

Answer

My problem solved with this snippet:

<?php $is_on_sale = $product->is_on_sale();
 if ( $is_on_sale ) { ?>
 <div class="pdoffpercent">
 %<?php echo $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); ?>
</div>
<?php } else{} ?>

It`s important to know how to use html in php.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement