仅在不销售产品时将CSS添加到WooCommerce产品页面

问题描述 投票:1回答:1

如果产品不销售,我需要添加一些CSS来隐藏页面上的某些元素/类。

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

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

php wordpress woocommerce hook-woocommerce price
1个回答
0
投票
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>";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.