WooCommerce: overriding basket prices with text

We have a bunch of products with:

  • No price
  • Zero price

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

We would like the cart and checkout summary to display “Special Order” or any other text, but WooCommerce seems to invalidate text-based prices.

Tried this: WooCommerce: add product to cart with re-pricing?

The above works fine with overriding a number, but when you try to override a text, it defaults to displaying 0 price .

Why? These products are custom built and the store administrator will update the price after talking with the customer / supplier.

+5
source share
2 answers

You need to filter the rows showing the price and subtotal in the basket. The link you spoke about concerned a change in the actual price. In your case, the price is $ 0 until you set the actual price later. There are probably filters for the totals of the cart, but this should be the beginning:

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

Of course, this also applies to any product with a price of 0, and perhaps not only to those that you configured to create to order, so you may need more conditional logic than I am here.

To keep track of your comment .... woocommerce_order_amount_total is a numeric value, not a display html. You can see the functions called in 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' ); 

Updated screenshot: cart

+4
source

Helgi's answer expands, here is the complete code that changes the prices to “be determined” on the pages of the basket, checkout pages and order confirmation emails. The text remained different so that people could determine where each filter is.

  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; }; 
+1
source

All Articles