目前,我正在使用css在单个产品页面上隐藏woocommerce的可变产品销售闪存,如下所示:
.product-type-variable .onsale {
display: none;
}问题:
我现在想有条件地显示正在出售的闪存信息。因此,当在实际正在销售的单一产品页面上选择一个变体时,销售徽章应该显示,否则就不应该显示。
这样做是可能的吗?
发布于 2022-04-10 08:33:53
找到了解决问题的方法。得到this question的帮助
/**
* Function to display variations sale prices only when a variation on sale is selected
*
*/
add_action( 'woocommerce_before_single_variation', 'action_wc_before_single_variation' );
function action_wc_before_single_variation() {
?>
<script type="text/javascript">
(function($){
// Check for the show_variation event that triggers when a variation is selected.
$('form.variations_form').on('show_variation', function(event, data){
//If variation is on sale, display the sale badge
if(data.display_price < data.display_regular_price){
$(".product-type-variable .onsale").css("display", "block");
}
//Otherwise hide the sale badge
else {
$(".product-type-variable .onsale").css("display", "none");
}
});
})(jQuery);
</script>
<?php
}代码进入活动主题的functions.php文件(使用子主题,以便在更新主题时不会覆盖代码)。
https://stackoverflow.com/questions/71808354
复制相似问题