hooks - How can I override wp_price woocommerce function in my theme

admin2025-06-05  2

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
Add a comment  | 

1 Answer 1

Reset to default 1

Hmm 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/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749089742a316274.html

最新回复(0)