猿问

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

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


这是模板代码:


<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(); ?>

我怎样才能改变这个变量?


Smart猫小萌
浏览 79回答 1
1回答

PIPIONE

只需使用以下WC_Product方法get_price_html()即可:<div class="searchproduct">&nbsp; &nbsp; <div class="searchsingle">&nbsp; &nbsp; &nbsp; &nbsp; <?php the_post_thumbnail( 'medium_large' ); ?>&nbsp; &nbsp; &nbsp; &nbsp; <h2 class="woocommerce-loop-product__title"><?php echo esc_html( get_the_title() ); ?></h2>&nbsp; &nbsp; &nbsp; &nbsp; <div class="price"><?php echo $product->get_price_html(); ?></div>&nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php the_permalink(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="button"><?php _e("Ajouter au panier", 'woocommerce'); ?</div>&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div></div>或者,如果您在产品促销时只想要一种格式的价格,您将使用:<div class="searchproduct">&nbsp; &nbsp; <div class="searchsingle">&nbsp; &nbsp; &nbsp; &nbsp; <?php the_post_thumbnail( 'medium_large' ); ?>&nbsp; &nbsp; &nbsp; &nbsp; <h2 class="woocommerce-loop-product__title"><?php echo esc_html( get_the_title() ); ?></h2>&nbsp; &nbsp; &nbsp; &nbsp; <div class="price"><?php wc_price( wc_get_price_to_display($product) ); ?></div>&nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php the_permalink(); ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="button"><?php _e("Ajouter au panier", 'woocommerce'); ?</div>&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; </div></div>
随时随地看视频慕课网APP
我要回答