forcestop04
forcestop04

Reputation: 33

Add thousand separator to product quantity in WooCommerce

Our company orders portal deals in 1000’s of product sales.

I am trying to add a thousand separator to the quantity to make it easier to read (not as typing). It should appear like the thousand separator of price. I’ve been trying to add in some custom code to my child theme functions page:

// define the woocommerce_quantity callback
function filter_woocommerce_quantity( $quantity, $product ) {

// filter
return number_format($stock_quantity,0,”.”,”,”);
};

// add the filter
add_filter( ‘woocommerce_quantity’, ‘filter_woocommerce_quantity’, 10, 2 );

So far, not luck. Is there anything obvious that i’m doing wrong, or is this a more complex problem?

Upvotes: 3

Views: 775

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

To format a number with grouped thousands you could use

Parameters

  • number - The number being formatted.
  • decimals - Sets the number of decimal points.
  • dec_point - Sets the separator for the decimal point.
  • thousands_sep - Sets the thousands separator.

With the following code, the quantity display will be adjusted on the checkout page

function filter_woocommerce_checkout_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) {
    // Get quantity
    $cart_item_quantity = $cart_item['quantity'];
    
    // Format
    $cart_item_quantity = number_format($cart_item_quantity, 0, ',', ' ');
    
    // Output
    $item_qty = '<strong class="product-quantity">' . sprintf( '&times;&nbsp;%s', $cart_item_quantity ) . '</strong>';

    // Return
    return $item_qty;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );

Upvotes: 3

Related Questions