i have doubt regarding usability of wc_price filter , When i look the code [https://docs.woocommerce.com/wc-apidocs/source-function-wc_price.html#489] i can find wc_price filter .
function wc_price( $price, $args = array() ) {
extract( apply_filters( 'wc_price_args', wp_parse_args( $args, array(
'ex_tax_label' => false,
'currency' => '',
'decimal_separator' => wc_get_price_decimal_separator(),
'thousand_separator' => wc_get_price_thousand_separator(),
'decimals' => wc_get_price_decimals(),
'price_format' => get_woocommerce_price_format(),
) ) ) );
$negative = $price < 0;
$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
$price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );
if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {
$price = wc_trim_zeros( $price );
}
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>', $price );
$return = '<span class="woocommerce-Price-amount amount">' . $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>';
}
return apply_filters( 'wc_price', $return, $price, $args );
}
This means this filter is used for changing the product price .
so now i want to double the frontend price by code . So iwrite the following function
add_filter( 'wc_price', 'double_price', 10, 3 );
function double_price( $return, $price, $args){
$price=$price*2;
return $price;
}
Now the price is showing in frontend without currency symbol .
then i rewrite it like this
function double_price( $return, $price, $args){
$price=$price*2;
return '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>'.$price.'</span>';
}
Now it's working .
But i don't think this is the correct way . Can someone explain how can i use this function properly .. What is the use of $args,$price,$retun
in this filter ?
Also if i need to to change all product price based on category , how can i get product id inside this filter ? if i write the product id then i will write
function double_price( $return, $price, $args){
$price=$price*2;
if( has_term( 'shirts', 'product_cat' ,$product->ID) ) {
$price=130;
}
return '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>'.$price.'</span>';
}
Please note: this question have a sub question After using woocommerce_product_get_price hook the checkout page price is incorrect
The hook wc_price
is a formatting price hook… You should use instead woocommerce_product_get_price
filter hook:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
Tested and working
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This hook is fired before any formatting price function and it's a composite hook based on
get_price()
method here applied toWC_Product
object type.