How to add external custom url to woocommerce endp

2020-05-03 10:19发布

I want to add a custom endpoint url to my-account page on woocommerce. is it possible? So when customer clicks to this link they will redirect to my youtube page

function custom_wc_end_point() {
    if(class_exists('WooCommerce')){
    add_rewrite_endpoint( 'videos', EP_ROOT | EP_PAGES );
}
}
add_action( 'init', 'custom_wc_end_point' );
function custom_endpoint_query_vars( $vars ) {
    $vars[] = 'videos';
    return $vars;
}
add_filter( 'query_vars', 'custom_endpoint_query_vars', 0 );
function ac_custom_flush_rewrite_rules() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'ac_custom_flush_rewrite_rules' );
// add the custom endpoint in the my account nav items
function custom_endpoint_acct_menu_item( $items ) {

    $download = $items['downloads'];
    unset( $items['downloads'] );
    $items['videos'] = __( 'Watch Videos ', 'woocommerce' ); // replace videos with your endpoint name
    $items['downloads'] = $download;
        return $items;
}


add_filter( 'woocommerce_account_menu_items', 'custom_endpoint_acct_menu_item' );

function youtube_custom_endpoint() {
      // Is it possible wehn click on this link it move to my youtube page

}
add_action( 'woocommerce_account_videos_endpoint', 'youtube_custom_endpoint' );

3条回答
爷的心禁止访问
2楼-- · 2020-05-03 10:49

I know this question has been around for a while but i'd like to write my answer to help someone either now or in future. Was working on a Digital eCommerce project that requires heavy customization to match EDD. This question is one of the customization (in which i don't want the edit-address endpoint to be accessible; instead it should redirect to edit-account endpoint. I as well tested this code to answer your question and was redirected to YouTube. Customize to suit your need.

function howabout_redirect_endpoint () {
    $my_account_link = get_permalink( get_option('woocommerce_myaccount_page_id') );

    /*
    * Redirect to any URL of your choice 
    */
    $location_1 = 'https://youtube.com';

    /*
    * Redirect to Other Endpoint of Choice, i chose Edit Account Endpoint  
   **/
    $location_2 = $my_account_link.'edit-account/'; 

    if (is_wc_endpoint_url( 'edit-address' )) { //You can add your custom Endpoint here.
        wp_redirect( $location_1 ); //Pass any of the Variable in here, i chose the youtube variable, which answers this question
        exit;
    }
}
add_action( 'template_redirect', 'howabout_redirect_endpoint' );

Code goes in function.php file of your active child theme (or active theme). Tested and works.

WP: v5.1.1
WC: v3.6.2

查看更多
放我归山
3楼-- · 2020-05-03 10:58
add_filter ( 'woocommerce_account_menu_items', 'misha_one_more_link' );
function misha_one_more_link( $menu_links ){

    // we will hook "anyuniquetext123" later
    $new = array( 'anyuniquetext123' => '**Candidate Dashboard**' );

    // or in case you need 2 links
    // $new = array( 'link1' => 'Link 1', 'link2' => 'Link 2' );

    // array_slice() is good when you want to add an element between the other ones
    $menu_links = array_slice( $menu_links, 0, 1, true ) 
    + $new 
    + array_slice( $menu_links, 1, NULL, true );


    return $menu_links;
}

add_filter( 'woocommerce_get_endpoint_url', 'misha_hook_endpoint', 10, 4 );
function misha_hook_endpoint( $url, $endpoint, $value, $permalink ){

    if( $endpoint === 'anyuniquetext123' ) {

        // ok, here is the place for your custom URL, it could be external
        $url = **'http://alatta.org.ye/candidate-dashboard/';**

    }
    return $url;

}

just change the "Candidate Dashboard" which is the the page name or the title name that appear in the my-account menu.

second thing change the URL to your url, i make it in bold the things to be changed.

Best,

查看更多
一夜七次
4楼-- · 2020-05-03 11:01

You can use the "woocommerce_get_endpoint_url" filter to achieve what you want:

add_filter( 'woocommerce_get_endpoint_url', 'maybe_redirect_endpoint', 10, 4 );
function maybe_redirect_endpoint ($url, $endpoint, $value, $permalink)
{
    if( $endpoint == 'my-custom-endpoint')
        $url = 'https://www.youtube.com/watch?v=Q0q1gCsZykg';
    return $url;
}
查看更多
登录 后发表回答