首页 文章

WooCommerce:购物车价格覆盖文字

提问于
浏览
5

我们有一堆产品:

  • 没有价格

  • 零价格

我们使用内置挂钩购买它们但是购物车在结账时仍然显示为 0 price .

我们希望购物车和结帐摘要显示“特殊订单”或任何其他文字,但似乎WooCommerce使基于文本的价格无效 .

试过这个:WooCommerce: Add product to cart with price override?

上面的数字覆盖工作正常,但尝试使用文本覆盖时,它默认返回显示 0 price .

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

2 回答

  • 4

    您需要过滤显示购物车中的价格和小计的字符串 . 您提到的链接讨论了更改实际价格 . 在您的情况下,价格是0美元,直到您稍后设置实际价格 . 也可能是购物车总数的过滤器,但这应该是一个开始:

    add_filter( 'woocommerce_cart_item_price', 'so_38057349_cart_item_price', 10, 3 );
    function so_38057349_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', 'so_38057349_cart_item_subtotal', 10, 3 );
    function so_38057349_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_order_formatted_line_subtotal', 'so_38057349_order_item_subtotal', 10, 3 );
    function so_38057349_order_item_subtotal( $subtotal, $item, $order ) {
    
        if ( isset( $item[ 'line_subtotal' ] ) && $item[ 'line_subtotal' ] == 0 ) {
            $subtotal = __( 'To be determined', 'yourtheme' );
        }
    
        return $subtotal;
    }
    

    当然,这也适用于价格为0的任何产品,也可能不仅适用于您自定义构建的产品,因此您可能需要比我在此处提供的条件逻辑更多的条件逻辑 .

    要跟进你的评论...... woocommerce_order_amount_total 是数字总数而不是显示的html . 您可以在cart-totals.php模板中看到正在调用的函数 .

    function so_38057349_woocommerce_cart_subtotal( $cart_subtotal, $compound, $cart ) { 
        if( $cart->subtotal == 0 ){
            $cart_subtotal = __( 'Order subtotal to be determined', 'yourtheme' ); 
        }
        return $cart_subtotal; 
    }; 
    add_filter( 'woocommerce_cart_subtotal', 'so_38057349_woocommerce_cart_subtotal', 10, 3 ); 
    
    
    // define the woocommerce_order_amount_total callback 
    function so_38057349_woocommerce_order_amount_total( $order_total ) { 
        if( WC()->cart->get_total() == 0 ){
            $order_total = __( 'Order total to be determined', 'yourtheme' ); 
        }
        return $order_total; 
    }; 
    add_filter( 'woocommerce_cart_totals_order_total_html', 'so_38057349_woocommerce_order_amount_total' );
    

    更新截图:
    cart

  • 1

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

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

相关问题