[WooCommerce] รวมฟังก์ชั่น get URL หน้าต่าง ๆ

เวลาเขียน Template WooCommerce บางทีเราอยากได้ URL ของหน้าบางหน้ามาใช้ แต่ในไฟล์ Template WooCommerce มันไม่มี มีแต่ hook ชื่อแปลก ๆ ที่เราเข้าไปแก้ไม่ได้

เลยไปเสิร์จหามาว่าแต่ละหน้าจะเขียน PHP ดึง URL ได้ยังไงบ้าง

Shop URL

$shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );

My Account URL

$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
if ( $myaccount_page_id ) {
  $myaccount_page_url = get_permalink( $myaccount_page_id );
}

Cart URL

global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();

Checkout URL

global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();p

Payment URL

$payment_page = get_permalink( woocommerce_get_page_id( 'pay' ) );
 
// make ssl if needed
if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' ) $payment_page = str_replace( 'http:', 'https:', $payment_page );op

Logout URL

$myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' );
 
if ( $myaccount_page_id ) {
 
  $logout_url = wp_logout_url( get_permalink( $myaccount_page_id ) );
 
  if ( get_option( 'woocommerce_force_ssl_checkout' ) == 'yes' )
    $logout_url = str_replace( 'http:', 'https:', $logout_url );
}

Reference: https://www.skyverge.com/blog/get-woocommerce-page-urls/


Posted

in

, , ,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *