在WooCommerce中显示正常价格之前的销售价格

时间:2019-02-17 03:37:22

标签: php wordpress woocommerce product price

我是新成员,编程人员薄弱。我要在正常价格之前显示销售价格(如图片所示)。 我确定这里的钩子是woocommerce_before_variations_form。 这是在挂钩中编辑的代码。

// define the woocommerce_before_variations_form callback
function action_woocommerce_before_variations_form () {
     // make action magic happen here ...
};
         
// add the action
add_action ('woocommerce_before_variations_form', 'action_woocommerce_before_variations_form', 10, 0);

images

您能帮我在正常价格之前显示销售价格吗?

2 个答案:

答案 0 :(得分:4)

以下挂钩函数代码将在正常价格之前显示销售价格:

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

代码进入活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

答案 1 :(得分:1)

您可以仅使用jQuery并替换为显示正常价格和销售价格的元素来解决此问题:

$("#element1").before($("#element2"));

$("#element1").after($("#element2"));

:)

  

和js小提琴   https://jsfiddle.net/nak73406/v9k7b5c1/5/

相关问题