Home » WooCommerce: Edit Product Layout If Logged In

WooCommerce: Edit Product Layout If Logged In

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 4.5
 * @donate $9     https://tutoraspire.com
 */

add_action( 'woocommerce_before_single_product', 'tutoraspire_single_product_layout_logged_in_purchased' );

function tutoraspire_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
        ?>
            
        
        

You may also like