Thursday, October 22, 2020

Woocommerce: My Account Page Customisation

 /**

 * My ACCOUNT PAGE CUSTOMISATION

 */


/**

 * @snippet       Merge Two "My Account" Tabs @ WooCommerce Account

 * @how-to        Get CustomizeWoo.com FREE

 * @source        https://businessbloomer.com/?p=73601

 * @author        Rodolfo Melogli

 * @compatible    Woo 3.5.3

 * @donate $9     https://businessbloomer.com/bloomer-armada/

 */


// -------------------------------

// Remove Downloads Tab (downloads in this case)

 

add_filter( 'woocommerce_account_menu_items', 'remove_downloads_my_account', 999 );

  

function remove_downloads_my_account( $items ) {

unset($items['downloads']);

return $items;

}


// -------------------------------

// 1. First, hide the tab that needs to be merged/moved (edit-address in this case)

 

add_filter( 'woocommerce_account_menu_items', 'remove_address_my_account', 999 );

  

function remove_address_my_account( $items ) {

unset($items['edit-address']);

return $items;

}

 

// -------------------------------

// 1. First, hide the tab that needs to be merged/moved (payment-methods in this case)

 

add_filter( 'woocommerce_account_menu_items', 'remove_payment_my_account', 999 );

  

function remove_payment_my_account( $items ) {

unset($items['payment-methods']);

return $items;

}

// -------------------------------

// 2. Second, print the ex tab content into an existing tab (edit-account in this case)

 

add_action( 'woocommerce_account_edit-account_endpoint', 'woocommerce_account_edit_address' );

//add_action( 'woocommerce_account_edit-account_endpoint', 'woocommerce_account_payment_methods' );



// -------------------------------

// Redirect initial login straight away to orders page like Walmart

// 

add_action('template_redirect', 'redirect_to_orders_from_dashboard' );

 

function redirect_to_orders_from_dashboard(){

 

if( is_account_page() && empty( WC()->query->get_current_endpoint() ) ){

wp_safe_redirect( wc_get_account_endpoint_url( 'orders' ) );

exit;

}

 

}


// Display the product thumbnail in order view pages

add_filter( 'woocommerce_order_item_name', 'display_product_image_in_order_item', 20, 3 );

function display_product_image_in_order_item( $item_name, $item, $is_visible ) {

    // Targeting view order pages only

    if( is_wc_endpoint_url( 'view-order' ) ) {

        // Get the WC_Product object (from order item)

        $product   = $item->get_product(); 


        // Testing if the product exist in Woocommerce <== UPDATE

        if( $product && is_object( $product ) ) {

            // Get the product thumbnail (from product object)

            $thumbnail = $product->get_image(array( 36, 36)); 

            // Avoiding empty thumbnail (updated)

            if( $product->get_image_id() > 0 )

                $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>' . $item_name;

        } else {

            // When product doesn't exist, we get the name from the order item (with no thumbnail)

            $item_name = $item->get_name();

        }

    }

    return $item_name;

}

No comments: