如果没有促销价,如何显示woocommerce促销价或正常价 [英] How to display woocommerce sale price or regular price if there is no sale price

查看:14
本文介绍了如果没有促销价,如何显示woocommerce促销价或正常价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 woocommerce 产品插件插件,我想在插件的下拉部分显示产品的价格.目前我拥有的代码是这个

I'm using a woocommerce product addons plugin where I would like to display the price of a product in my dropdown section of the addons. Currently the code I have is this

    <?php
$loop = 0;
$current_value = isset( $_POST['addon-' . sanitize_title( $addon['field-name'] ) ] ) ? wc_clean( $_POST[ 'addon-' . sanitize_title( $addon['field-name'] ) ] ) : '';
global $product;
?>
<p class="form-row form-row-wide addon-wrap-<?php echo sanitize_title( $addon['field-name'] ); ?>">
    <select class="addon addon-select" name="addon-<?php echo sanitize_title( $addon['field-name'] ); ?>">

        <?php if ( ! isset( $addon['required'] ) ) : ?>
            <option value=""><?php _e('None', 'woocommerce-product-addons'); ?></option>
        <?php else : ?>
            <!--<option value=""><?php _e('Select an option...', 'woocommerce-product-addons'); ?></option>-->
        <?php endif; ?>

        <?php foreach ( $addon['options'] as $i => $option ) :
            $loop ++;
            $price = apply_filters( 'woocommerce_product_addons_option_price',
                $option['price'] > 0 ? ' + ' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . '' : '',
                $option,
                $i,
                'select'
            );
            ?>
            <option data-raw-price="<?php echo esc_attr( $option['price'] ); ?>" data-price="<?php echo get_product_addon_price_for_display( $option['price'] ); ?>" value="<?php echo sanitize_title( $option['label'] ) . '-' . $loop; ?>" <?php selected( $current_value, sanitize_title( $option['label'] ) . '-' . $loop ); ?>><?php echo wptexturize( $option['label'] . ' (' ); echo balanceTags($product->get_price_html()) . $price ?>)</option>
        <?php endforeach; ?>

    </select>
</p>

我正在使用这个回声

$product->get_price_html()

这样做虽然是显示 $"sale price" $"price" 但我只想显示销售价格,如果没有销售价格,我只想显示产品价格.看看下面的代码,我将如何做到这一点?

what this does though is display the $"sale price" $"price" but I just want to display just the sale price or just the product price if there is no sale price. Looking at the code below, how would I accomplish this?

推荐答案

非常简单.我们将编写一个自定义函数,首先确定产品是否在售.然后它会根据之前定义的销售条件返回常规价格或销售价格.所以函数将是:

Pretty simple. We'll write a custom function that will first be sure on that if the product is on sale or not. Then it'll return regular or sale price based on the sale condition it defined previously. So the function will be:

/**
 * Returns product price based on sales.
 * 
 * @return string
 */
function the_dramatist_price_show() {
    global $product;
    if( $product->is_on_sale() ) {
        return $product->get_sale_price();
    }
    return $product->get_regular_price();
}

现在调用这个函数 the_dramatist_price_show() 而不是 $product->get_price_html().您将获得基于是否在售的价格,没有货币符号.

Now call this function the_dramatist_price_show() instead of $product->get_price_html(). You'll get the price based on is on sale or not without currency symbol.

希望有所帮助.

这篇关于如果没有促销价,如何显示woocommerce促销价或正常价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆