• Resolved namespending

    (@namespending)


    Hello,

    I’m wondering if it is in the pipeline to allow for multiple default wishlists for multiple users?
    For example, having the following lists for a gaming website:

    – Backlog
    – Played
    – Wishlist

    Users can obviously create these themselves, however I would rather have them default to that. Alternatively, is this something that is already possible that I’m not aware of?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Fauzan Azizie

    (@fauzanade)

    Hi @namespending,

    Thanks for the suggestion, and the gaming site example makes the use case really clear.

    To answer your second question first: no, this isn’t something the plugin can do at the moment. Right now there’s only a single default wishlist (you can change its label under the wishlist settings), and any additional lists have to be created by each user themselves.

    The good news is that pre-defined wishlists for all users is already on our development team’s radar, and I’ve added your request to the existing feature ticket to help with prioritization. I can’t promise a timeline, but it’s actively being looked at.

    In the meantime, you can achieve this with a small code snippet that creates your chosen wishlists whenever a new user registers. Two things to know before using it: it only applies to users who register after you add it (existing users keep whatever they have), and you should remove it once the plugin supports this natively.

    Add this via your child theme’s functions.php or a code snippets plugin, and edit the $wishlist_names line to the list names you want. The first name in the list becomes each user’s default wishlist:

    /**
    * Create a set of default wishlists for every new user.
    *
    * Remove this snippet if the wishlist plugin adds this feature natively.
    */
    add_action( 'user_register', 'my_seed_default_wishlists', 20 );

    function my_seed_default_wishlists( $user_id ) {

    // Bail if the wishlist plugin is not active.
    if ( ! class_exists( '\SaveToWishlist\Classes\Factories\Collections' ) ) {
    return;
    }

    // Edit these names. The FIRST one becomes the user's default wishlist.
    $wishlist_names = array( 'Wishlist', 'Backlog', 'Played' );

    // Only ever seed a user once.
    if ( get_user_meta( $user_id, '_my_default_wishlists_seeded', true ) ) {
    return;
    }

    $collections = \SaveToWishlist\Classes\Factories\Collections::instance();

    $first_id = 0;
    foreach ( $wishlist_names as $index => $name ) {
    $created = $collections->save_collection(
    array(
    'user_id' => $user_id,
    'name' => $name,
    'description' => '',
    'status' => 'publish',
    'is_default' => 0 === $index ? 1 : 0,
    'is_public' => 0,
    )
    );

    if ( 0 === $index && ! empty( $created->id ) ) {
    $first_id = (int) $created->id;
    }
    }

    // Make sure the first wishlist ends up as the default one.
    if ( $first_id ) {
    $collections->update_default_wishlist( $user_id );
    $collections->update_default_wishlist( $user_id, 1, $first_id );
    }

    update_user_meta( $user_id, '_my_default_wishlists_seeded', 1 );
    }

    I tested this on a fresh site running the current version of the plugin: new users get all three lists on registration with the first one set as their default, and it won’t create duplicates if it runs more than once.

    Hope that helps you get set up in the meantime. Thanks again for taking the time to share the idea!

    Thread Starter namespending

    (@namespending)

    ooo thank you! I’ve just tried registering a user after adding this to my code snippets (WPCode) where I’ve tried both HTML and Universal types, which hasn’t worked?

    Any ideas?

    Thread Starter namespending

    (@namespending)

    Sorry meant to say I’d also tried .php which also hasn’t worked.

    I am registering a user in the back end if that matters?

    Plugin Support Fauzan Azizie

    (@fauzanade)

    Hi @namespending,

    Registering the user from the backend is fine – the snippet runs for users created there too. So before changing anything, can you tell me where exactly you’re checking for the wishlists? I suspect the snippet may already be working and the lists just aren’t showing where you’re looking.

    The admin Wishlists screen hides empty wishlists belonging to regular users by default, and the lists the snippet creates start out empty. Two ways to confirm:

    1. The most reliable check: log in as the newly created user (or use a user-switching plugin) and open the wishlist page. All three lists should be there, with the first one marked as the default.
    2. Or on the admin Wishlists screen, change the visibility dropdown from “Hide empty (non-admin)” to “Show all wishlists”, then look for your test user’s lists.

    If you still see nothing after checking as the user, then it’s the snippet setup, so it’s worth verifying in WPCode:

    1. The code type is “PHP Snippet”.
    2. The snippet is toggled “Active”. One WPCode quirk to know about: if you edit and re-save a snippet that’s already active, or save the same code as a second snippet, WPCode can detect an error and quietly switch it off. So if you saved it more than once while testing, re-check the toggle.
    3. Under Insertion, Auto Insert is set to “Run Everywhere” (not “Frontend Only”, since you’re registering users in the backend).
    4. Open the snippet and check the line near the top still reads \SaveToWishlist\Classes\Factories\Collections with all the backslashes intact. If they get lost in copy-paste (some apps strip them), the snippet runs without errors but does nothing.
    5. Test with a brand-new user each time. Users registered while the snippet wasn’t running don’t get the lists retroactively.

    I’ve also improved the snippet since my first post, so please replace your copy with this version. It shows your first-named list at the top of the wishlist page (the original put it at the bottom), and it’s now safe to edit and re-save in WPCode:

    /**
    * Create a set of default wishlists for every new user.
    *
    * Remove this snippet if the wishlist plugin adds this feature natively.
    */
    add_action( 'user_register', 'my_seed_default_wishlists', 20 );

    if ( ! function_exists( 'my_seed_default_wishlists' ) ) {
    function my_seed_default_wishlists( $user_id ) {

    // Bail if the wishlist plugin is not active.
    if ( ! class_exists( '\SaveToWishlist\Classes\Factories\Collections' ) ) {
    return;
    }

    // Edit these names. The FIRST one becomes the user's default wishlist.
    $wishlist_names = array( 'Wishlist', 'Backlog', 'Played' );

    // Only ever seed a user once.
    if ( get_user_meta( $user_id, '_my_default_wishlists_seeded', true ) ) {
    return;
    }

    $collections = \SaveToWishlist\Classes\Factories\Collections::instance();

    // Create in reverse order so the first name is the newest wishlist,
    // which puts it at the top of the wishlist page (sorted newest first).
    $default_id = 0;
    foreach ( array_reverse( $wishlist_names ) as $name ) {
    $created = $collections->save_collection(
    array(
    'user_id' => $user_id,
    'name' => $name,
    'description' => '',
    'status' => 'publish',
    'is_default' => 0,
    'is_public' => 0,
    )
    );

    if ( ! empty( $created->id ) ) {
    $default_id = (int) $created->id;
    }
    }

    // Make sure the first-named wishlist (created last) is the default one.
    if ( $default_id ) {
    $collections->update_default_wishlist( $user_id );
    $collections->update_default_wishlist( $user_id, 1, $default_id );
    }

    update_user_meta( $user_id, '_my_default_wishlists_seeded', 1 );
    }
    }

    I re-tested this exact version today on a fresh site running the current plugin version, installed through WPCode with the user registered from the backend, and it works: the new user gets all three lists with “Wishlist” on top as the default.

    Let me know what you find and we’ll get you sorted!

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.