php - WooCommerce:使用文本覆盖购物车价格

标签 php wordpress woocommerce cart

我们有一堆产品:

  • 没有价格
  • 零价格

我们使用内置 Hook 让它们可以购买,但购物车在结账时仍将它们显示为 0 价格

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

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

上面的数字覆盖效果很好,但当尝试使用文本覆盖时,它默认返回显示 0 价格

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

最佳答案

您需要过滤购物车中显示价格和小计的字符串。您提到的链接讨论了更改实际价格。在您的情况下,价格 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

关于php - WooCommerce:使用文本覆盖购物车价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38057349/

相关文章:

php - 获取存储过程 Laravel 的结果

php - 即使变量分配有对象数组,v-for 也不起作用

php - 覆盖 Controller Symfony 3.4/4.0

javascript - 通过带有索引的 jquery ajax 发送 php 数组并接收数组值

php - 用于显示 : "Buy X more products to get a discount" 的 ajaxify 消息的 WooCommerce 短代码

wordpress - 抱歉,没有符合您选择的产品。请选择不同的组合 WooCommerce

css - 折叠后按钮消失?

css - 在 genesis 中的 style.css 之前添加 CDN CSS

wordpress - Woocommerce 产品发布、更新和删除 Hook

php - WooCommerce 预订 : adding and reordering item data in cart and checkout