$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>hooks - How can I override wp_price woocommerce function in my theme|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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/

Post a comment

comment list (0)

  1. No comments so far