rad*_*dug 6 php wordpress cart wordpress-plugin woocommerce
我们有一堆产品:
我们使用内置挂钩可以购买它们,但购物车仍然0 price在结账时显示它们.
我们希望购物车和结帐摘要显示"特殊订单"或任何其他文字,但似乎WooCommerce使基于文本的价格无效.
试过这个: WooCommerce:用价格覆盖将产品添加到购物车?
以上工作正常,数字覆盖,但尝试使用文本覆盖时,它默认返回显示0 price.
为什么?这些产品是按订单生产的,商店管理员将在与客户/供应商交谈后更新价格.
您需要过滤购物车中显示价格和小计的字符串。您提到的链接讨论了更改实际价格。在您的情况下,价格为 $ 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;
}
Run Code Online (Sandbox Code Playgroud)
当然,这也将适用于任何价格为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' );
Run Code Online (Sandbox Code Playgroud)
更新的屏幕截图:

扩展 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;
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5984 次 |
| 最近记录: |