• Resolved araislamara

    (@araislamara)


    hey there,

    I was wondering if you guys offer any API endpoint to get the items in the whitelist for Sepisifec users.

    or any PHP function to get the product IDs for that user.

    I checked get_user_metabox, and there is no data for your plugin.

    Thanx you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Bartek

    (@bartj)

    Hi @araislamara!

    Although, there isn’t anything formalized for external use, you could achieve your goal with a bit of coding and reading of our source code (I warn you, it’s an experienced solution).

    Flexible Wishlist exposes flexible_wishlist/init a hook, which has access to WhishlistRepository? – a class being able to query the whole wishlist for a user.

    A code example could look like (please, don’t treat it as production ready code, you need to change it according to your requirements):

    do_action(
      'flexible_wishlist/init',
      function ( $_, $wishlist_repository ) {
        $user_id = get_current_user_id();
        $wishlist = $wishlist_repository->get_by_user( $user_id );
        $wishlist_items = $wishlist->get_items();
    
        $product_id = [];
        foreach ( $wishlist_items as $wishlist_item ) {
          $product_id[] = $wishlist_item->get_product_id();
        }
    
        // Do something with populated products ids.
      },
      10,
      2
    );
    Thread Starter araislamara

    (@araislamara)

    hey

    Thanx you , i used your code as part of custom endpoint

    unfortunately it returns an emty response

    add_action( 'rest_api_init', function () {
    
        register_rest_route( 'my_flexible_test', '/v1', array(
    
          'methods' => 'GET',
    
          'callback' => 'myfuncallback',
    
        ) );
    
      } );
    
      function myfuncallback($request){
    
          return do_action(
    
              'flexible_wishlist/init',
    
              function ( $_, $wishlist_repository ) {
    
                $user_id = 1;
    
                $wishlist = $wishlist_repository->get_by_user( $user_id );
    
                $wishlist_items = $wishlist->get_items();
    
                $product_id = [];
    
                foreach ( $wishlist_items as $wishlist_item ) {
    
                  $product_id[] = $wishlist_item->get_product_id();
    
                }
    
                return $product_id;
    
              },
    
              10,
    
              2
    
          );
    
      }

    Plugin Contributor Bartek

    (@bartj)

    Hi @araislamara!

    I can see some mistakes in your code:

    1. You don’t attach, but fire an action. Use add_action, not do_action
    2. You are trying to attach to hook from within REST API callback, but this way, you try to hook into action after it has been executed. It isn’t possible in WordPress, as you should attach all your hook callbacks before a hook will be dispatched (i.e. with do_action)

    To do this correctly, you should first hook into Flexible Wishlist, and from within, register a REST API route. It is important to preserve a correct order of execution.

    Here’s an example:

    function get_wishlist_products_by_user( $wishlist_repository, $user_id ) {
      /** @var array $wishlist One user can have multiple wishlists */
      $wishlists = $wishlist_repository->get_by_user( $user_id );
      $product_id = [];
      foreach ( $wishlists as $wishlist ) {
        $wishlist_items = $wishlist->get_items();
        foreach ( $wishlist_items as $wishlist_item ) {
          $product_id[] = $wishlist_item->get_product_id();
        }
      }
      return $product_id;
    }
    
    // Hook into Flexible Wishlist initiation point
    add_action(
      'flexible_wishlist/init',
      // Here we can access WishlistRepository, which we will need to get products for user
      function ( $_, $wishlist_repository ) {
    
        // Register our REST API route
        add_action(
          'rest_api_init',
          // This may be done better with OOP and keeping wishlist repository as class field
          // Otherwise, we need to pass it down as closure argument
          function () use ( $wishlist_repository ) {
            register_rest_route(
              'my_flexible_test',
              '/v1',
              [
                'methods' => 'GET',
                'callback' => static function () use( $wishlist_repository ) {
                  return rest_ensure_response(
                    get_wishlist_products_by_user( $wishlist_repository, 1 )
                  );
                },
                'permission_callback' => '__return_true'
              ]
            );
          }
        );
    
      },
      10,
      2
    );
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘rest api or php functions’ is closed to new replies.