在WooCommerce中为产品数量添加千位分隔符。

3

我们公司的订单门户涉及成千上万的产品销售。

我正在尝试向数量中添加千位分隔符,以使其更易于阅读(而不是作为输入)。它应该像价格的千位分隔符一样显示。 我一直在尝试在我的子主题功能页面中添加一些自定义代码:

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

到目前为止,还没有运气。我是否做错了什么明显的事情,还是这是一个更加复杂的问题?
1个回答

3

要对数字进行千位分组格式化,您可以使用:

参数

  • 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( '&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 );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接