我怀疑wc_price过滤器的可用性,当我查看price.html#489]代码时,我可以找到wc_price过滤器。
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 );
}
这意味着这个过滤器用于改变产品价格。
所以现在我想通过代码将前端价格提高一倍。因此,我编写了以下函数
add_filter( 'wc_price', 'double_price', 10, 3 );
function double_price( $return, $price, $args){
$price=$price*2;
return $price;
}
现在,价格在没有货币符号的情况下出现在正面。
然后我就这样重写了
function double_price( $return, $price, $args){
$price=$price*2;
return '<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">£</span>'.$price.'</span>';
}
现在起作用了。
但我不认为这是正确的方法。可以有人解释我如何正确地使用这个函数。。$args,$price,$retun
在这个过滤器中的用途是什么?
另外,如果我需要根据类别更改所有产品的价格,如何在此过滤器中获得产品标识?如果我写了产品id,我就会写。
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>';
}
请注意:这个问题有一个子问题price hook the checkout page price is incorrect
发布于 2017-12-13 06:27:21
钩子wc_price
是格式化价格钩子…。您应该使用woocommerce_product_get_price
过滤器钩子:
add_filter( 'woocommerce_product_get_price', 'double_price', 10, 2 );
function double_price( $price, $product ){
return $price*2;
}
测试和工作
代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。
这个钩子是在任何格式化价格函数之前触发的,它是一个基于
get_price()
方法的复合钩子,这里应用于WC_Product
对象类型。
https://stackoverflow.com/questions/47786331
复制相似问题