从WooCommerce价格中删除所有跨度标签和类别

时间:2019-12-27 作者:chouaib achache

我想删除所有span标记并自动调整价格

<span class="price">
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">$</span>690.00</span></span>

1 个回复
SO网友:Adam

有几种方法可以做到这一点,这里有两种:

方法#1

在主题(如果使用子主题扩展父主题,则为父主题或子主题)中创建文件夹结构,如下所示:

{your_theme_folder}
 ∟ woocommerce      (directory)
   ∟ single-product (directory)
     ∟ price.php    (copy from wp-content/plugins/woocommerce/templates/single-product/price.php)
示例:{your_theme_folder}/woocommerce/single-product/price.php

此文件:price.php 包含以下内容:

<?php

global $product;

?>

<p class="<?php echo esc_attr( apply_filters( \'woocommerce_product_price_class\', \'price\' ) );?>">
    <?php echo $product->get_price_html(); ?>
</p>

并将模板修改为以下内容:

<?php

global $product;

?>

<p class="<?php echo esc_attr( apply_filters( \'woocommerce_product_price_class\', \'price\' ) );?>">
    <?php echo wc_get_price_to_display( $product ) . $product->get_price_suffix(); ?>
</p>

方法#2

$product->get_price_html() 使用wc_price 函数设置价格格式(负责所有这些跨度),您也可以通过apply_filters( \'wc_price\', $return, $price, $args, $unformatted_price );

示例:

WARNING 这将影响各地的价格(任何使用wc_price() 函数)因此,请确保在必要时将逻辑包装在条件子句中!


function custom_wc_price( $return, $price, $args, $unformatted_price ) {

    // modify the price using: $return, $price, $args, $unformatted_price

    // crude example
    $return = $unformatted_price;


    return $return;

}

add_filter( \'wc_price\', \'custom_wc_price\', 10, 4 );
请注意,其他过滤器和价格转换发生在wc_price 因此,我建议您在WooCommerce插件目录中查看该函数的内部内容,因为您可能希望重新运行价格raw_woocommerce_priceformatted_woocommerce_price 过滤或简单地跨负责应用小数分隔符、千分隔符、货币等的逻辑进行复制。

参见wc_price 在里面wp-content/plugins/woocommerce/includes/wc-formatting-functions.php:

/**
 * Format the price with a currency symbol.
 *
 * @param  float $price Raw price.
 * @param  array $args  Arguments to format a price {
 *     Array of arguments.
 *     Defaults to empty array.
 *
 *     @type bool   $ex_tax_label       Adds exclude tax label.
 *                                      Defaults to false.
 *     @type string $currency           Currency code.
 *                                      Defaults to empty string (Use the result from get_woocommerce_currency()).
 *     @type string $decimal_separator  Decimal separator.
 *                                      Defaults the result of wc_get_price_decimal_separator().
 *     @type string $thousand_separator Thousand separator.
 *                                      Defaults the result of wc_get_price_thousand_separator().
 *     @type string $decimals           Number of decimals.
 *                                      Defaults the result of wc_get_price_decimals().
 *     @type string $price_format       Price format depending on the currency position.
 *                                      Defaults the result of get_woocommerce_price_format().
 * }
 * @return string
 */
function wc_price( $price, $args = array() ) {
    $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(),
            )
        )
    );

    $unformatted_price = $price;
    $negative          = $price < 0;
    $price             = apply_filters( \'raw_woocommerce_price\', floatval( $negative ? $price * -1 : $price ) );
    $price             = apply_filters( \'formatted_woocommerce_price\', number_format( $price, $args[\'decimals\'], $args[\'decimal_separator\'], $args[\'thousand_separator\'] ), $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\'], \'<span class="woocommerce-Price-currencySymbol">\' . get_woocommerce_currency_symbol( $args[\'currency\'] ) . \'</span>\', $price );
    $return          = \'<span class="woocommerce-Price-amount amount">\' . $formatted_price . \'</span>\';

    if ( $args[\'ex_tax_label\'] && wc_tax_enabled() ) {
        $return .= \' <small class="woocommerce-Price-taxLabel tax_label">\' . WC()->countries->ex_tax_or_vat() . \'</small>\';
    }

    /**
     * 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.
     */
    return apply_filters( \'wc_price\', $return, $price, $args, $unformatted_price );
}
(注:此答案并非详尽无遗)

相关推荐