仅当产品未打折时才将 CSS 添加到 WooCommerce 产品页面

Add CSS to WooCommerce product pages only if product is not on sale

如果产品不打折,我需要添加一些 CSS 来隐藏页面上的一些 elements/classes。

从我的另一个功能来看,我假设我可以检查 $args['meta_key'] = '_sale_price'; 是否为空白,如果它然后添加 CSS。

如何在我的 functions.php 中使用它?

add_action( 'wp', 'add_cssif_product_not_onsale' );

function add_cssif_product_not_onsale() {

    if ( is_product() ) {
        global $post;
        $product = wc_get_product( $post );
        if ( !$product->is_on_sale() )
            echo "<style>body{display:none}</style>";
    }
}