WooCommerce:用文字覆盖购物车价格 [英] WooCommerce: Cart price override with text

查看:72
本文介绍了WooCommerce:用文字覆盖购物车价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一堆产品,其中之一:

We have a bunch of products with either:


  • 无价格

  • 零价格

我们通过内置挂钩使它们可购买,但购物车仍将其显示为具有 0价格

We made them purchasable with the built-in hook but the cart still displays them as having a 0 price during checkout.

我们想要购物车&结帐摘要以显示特殊订单或任何其他文本,但WooCommerce似乎使基于文本的价格无效。

We'd like the cart & checkout summary to display "Special order" or any other text, but it seems WooCommerce invalidates text-based prices.

尝试了以下操作:
WooCommerce:将产品添加到具有价格替换功能的购物车中吗?

上面的方法可以很好地覆盖数字,但是尝试覆盖文本时,默认返回显示 0价格

The above works fine with a number override, but when tried with a text override it defaults back to displaying a 0 price.

为什么?这些产品是按订单生产的,在与客户/供应商交谈后,商店管理员将更新价格。

Why? These products are built-to-order and the shop admin will update price after talking to customer/supplier.

推荐答案

在Helga的产品上进行扩展答案,这是完整的代码,可将购物车页面,结帐页面和订单确认电子邮件上的价格更改为待定。文本保持不同,以便人们可以发现每个过滤器的位置。

expanding on Helga's answer, here's the full code that changes the prices to 'to be determined' on cart pages, checkout pages and order confirmation emails too. Text kept different so people can spot where each filter goes.

    add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price, $cart_item, $cart_item_key ) {
    if ( $cart_item[ 'data' ]->price == 0 ) {
        $price = __( 'Special Order', 'yourtheme' );
    }
    return $price;
}

add_filter( 'woocommerce_cart_item_subtotal', 'filter_cart_item_subtotal', 10, 3 );
function filter_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
    if ( $cart_item[ 'data' ]->price == 0 ) {
        $subtotal = __( 'To be determined', 'yourtheme' );
    }
    return $subtotal;
}

add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 ); 
function filter_woocommerce_cart_subtotal( $cart_subtotal, $compound, $instance ) { 
    $cart_subtotal = __( 'To be determined 4', 'yourtheme' ); 
    return $cart_subtotal; 
}; 


add_filter( 'woocommerce_order_formatted_line_subtotal', 'filter_order_item_subtotal', 10, 3 );
function filter_order_item_subtotal( $subtotal, $item, $order ) {
    if ( isset( $item[ 'line_subtotal' ] ) && $item[ 'line_subtotal' ] == 0 ) {
        $subtotal = __( 'To be determined 2', 'yourtheme' );
    }
    return $subtotal;
}

add_filter( 'woocommerce_order_subtotal_to_display', 'filter_woocommerce_order_subtotal_to_display', 10, 3 ); 
function filter_woocommerce_order_subtotal_to_display( $subtotal, $compound, $instance ) { 
        $subtotal = __( 'To be determined 6', 'yourtheme' );
    return $subtotal; 
};

add_filter( 'woocommerce_get_formatted_order_total', 'filter_woocommerce_get_formatted_order_total', 10, 2 ); 
function filter_woocommerce_get_formatted_order_total( $formatted_total, $instance ) { 
        $formatted_total = __( 'To be determined 8', 'yourtheme' );
    return $formatted_total; 
}; 

这篇关于WooCommerce:用文字覆盖购物车价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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