WooCommerce: Edit Product Layout If Logged In

Logged in customers often require different UX, communication and website layout. You can hide add to cart buttons for logged out users, yes, but you can also completely remodel the single product page layout. 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!

A possible scanario for logged in customers who purchased the current product: removing purchase information and image and replace it with a video.

PHP Snippet: Edit Single Product Page Layout for Logged In Customers

/**
 * @snippet       Logged-in Layout @ WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 4.5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_before_single_product', 'bbloomer_single_product_layout_logged_in_purchased' );

function bbloomer_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...
        // REFER TO https://www.businessbloomer.com/woocommerce-visual-hook-guide-single-product-page/        

        // ADD CUSTOM CSS
        ?>
            <style>
                .selector {
                    property: value;
                    property: value;
                }
            </style>
        <?php

        // ADD CUSTOM HTML E.G. VIDEO
        add_action( 'woocommerce_after_single_product_summary', 'bbloomer_add_video_single_prod_page', 1 );

    }
}

function bbloomer_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
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.