Code chuyển giá tiền thành chữ

code chuyển giá tiền thành chữ
code chuyển giá tiền thành chữ

Chèn vào funtions của theme đoạn code sau.

/* Chuyển đơn vị tiền tệ */
function format_price($price) {
    $suffixes = array('k', 'tr', 'tỉ');
    $suffix_index = 0;
    while (abs($price) >= 1000 && $suffix_index < count($suffixes)) {
        $price /= 1000;
        $suffix_index++;
    }
    if (floor($price) == $price) {
        $formatted_price = number_format($price, 0, ',', '.');
    } else {
        $formatted_price = number_format($price, 1, ',', '.');
    }
    if ($suffix_index > 0) {
        $formatted_price .= $suffixes[$suffix_index - 1];
    }
    return rtrim($formatted_price);
}
add_filter('woocommerce_get_price_html', 'custom_price_html', 100, 2);
/* Ghi đè vào giá tiền của woo */
function custom_price_html($price_html, $product) {
    $price = $product->get_price();
    $formatted_price = format_price($price);
    $price_html = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';
    return $price_html;
}