gpt4 book ai didi

php - 用缺货的 WooCommerce 产品的文本替换显示的价格

转载 作者:行者123 更新时间:2023-12-04 15:06:53 25 4
gpt4 key购买 nike

我不是专家,但我需要更改我的 Woocommerce 产品页面中“out of stoke”产品的价格。

  1. 我发现了如何将价格更改为“已售出”(并且有效)
//Change price to 'sold'

add_filter('woocommerce_product_get_price','change_price_regular_member', woocommerce_currency_symbols, 10, 2 );

function change_price_regular_member( $price, $product)
{
if (!$product->is_in_stock())
$price = "SOLD";
return $price;
}
  1. 我尝试隐藏我的货币符号(将 $SOLD 更改为 SOLD)
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product;

if (!$product->is_in_stock() ) {
switch( $currency ) {
case 'USD': $currency_symbol = '';
break;
}}

return $currency_symbol;
}

它不起作用

错误列表

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_get_price_html 钩子(Hook),如下所示(将替换格式化显示的价格和货币):

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;
}

代码进入事件子主题(或事件主题)的 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'

关于php - 用缺货的 WooCommerce 产品的文本替换显示的价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65963520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com