WooCommerce:使用文本覆盖购物车价格
WooCommerce: Cart price override with text
我们有一堆产品:
- 无价格
- 零价
我们让它们可以通过内置挂钩购买,但购物车在结账时仍将它们显示为 0 price
。
我们希望购物车和结帐摘要显示 "Special order" 或任何其他文本,但 WooCommerce 似乎使基于文本的价格无效。
试过这个:
WooCommerce: Add product to cart with price override?
上面的数字覆盖效果很好,但是当尝试使用文本覆盖时,它默认返回显示 0 price
.
为什么?这些产品是按订单生产的,商店管理员将在与 customer/supplier.
交谈后更新价格
您需要过滤购物车中显示价格和小计的字符串。您提到的 link 讨论了更改实际价格。在您的情况下,价格 是 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' );
更新截图:
扩展 Helga 的回答,这里是将购物车页面、结帐页面和订单确认电子邮件中的价格更改为 'to be determined' 的完整代码。文本保持不同,以便人们可以发现每个过滤器的位置。
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;
};
我们有一堆产品:
- 无价格
- 零价
我们让它们可以通过内置挂钩购买,但购物车在结账时仍将它们显示为 0 price
。
我们希望购物车和结帐摘要显示 "Special order" 或任何其他文本,但 WooCommerce 似乎使基于文本的价格无效。
试过这个: WooCommerce: Add product to cart with price override?
上面的数字覆盖效果很好,但是当尝试使用文本覆盖时,它默认返回显示 0 price
.
为什么?这些产品是按订单生产的,商店管理员将在与 customer/supplier.
交谈后更新价格您需要过滤购物车中显示价格和小计的字符串。您提到的 link 讨论了更改实际价格。在您的情况下,价格 是 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' );
更新截图:
扩展 Helga 的回答,这里是将购物车页面、结帐页面和订单确认电子邮件中的价格更改为 'to be determined' 的完整代码。文本保持不同,以便人们可以发现每个过滤器的位置。
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;
};