在自定义模板中显示WooCommerce产品价格(带折扣)

时间:2020-07-30 13:01:35

标签: php wordpress woocommerce product price

我需要在特定模板中显示产品。

这是模板代码:

<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>

我想显示价格(有折扣)和旧价格。

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

如何更改此变量?

1 个答案:

答案 0 :(得分:1)

请改为使用WC_Product方法get_price_html(),例如:

<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>

或者,如果您只想在商品销售时以一种格式化的价格购买商品,则可以使用:

<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>