admin管理员组文章数量:1130349
I need to overwrite the wc_price in my theme's function as I need to make suitable changes into it based on my requirements.
I have used
add_filter( 'formatted_woocommerce_price', 'span_custom_prc', 10, 5 );
However In this case I was not able to change the value of $formatted_price So please guide me which hook do I need to use?
This is the Code I have used in my functions.php However Please check it below
function span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator, $price_format ) {
global $post;
$id = $post->ID;
echo $price;
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format,
'<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>',
$price );
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
if ( $marketstatus == "On" ) {
$return = '<span class="woocommerce-Price-amount amount"> ON' . $formatted_price . '</span>';
} else {
$return = '<span class="woocommerce-Price-amount amount" style="display:none">' . $formatted_price . '</span>';
}
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
I need to overwrite the wc_price in my theme's function as I need to make suitable changes into it based on my requirements.
I have used
add_filter( 'formatted_woocommerce_price', 'span_custom_prc', 10, 5 );
However In this case I was not able to change the value of $formatted_price So please guide me which hook do I need to use?
This is the Code I have used in my functions.php However Please check it below
function span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator, $price_format ) {
global $post;
$id = $post->ID;
echo $price;
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format,
'<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>',
$price );
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
if ( $marketstatus == "On" ) {
$return = '<span class="woocommerce-Price-amount amount"> ON' . $formatted_price . '</span>';
} else {
$return = '<span class="woocommerce-Price-amount amount" style="display:none">' . $formatted_price . '</span>';
}
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
Share
Improve this question
edited Dec 1, 2017 at 8:23
CodeMascot
4,5372 gold badges15 silver badges25 bronze badges
asked Dec 1, 2017 at 6:20
Pratik bhattPratik bhatt
1,2882 gold badges13 silver badges27 bronze badges
4
|
1 Answer
Reset to default 1Hmm how about this?
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0]*2.5);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
Taken from here:https://sceptermarketing/how-to-change-the-woocommerce-price-via-functions-php/
I need to overwrite the wc_price in my theme's function as I need to make suitable changes into it based on my requirements.
I have used
add_filter( 'formatted_woocommerce_price', 'span_custom_prc', 10, 5 );
However In this case I was not able to change the value of $formatted_price So please guide me which hook do I need to use?
This is the Code I have used in my functions.php However Please check it below
function span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator, $price_format ) {
global $post;
$id = $post->ID;
echo $price;
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format,
'<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>',
$price );
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
if ( $marketstatus == "On" ) {
$return = '<span class="woocommerce-Price-amount amount"> ON' . $formatted_price . '</span>';
} else {
$return = '<span class="woocommerce-Price-amount amount" style="display:none">' . $formatted_price . '</span>';
}
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
I need to overwrite the wc_price in my theme's function as I need to make suitable changes into it based on my requirements.
I have used
add_filter( 'formatted_woocommerce_price', 'span_custom_prc', 10, 5 );
However In this case I was not able to change the value of $formatted_price So please guide me which hook do I need to use?
This is the Code I have used in my functions.php However Please check it below
function span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator, $price_format ) {
global $post;
$id = $post->ID;
echo $price;
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format,
'<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>',
$price );
$marketstatus = get_post_meta( $post->ID,
'wcv_custom_product_marketstatus', true );
if ( $marketstatus == "On" ) {
$return = '<span class="woocommerce-Price-amount amount"> ON' . $formatted_price . '</span>';
} else {
$return = '<span class="woocommerce-Price-amount amount" style="display:none">' . $formatted_price . '</span>';
}
if ( $ex_tax_label && wc_tax_enabled() ) {
$return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
}
Share
Improve this question
edited Dec 1, 2017 at 8:23
CodeMascot
4,5372 gold badges15 silver badges25 bronze badges
asked Dec 1, 2017 at 6:20
Pratik bhattPratik bhatt
1,2882 gold badges13 silver badges27 bronze badges
4
- It would help, if you provided more information on how you wish to change the price output (I'm assuming you only wish to change the output, since you're using a filter). – Daniel Fonda Commented Dec 1, 2017 at 8:00
- Yes Actually I have added one extra on/off field in woocommerce products . If that is set to on the price will be displayed else not. For the code please check my question edit. – Pratik bhatt Commented Dec 1, 2017 at 8:09
-
You're not actually returning anything from your function. You need to
return $return;at the end. – Jacob Peattie Commented Dec 1, 2017 at 9:34 - Yes I know but I want to also change the value of $formatted price. I am not able to change it. – Pratik bhatt Commented Dec 1, 2017 at 10:13
1 Answer
Reset to default 1Hmm how about this?
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0]*2.5);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
Taken from here:https://sceptermarketing/how-to-change-the-woocommerce-price-via-functions-php/
本文标签: hooksHow can I override wpprice woocommerce function in my theme
版权声明:本文标题:hooks - How can I override wp_price woocommerce function in my theme 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749089716a2314327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


return $return;at the end. – Jacob Peattie Commented Dec 1, 2017 at 9:34