将显示的价格替换为缺货 WooCommerce 产品的文本 [英] Replace displayed price by a text for out of stock WooCommerce products

查看:31
本文介绍了将显示的价格替换为缺货 WooCommerce 产品的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是专家,但我需要更改失控"的价格行程我的 Woocommerce 产品页面中的产品.

I'm not an expert, but I need to change price stroke for "out of stoke" products in my Woocommerce product page.

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

Use instead woocommerce_get_price_html hook as follows (that will replace the formatted displayed price and currency):

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 文件中.它应该更好地工作.

Code goes in functions.php file of the active child theme (or active theme). it should better work.

与您的评论相关的补充:

对于您的第二个代码片段,请尝试以下操作:

For your 2nd code snippet, try the following instead:

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

它应该可以工作.如果您想让它在产品缺货时适用于所有货币,请删除 &&$currency === 'USD' 来自 IF 语句

It should works. If you want to make it work for all currencies when product is out of stock remove && $currency === 'USD' from the IF statement

这篇关于将显示的价格替换为缺货 WooCommerce 产品的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆