programing

WooCommercommercommerce:고객 계정 페이지에 사용자 정의 템플릿 추가

lastcode 2023. 6. 21. 22:39
반응형

WooCommercommercommerce:고객 계정 페이지에 사용자 정의 템플릿 추가

고객 '계정' 섹션에 사용자가 주문을 편집할 수 있는 사용자 지정 페이지를 추가하려고 합니다.현재는 URL의 끝점을 설정하고 픽업할 수 있지만, WooCommerce가 페이지 레이아웃을 시작하고 템플릿 위치를 설정할 수 있도록 해야 합니다.

호출할 URL:

/my-account/edit-order/55/

끝점이 설정되고 템플릿이 재정의된 파일에 있습니다.

// Working
add_action( 'init', 'add_endpoint' );
function add_endpoint(){
     add_rewrite_endpoint( 'edit-order', EP_ALL );
}

// need something here to check for end point and run page as woocommerce

// Not been able to test
add_filter( 'wc_get_template', 'custom_endpoint', 10, 5 );
function custom_endpoint($located, $template_name, $args, $template_path, $default_path){

    if( $template_name == 'myaccount/my-account.php' ){
        global $wp_query;
        if(isset($wp_query->query['edit-order'])){
            $located = get_template_directory() . '/woocommerce/myaccount/edit-order.php';
        }
    }

    return $located;
}

도와주셔서 감사합니다.

이것은 WooCommerce 2.6+탭으로 표시된 "내 계정" 페이지 엔드포인트를 확장하고 조작할 수 있는 솔루션입니다(이 답변의 끝에 있는 이 참조 참조). 따라서 이를 달성하기 위해 다음과 같이 할 수 있습니다.

add_action( 'init', 'custom_new_wc_endpoint' );
function custom_new_wc_endpoint() {
    add_rewrite_endpoint( 'edit-order', EP_ROOT | EP_PAGES );
}

add_filter( 'query_vars', 'custom_query_vars', 0 );
function custom_query_vars( $vars ) {
    $vars[] = 'edit-order';
    return $vars;
}

add_action( 'after_switch_theme', 'custom_flush_rewrite_rules' );    
function custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}

// The custom template location
add_action( 'woocommerce_account_edit-order_endpoint', 'custom_endpoint_content' );
function custom_endpoint_content() {
    include 'woocommerce/myaccount/edit-order.php'; 
}

그런 다음 새 주문 편집 끝점을 내 계정 메뉴에 삽입해야 합니다.

add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
function custom_my_account_menu_items( $items ) {
    // Remove the orders menu item.
    $orders_item = $items['orders']; // first we keep it in a variable
    unset( $items['orders'] ); // we unset it then

    // Insert your custom endpoint.
    $items['edit-order'] = __( 'Edit Order', 'woocommerce' );

    // Insert back the logout item.
    $items['orders'] = $orders_item; // we set it back

    return $items;
}

중요:다시 쓰기 규칙(2가지 방법)을 플러시해야 합니다.

  • Permalinks 옵션 페이지로 이동하여 Permal 링크를 다시 저장합니다(헬게이트 바이킹 덕분).
  • 테마를 비활성화/활성화할 수도 있습니다.

참조:

언급URL : https://stackoverflow.com/questions/38051331/woocommerce-adding-custom-template-to-customer-account-pages

반응형