为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
Smart猫小萌 2023-09-08 18:24:52
我需要在特定模板中显示产品。这是模板代码:<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 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

只需使用以下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>


查看完整回答
反对 回复 2023-09-08
  • 1 回答
  • 0 关注
  • 78 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号