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
}
Share:

Leave a Reply