Skip to content
Advertisement

Display the WooCommerce product price (with discount) in a custom template

I need to display a product in a specific template.

Here is the template code:

<div class="searchproduct">
    <div class="searchsingle">
        <?php the_post_thumbnail( 'medium_large' ); ?>
        <h2 class="woocommerce-loop-product__title"><?php echo esc_html( get_the_title() ); ?></h2>
        <div class="price"><span class="woocommerce-Price-amount amount"><?php echo $product->get_price(); ?><span class="woocommerce-Price-currencySymbol"> €</span></span></div>
        <a href="<?php the_permalink(); ?>">
            <div class="button">Ajouter au panier</div>
        </a>
    </div>
</div>

I would like to display the price (with discount) and the old price.

<?php echo $product->get_price(); ?>

how can I change this variable?

Advertisement

Answer

Simply use instead the WC_Product method get_price_html() like:

<div class="searchproduct">
    <div class="searchsingle">
        <?php the_post_thumbnail( 'medium_large' ); ?>
        <h2 class="woocommerce-loop-product__title"><?php echo esc_html( get_the_title() ); ?></h2>
        <div class="price"><?php echo $product->get_price_html(); ?></div>
        <a href="<?php the_permalink(); ?>">
            <div class="button"><?php _e("Ajouter au panier", 'woocommerce'); ?</div>
        </a>
    </div>
</div>

Or if you want only one formatted price when product is on sale, you will use:

<div class="searchproduct">
    <div class="searchsingle">
        <?php the_post_thumbnail( 'medium_large' ); ?>
        <h2 class="woocommerce-loop-product__title"><?php echo esc_html( get_the_title() ); ?></h2>
        <div class="price"><?php wc_price( wc_get_price_to_display($product) ); ?></div>
        <a href="<?php the_permalink(); ?>">
            <div class="button"><?php _e("Ajouter au panier", 'woocommerce'); ?</div>
        </a>
    </div>
</div>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement