After we installed WooCommerce, it is generating some default page which needed to run an e-commerce website like Account, Shop, Cart, Checkout.
These four are simple WordPress pages so we can query their URL directly by page ID, but this isn’t the most robust solution. We want to get a page URL which is set to a Woo functionality despite the page ID. This is where the wc_get_page_id() function comes into the picture.
Using wc_get_page_id() we can get a specific WooCommerce page’s ID by giving a string to the functions. In this way, we can query the previously mentioned pages using the get_permalink() function.
Get and Display the Shop URL
To fetch the shop URL, we need to use the “shop” keyword in the function and using the given ID in the get_permalink().
<?php echo get_permalink( wc_get_page_id( 'shop' ) ); ?>
Get and Display the My Account URL
Do everything like in the first example but with the “myaccount” keyword.
<?php echo get_permalink( wc_get_page_id( 'myaccount' ) ); ?>
Get and Display the Cart URL
Use the “cart” keyword.
<?php echo get_permalink( wc_get_page_id( 'cart' ) ); ?>
Get and Display the Checkout URL
Use the “checkout” keyword. Note that if your cart is empty you will be redirected to the cart page.
<?php echo get_permalink( wc_get_page_id( 'checkout' ) ); ?>
Get and Display the Logout URL
To get the URL we have to use the wp_logout_url() function. Optionally we can give a parameter if we want to redirect somewhere after the logout. To make the URL secure – if SSL is enabled – we replace the HTTP with HTTPS.
<?php $logout_url = wp_logout_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ); if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) { echo $logout_url = str_replace( 'http:', 'https:', $logout_url ); }; ?>