Displaying price only when the variation is selected and discount in percentage relatively to the regular price

一个人想着一个人 提交于 2019-12-08 04:52:36

问题


I've this problem. Let me show pictures so I can explain better.

Variation product selected but because all the variations have the same price the price do not show in the bottom:

Variation product selected, because they have different PROMO prices they show on top and the regular-promo price after selection:

What I need is that only after selected the variations the price is show in the bottom like the second image and calculation the discount between the promo variation price and the regular variation price. I need the same behavior in the two cases.

I've searched a lot, but none of the things have solved this behavior. Here some answer that are very close:

  • calculating discount percentage
  • variation prices customizations

回答1:


After some search, there this simple dedicated filter hook woocommerce_show_variation_price that will make exactly what you are expecting:

add_filter( 'woocommerce_show_variation_price', 'filter_show_variation_price', 10, 3 );
function filter_show_variation_price( $condition, $product, $variation ){
    if( $variation->get_price() === "" ) return false;
    else return true;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and work… This will display the selected variation price even if all variations prices are the same…



来源:https://stackoverflow.com/questions/47328965/displaying-price-only-when-the-variation-is-selected-and-discount-in-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!