Woocommerce - 设置价格小数点样式并隐藏 .00

问题描述 投票:0回答:1

我一直在尝试实现以下目标,对 woocommerce 价格小数进行样式化(将它们包装在一个类中),以便通过 CSS 编辑使它们成为上标,同时隐藏/删除它们,以防它们是 .00

网上有多种解决方案,不幸的是似乎没有一个有效, 这是我最接近的成功:

add_filter( 'formatted_woocommerce_price', 'dcwd_superscript_wc_formatted_price', 10, 5 );
function dcwd_superscript_wc_formatted_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    // Leave prices unchanged in Dashboard.
    if ( is_admin() ) {
        return $formatted_price;
    }

// Format units, including thousands separator if necessary.
$unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
// Format decimals, with leading zeros as necessary (e.g. for 2 decimals, 0 becomes 00, 3 becomes 03 etc).
$decimal = '';
$num_decimals = wc_get_price_decimals();
if ( $num_decimals ) {
    $decimal = sprintf( '%0'.$num_decimals.'d', round( ( $price - intval( $price ) ) * 100 ) );
}

return $unit . '<span class="decimalsev">' . $decimal_separator. $decimal . '</span>';
}


// Remove decimals if .00
add_filter( 'formatted_woocommerce_price', 'dcwd_remove_zero_decimals', 10, 5 );
function dcwd_remove_zero_decimals( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
/*
    // Leave decimals in on single product page.
    if ( is_product() ) {
        return $formatted_price;
    }
*/ 

    if ( $price - intval( $price ) == 0 ) {
        // Format units, including thousands separator if necessary.
        return $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
    }
    else {
        return $formatted_price;
    }
}

问题在于,当价格设置为含税且设置了税费(在我的例子中为 23%)时,应显示为 4 欧元的价格可能会显示为 3.100 欧元(3 欧元加 100 美分),或 10 4 欧元单位将显示为 39.100 欧元(39 欧元和 100 美分)

还有其他人建议更好或不同的方法来实现这一目标吗? 预先感谢您的关注和建议。

php wordpress woocommerce
1个回答
0
投票

我建议你尝试一下给定的方法,它应该会给你想要的结果。

请将给定的代码添加到您主题的

functions.php
文件中。

// Here we are modifying formatted price to include a span for decimals.
add_filter( 'formatted_woocommerce_price', 'superscript_wc_formatted_price', 10, 5 );
function superscript_wc_formatted_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    // Here we are leaving prices unchanged in case of Dashboard.
    if ( is_admin() ) {
        return $formatted_price;
    }

    // Here we have Format units, including thousands separator if necessary.
    $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
    
    // Here we are checking if there are decimals to display.
    $decimal = '';
    $num_decimals = wc_get_price_decimals();
    if ( $num_decimals ) {
        $decimal = sprintf( '%0'.$num_decimals.'d', round( ( $price - intval( $price ) ) * 100 ) );
    }

    // Here we returning formatted price with a span for decimals.
    return '<span class="price-unit">' . $unit . '</span>' . '<span class="decimalsev">' . $decimal_separator. $decimal . '</span>';
}

另外,请将给定的代码添加到您的

syle.css
theme
文件中。

.decimalsev {
    font-size: smaller; /* You can adjust the font size as per your need */
    vertical-align: super;
}
.price-unit + .decimalsev:empty {
    display: none; /* Here we have Hide decimals when they are .00 */
}

另外,请将给定的代码添加到主题或自定义脚本文件的

JS file
以删除
.00

jQuery(document).ready(function($) {
    $( '.decimalsev' ).each(function() {
        if ($(this).text() === '.') {
            $(this).parent().find( '.price-unit' ).next( '.decimalsev' ).remove();
        }
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.