如何在WooCommerce中颠倒/调换“售价”和“正常价格”的顺序?

时间:2018-12-21 作者:Benmay

默认情况下,wooCommerce首先显示“正常价格”,其次显示“销售价格”。我希望颠倒所有类型(简单、变体等)的顺序。这可能吗?怎样

Thx公司

1 个回复
SO网友:mrben522

您将要使用hooks and filters 要改变这一点。我会看看woocommerce_get_price_html 和/或woocommerce_variable_price_html 过滤器。

编辑:

以下是您需要更改的核心WC代码:

/**
 * Format a sale price for display.
 *
 * @since  3.0.0
 * @param  string $regular_price Regular price.
 * @param  string $sale_price    Sale price.
 * @return string
 */
function wc_format_sale_price( $regular_price, $sale_price ) {
    $price = \'<del>\' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . \'</del> <ins>\' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . \'</ins>\';
    return apply_filters( \'woocommerce_format_sale_price\', $price, $regular_price, $sale_price );
}
如您所见,返回值上有一个过滤器。您想要做的是挂接到该过滤器中,并返回您想要的价格。类似这样:

add_filter(\'woocommerce_format_sale_price\', \'wc_override_sale_price_format\', 10, 2);
function wc_override_sale_price_format( $regular_price, $sale_price ) {
    $price = \'<ins>\' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . \'</ins> <del>\' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . \'</del>\';
    return $price;
}

相关推荐