Php 将显示的价格替换为缺货商品的文本
我不是专家,但我需要在我的Woocommerce产品页面中更改“场外”产品的价格Php 将显示的价格替换为缺货商品的文本,php,wordpress,woocommerce,product,hook-woocommerce,Php,Wordpress,Woocommerce,Product,Hook Woocommerce,我不是专家,但我需要在我的Woocommerce产品页面中更改“场外”产品的价格 我发现了如何将价格改为“卖出”(这就是工作) 我试图隐藏我的货币符号(将$sell更改为sell) 但它不起作用 错误列表 Your PHP code changes were rolled back due to an error on line 66 of file wp-content/themes/pro-child/functions.php. Please fix and try saving agai
Your PHP code changes were rolled back due to an error on line 66 of file wp-content/themes/pro-child/functions.php. Please fix and try saving again.
Uncaught Error: Call to a member function is_in_stock() on null in wp-content/themes/pro-child/functions.php:66
Stack trace:
#0 wp-includes/class-wp-hook.php(287): change_existing_currency_symbol('$', 'USD')
#1 wp-includes/plugin.php(212): WP_Hook->apply_filters('$', Array)
#2 wp-content/plugins/woocommerce/includes/wc-core-functions.php(854): apply_filters('woocommerce_cur...', '$', 'USD')
#3 wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-price-filter.php(45): get_woocommerce_currency_symbol()
#4 wp-includes/class-wp-widget-factory.php(61): WC_Widget_Price_Filter->__construct()
#5 wp-includes/widgets.php(115): WP_Widget_Factory->register('WC_Widget_Price...')
#6 wp-content/plugins/woocommerce/includes/wc-
你能帮我吗?有什么问题吗?使用
woocommerce\u get\u price\u html
hook,如下所示(这将替换格式化显示的价格和货币):
代码进入活动子主题(或活动主题)的functions.php文件。它应该更有效
与您的评论相关的添加: 对于第二个代码段,请尝试以下操作:
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
$product = wc_get_product( get_the_ID() );
if ( is_a( $product, 'WC_Product' ) && $currency === 'USD' && ! $product->is_in_stock() ) {
$currency_symbol = '';
}
return $currency_symbol;
}
它应该有效。如果您想在产品缺货时使其适用于所有货币,请从
If
语句中删除&&&$currency==='USD'
,谢谢-它在标准主题中工作得非常好(只需测试它),但不幸的是,在我的情况下不是这样。我将“x主题”与两个独立的内部参数{dc:woocommerce:currency_symbol}和{{dc:woocommerce:product_price}一起使用,第一个代码已更改(product price),因此我确实需要第二个代码来仅更改已售出产品的currency_sumbol。也许你还有一个想法?@EvgeniyKrasnoshlykov我已经添加了一个新的东西,让你的第二个代码段工作起来……试试看。如果这个答案回答了你的问题,你可以请你回答,谢谢。非常感谢!!!你是我个人的英雄。效果很好
Your PHP code changes were rolled back due to an error on line 66 of file wp-content/themes/pro-child/functions.php. Please fix and try saving again.
Uncaught Error: Call to a member function is_in_stock() on null in wp-content/themes/pro-child/functions.php:66
Stack trace:
#0 wp-includes/class-wp-hook.php(287): change_existing_currency_symbol('$', 'USD')
#1 wp-includes/plugin.php(212): WP_Hook->apply_filters('$', Array)
#2 wp-content/plugins/woocommerce/includes/wc-core-functions.php(854): apply_filters('woocommerce_cur...', '$', 'USD')
#3 wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-price-filter.php(45): get_woocommerce_currency_symbol()
#4 wp-includes/class-wp-widget-factory.php(61): WC_Widget_Price_Filter->__construct()
#5 wp-includes/widgets.php(115): WP_Widget_Factory->register('WC_Widget_Price...')
#6 wp-content/plugins/woocommerce/includes/wc-
add_filter('woocommerce_get_price_html', 'change_sold_out_product_price_html', 100, 2 );
function change_sold_out_product_price_html( $price_html, $product ) {
if ( ! $product->is_in_stock() ) {
$price_html = __("SOLD", "woocommerce");
}
return $price_html;
}
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
$product = wc_get_product( get_the_ID() );
if ( is_a( $product, 'WC_Product' ) && $currency === 'USD' && ! $product->is_in_stock() ) {
$currency_symbol = '';
}
return $currency_symbol;
}