在 WooCommerce 中为产品数量添加千位分隔符
Add thousand separator to product quantity in WooCommerce
我们公司订购了 1000 件产品的门户网站交易。
我正在尝试为数量添加千位分隔符以使其更易于阅读(而不是打字)。它应该看起来像价格的千位分隔符。
我一直在尝试向我的子主题功能页面添加一些自定义代码:
// 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 );
到目前为止,还不是运气。有什么明显的地方我做错了吗,或者这是一个更复杂的问题?
要格式化以千为单位的数字,您可以使用
- number_format — 用千位分组格式化数字
参数
- number - 正在格式化的数字。
- decimals - 设置小数点数。
- 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, 0, ',', ' ');
// Output
$item_qty = '<strong class="product-quantity">' . sprintf( '× %s', $cart_item_quantity ) . '</strong>';
// Return
return $item_qty;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );
我们公司订购了 1000 件产品的门户网站交易。
我正在尝试为数量添加千位分隔符以使其更易于阅读(而不是打字)。它应该看起来像价格的千位分隔符。 我一直在尝试向我的子主题功能页面添加一些自定义代码:
// 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 );
到目前为止,还不是运气。有什么明显的地方我做错了吗,或者这是一个更复杂的问题?
要格式化以千为单位的数字,您可以使用
- number_format — 用千位分组格式化数字
参数
- number - 正在格式化的数字。
- decimals - 设置小数点数。
- 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, 0, ',', ' ');
// Output
$item_qty = '<strong class="product-quantity">' . sprintf( '× %s', $cart_item_quantity ) . '</strong>';
// Return
return $item_qty;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );