在WooCommerce中将千位分隔符添加到产品数量中

问题描述

我们的公司订购了1000笔产品销售的门户交易。

我正在尝试在数量添加千位分隔符,以使其更易于阅读(而不是键入)。它应该看起来像价格的千位分隔符。 我一直试图在我的子主题功能页面添加一些自定义代码

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

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

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

到目前为止,还不是运气。是否有明显的地方我做错了,还是这是一个更复杂的问题?

解决方法

要对成千上万的数字进行格式化,可以使用

参数

  • 数字-要格式化的数字。
  • 小数-设置小数点的数量。
  • dec_point-设置小数点的分隔符。
  • thousands_sep-设置千位分隔符。

使用以下代码,将在结帐页面上调整数量显示

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,',' ');
    
    // 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 );