在Woocommerce中,我试图在价格之前添加文本,如果高于59欧元
我尝试了以下代码(以及其他代码),但它们没有工作:
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
if ($price>='59'){
$new_price = $price .__('(GRATIS!)');
}
return $new_price;
}如果产品价格高于59,如何在产品价格之前添加(GRATIS!)文本?
发布于 2018-02-01 00:26:43
更新:您应该尝试以下重新访问的函数代码:
add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
// Only on frontend and excluding min/max prices on variable products
if( is_admin() || $product->is_type('variable') )
return $price_html;
// Get product price
$price = (float) $product->get_price(); // Regular price
if( $price > 15 )
$price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
return $price_html;
}代码位于活动子主题(或活动主题)的function.php文件中。
测试和工作。
对于单个产品页,只有,您将替换以下行: if( $price > 15 ){ 通过这一点: if( $price > 15 && is_product() ){
对于单一产品页和归档页面,您将替换以下行: if( $price > 15 ){ 通过这一点: 如果( $price > 15 && ( is_product() )\x=\x{
https://stackoverflow.com/questions/48552886
复制相似问题