php 如果没有促销价,如何显示woocommerce促销价或正常价
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43962716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display woocommerce sale price or regular price if there is no sale price
提问by JPWeb
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
我正在使用 woocommerce 产品插件插件,我想在插件的下拉部分显示产品的价格。目前我拥有的代码是这个
<?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>
I'm using this echo
我正在使用这个回声
$product->get_price_html()
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?
虽然这样做是显示 $"sale price" $"price" 但我只想显示销售价格或如果没有销售价格,则只显示产品价格。看看下面的代码,我将如何做到这一点?
回答by CodeMascot
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();
}
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.
现在调用这个函数the_dramatist_price_show()
而不是$product->get_price_html()
. 您将获得基于销售与否的价格,没有货币符号。
Hope that helps.
希望有帮助。
回答by Umair Mehmood
This article worked for me. you can also try this one.
这篇文章对我有用。你也可以试试这个。
If you want to get the regular price or sale price of a woocommerce product and you are getting nothing you need to know the following:
如果您想获得 woocommerce 产品的正常价格或销售价格而您一无所获,则需要了解以下内容:
If the product has no variations you can get the regular price and sales price just like that:
如果产品没有变化,您可以像这样获得正常价格和销售价格:
Get the price of a simple procut
获取简单产品的价格
<?php
#the product must be instantiated above like $product = new WC_Product();
echo $product->regular_price;
echo $product->sale_price;
?>
If the product has variations you will get nothing if you will use the code above.
如果产品有变化,如果您使用上面的代码,您将一无所获。
You need to get the variation product prices.
您需要获取变化的产品价格。
Get the price of a product with variations
获取有变化的产品的价格
#1 Get product variations
$product_variations = $product->get_available_variations();
#2 Get one variation id of a product
$variation_product_id = $product_variations [0]['variation_id'];
#3 Create the product object
$variation_product = new WC_Product_Variation( $variation_product_id );
#4 Use the variation product object to get the variation prices
echo $variation_product ->regular_price;
echo $variation_product ->sale_price;
That should be all.
这应该是全部。
Enjoy.
享受。
Source: http://www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/
资料来源:http: //www.w3bdeveloper.com/how-to/how-to-get-regular-price-of-a-product-in-wordpress-woocommerce/
回答by Mike
That's what works for me with Wordpress 5.1 and WooCommerce 3.5.5 :
这就是 Wordpress 5.1 和 WooCommerce 3.5.5 对我有用的方法:
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$price_sale = get_post_meta( get_the_ID(), '_sale_price', true);
if ($price_sale !== "") {
echo $price_sale;
} else {
echo $price;
}