Hide WooCommerce Product Elements If User Logged In

You can hide add to cart buttons for logged out users, yes. For example, you can remove the featured image, the add to cart button (because maybe you only want them to purchase one product), the sale badge, the price, product tabs, and so on – while also adding logged-in only information such as custom buttons, banners, and media.

In this tutorial we’ll see how to target logged in customers who purchased the current product, how to remove some default layout elements and how to add some custom HTML and CSS to the single product page. Enjoy!

See also  Remove Width & Height Attributes in Wordpress
Hide WooCommerce Product Elements If User Logged In

PHP Snippet: Hide WooCommerce Product Elements If User Logged In

/**
 * @snippet       Logged-in Layout @ WooCommerce Single Product
 * @how-to        Get codeithub.com FREE
 * @author        Arun
 * @testedwith    WooCommerce 4.5
 * @donate    https://codeithub.com
 */
 
add_action( 'woocommerce_before_single_product', 'codeithub_single_product_layout_logged_in_purchased' );
 
function codeithub_single_product_layout_logged_in_purchased() { 
    global $product;
    if ( ! is_user_logged_in() ) return;  
    $current_user = wp_get_current_user();
    $theid = $product->get_id();
 
    // TARGET ONLY LOGGED IN CUSTOMERS WHO PURCHASED THIS PRODUCT    
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $theid ) ) {
 
        // REMOVE ADD TO CART
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
 
        // REMOVE PRICE
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
 
        // REMOVE FEAT. IMAGE
        remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
 
        // AND SO ON...
        // ADD CUSTOM CSS
        ?>
            <style>
                .selector {
                    property: value;
                    property: value;
                }
            </style>
        <?php
 
        // ADD CUSTOM HTML E.G. VIDEO
        add_action( 'woocommerce_after_single_product_summary', 'codeithub_add_video_single_prod_page', 1 );
 
    }
}
 
function codeithub_add_video_single_prod_page() {
    ?>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/3KGM3FfaXew" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
    <?php
}

Leave a Reply