• indepthphotos

    (@indepthphotos)


    Hi, I have created a Wishlist tab in My Account and would like to ammend the button on the product pages to point to my-account/wishlist/

    Currently it can only be set to the My Account page. Is there a way of possibly doing this?

    Many thanks
    Dave

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter indepthphotos

    (@indepthphotos)

    I found a temporary solution by amending some of the plugin code, but it would be great if you could add this as a feature for logged-in users.



    Custom End Point Code

    /**
     * 1. Register new endpoint (URL) for My Account page
     * Note: Re-save Permalinks or it will give a 404 error
     */
    function add_wishlist_endpoint() {
        add_rewrite_endpoint( 'wishlist', EP_ROOT | EP_PAGES );
    }
    add_action( 'init', 'add_wishlist_endpoint' );
    
    /**
     * 2. Add new query var
     */
    function add_wishlist_query_var( $vars ) {
        $vars[] = 'wishlist';
        return $vars;
    }
    add_filter( 'query_vars', 'add_wishlist_query_var', 0 );
    
    /**
     * 3. Insert the new endpoint into the My Account menu
     */
    function add_wishlist_tab_to_my_account( $items ) {
        $items['wishlist'] = 'Wishlist';
        return $items;
    }
    add_filter( 'woocommerce_account_menu_items', 'add_wishlist_tab_to_my_account' );
    
    /**
     * 4. Add content to the new "Wishlist" tab
     */
    function display_wishlist_tab_content() {
        echo '<h3>Wishlist</h3>';
        echo do_shortcode( '[wishsuite_table]' );
    }
    add_action( 'woocommerce_account_wishlist_endpoint', 'display_wishlist_tab_content' );
    

    Amended Functions in helper-functions

    
    function wishsuite_get_post_list( $post_type = 'page' ) {
        $options = array();
        $options['0'] = __('Select', 'wishsuite');
        $perpage = -1;
        $all_post = array('posts_per_page' => $perpage, 'post_type' => $post_type);
        $post_terms = get_posts($all_post);
    
        // Add 'My Account > Wishlist' option
        $options['my_account_wishlist'] = 'My Account > Wishlist';
    
        if (!empty($post_terms) && !is_wp_error($post_terms)) {
            foreach ($post_terms as $term) {
                $options[$term->ID] = $term->post_title;
            }
        }
    
        return $options;
    }
    
    function wishsuite_get_page_url() {
        $page_id = wishsuite_get_option('wishlist_page', 'wishsuite_table_settings_tabs');
    
        // Check if page_id is 'my_account_wishlist'
        if ($page_id === 'my_account_wishlist') {
            // Return the custom URL for 'My Account > Wishlist'
            return site_url('/my-account/wishlist/');
        } else {
            // Return the default URL based on $page_id
            return get_permalink($page_id);
        }
    }

    Regards, Dave

    Plugin Contributor Tarek Aziz

    (@tarekht)

    Hi Dave,

    Thank you very much for getting in touch with us.

    I have thoroughly reviewed your message and understand your concern.

    Currently, our plugin doesn’t have the option to incorporate custom endpoints. However, it’s great to know that you have already found a workaround.

    You can keep that code snippet for now, but we recommend using the code in the functions.php file.

    This will ensure that the code remains intact even if you update the plugin in the future.

    Regarding your request to add this option as a feature, I’ll discuss this matter with our development team and see what we can do about it.

    Have a great day!

    Regards, Tarek

    • This reply was modified 1 year ago by Tarek Aziz.
    Thread Starter indepthphotos

    (@indepthphotos)

    Hi Tarek,

    That is great thanks.

    I have managed to implement the use of an endpoint within your plugin by amending just one function and then amending the social share template.

    This enables the button links to take a logged-in user to my-account rather than the page_id of where it usually is. It does however stay the same if a user is not logged in and will link to the page_id as previously set in settings.

    function wishsuite_get_page_url() {
    // Check if the user is logged in
    if (is_user_logged_in()) {
    // If logged in, return the URL for the wishlist page in the user's account
    return home_url('/my-account/mywishlist/');
    } else {
    // If not logged in, get the page ID from the options and return the corresponding URL
    $page_id = wishsuite_get_option('wishlist_page', 'wishsuite_table_settings_tabs');
    return get_permalink($page_id);
    }
    }

    In the social share template, I amended the $share_link line at the top so that it will return the public share page when in the my-account/mywishlist tab as follows.

    $page_id = wishsuite_get_option('wishlist_page', 'wishsuite_table_settings_tabs');
    $page_url = get_permalink($page_id);
    $share_link = $page_url . '?wishsuitepids=' . $idsString;

    Another addition I added is a copy-share link underneath the social icons at the bottom of the page so a user can easily copy just the link to the clipboard for easily pasting elsewhere.

    <div class="wishsuite-copy-button-container"> <button class="wishsuite-button wishsuite-copy-button">Copy Share Link</button></div><input type="hidden" id="wishsuite-share-link" value="<?= $share_link ?>">

    The JS to make this copy button work, is as follows.

    $(function() {
    // Define the copyToClipboard function
    function copy_ShareUrlToClipboard() {
        console.log('Function called');
        // Get the share link value
        var shareLink = $('#wishsuite-share-link').val();
        console.log('Share link:', shareLink);
    
        // Create a hidden input field to hold the share link
        var input = $('<input>').attr('type', 'text').attr('id', 'link-to-copy').val(shareLink);
    
        // Append the input to the body
        $('body').append(input);
    
        // Focus on the input to select its text
        input.trigger('focus');
        input.trigger('select');
    
        // Copy the selected text to the clipboard
        document.execCommand('copy');
    
        // Remove the input from the body
        input.remove();
    
        // Display a success message
        alert('Share link copied to clipboard!');
    }
    
    // Add an event listener to the button
    $('.wishsuite-copy-button').on('click', copy_ShareUrlToClipboard);
    });

    The code for the endpoint creation is now as follows but currently still sits in my code snippets.

    // Description: Snippet to add custom endpoint to MyAccount Page.
    
    function mywishlist_custom_endpoint() {
    add_rewrite_endpoint( 'mywishlist', EP_ROOT | EP_PAGES );
    }
    add_action( 'init', 'mywishlist_custom_endpoint' );
    
    add_filter( 'woocommerce_account_menu_items', 'mywishlist_new_menu_items' );
    
    // Insert the new endpoint into the My Account menu.
    
    function mywishlist_new_menu_items( $items ) {
    $items[ 'mywishlist' ] = __( 'Wishlist' );
    return $items;
    }
    
    $endpoint = 'mywishlist';
    add_action( 'woocommerce_account_' . $endpoint . '_endpoint', 'mywishlist_endpoint_content' );
    function mywishlist_endpoint_content() {
    //content goes here
    echo 'Wishlist';
    echo do_shortcode( '[wishsuite_table]' );
    }

    Happy for you to pass on my ammendments and have them checked over for suitability. But all working on my end and is just a nice couple of little added features.

    Regards
    Dave

    Plugin Contributor Tarek Aziz

    (@tarekht)

    Hi Dave,

    Good morning from here!

    Thank you so very much for explaining the workaround in such a detailed manner. Your effort is greatly appreciated ??

    I will certainly pass it on to our development team so that they can thoroughly review your codes and the way you implemented them.

    Once the reviewing is done, we’ll try our best to integrate these features into our plugin.

    Wishing you a fantastic day ahead!

    Best Regards,
    Tarek Aziz

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add Endpoint to My Account’ is closed to new replies.