【问题标题】:Excessive DOM elements Currency Symbol过多的 DOM 元素货币符号
【发布时间】:2023-01-23 15:51:25
【问题描述】:

当我运行 pagespeed insight 时,<span class="woocommerce-Price-currencySymbol">导致过多的 Dom 元素。我尝试了以下方法但无济于事。许多人遇到过这些问题,但没有人有解决方案。


add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);

function change_existing_currency_symbol( $currency_symbol, $currency ) {
     switch( $currency ) {
          case 'AUD': $currency_symbol = 'AUD'; break;
     }
     return $currency_symbol;
}


include_once __DIR__ . '/theme-includes/theme-functions.php';

和这个

add_filter( 'formatted_woocommerce_price', 'span_custom_prc', 10, 5 );

function span_custom_prc( $number_format, $price, $decimals, $decimal_separator, $thousand_separator)
{
    return "<span class='woocommerce-Price-amount amount'>".$number_format."&nbsp;</span>";
}

【问题讨论】:

    标签: php wordpress dom woocommerce pagespeed


    【解决方案1】:

    在您的活动主题 functions.php 中使用以下代码 sn-p。

    使用 WooCommerce 6 测试正常

    add_filter('wc_price', 'custom_wc_price', 10, 5);
    
    /**
     * Filters the string of price markup.
     *
     * @param string       $return            Price HTML markup.
     * @param string       $price             Formatted price.
     * @param array        $args              Pass on the args.
     * @param float        $unformatted_price Price as float to allow plugins custom formatting. Since 3.2.0.
     * @param float|string $original_price    Original price as float, or empty string. Since 5.0.0.
     */
    function custom_wc_price($return, $price, $args, $unformatted_price, $original_price) {
        $args = apply_filters(
                'wc_price_args',
                wp_parse_args(
                        $args,
                        array(
                            'ex_tax_label' => false,
                            'currency' => '',
                            'decimal_separator' => wc_get_price_decimal_separator(),
                            'thousand_separator' => wc_get_price_thousand_separator(),
                            'decimals' => wc_get_price_decimals(),
                            'price_format' => get_woocommerce_price_format(),
                        )
                )
        );
    
        $original_price = $price;
    
        // Convert to float to avoid issues on PHP 8.
        $price = (float) $price;
    
        $unformatted_price = $price;
        $negative = $price < 0;
    
        /**
         * Filter raw price.
         *
         * @param float        $raw_price      Raw price.
         * @param float|string $original_price Original price as float, or empty string. Since 5.0.0.
         */
        $price = $negative ? $price * -1 : $price;
    
        /**
         * Filter formatted price.
         *
         * @param float        $formatted_price    Formatted price.
         * @param float        $price              Unformatted price.
         * @param int          $decimals           Number of decimals.
         * @param string       $decimal_separator  Decimal separator.
         * @param string       $thousand_separator Thousand separator.
         * @param float|string $original_price     Original price as float, or empty string. Since 5.0.0.
         */
        $price = number_format($price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator']);
    
        if (apply_filters('woocommerce_price_trim_zeros', false) && $args['decimals'] > 0) {
            $price = wc_trim_zeros($price);
        }
    
        $formatted_price = ( $negative ? '-' : '' ) . sprintf($args['price_format'],  get_woocommerce_currency_symbol($args['currency']), $price);
        $return = '<span class="woocommerce-Price-amount amount"><bdi>' . $formatted_price . '</bdi></span>';
    
        if ($args['ex_tax_label'] && wc_tax_enabled()) {
            $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
        }
    
    
        return $return;
    }
    

    【讨论】:

    • 我刚刚意识到这个 sn-p 解决了过多的 dom 元素问题,但它改变了产品价格。
    【解决方案2】:

    我已经应用了它不起作用的代码。任何人都可以帮助解决这个 dom 大小过大的问题。showing avoid excessive dom size

    【讨论】:

    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2013-10-26
    相关资源
    最近更新 更多