HISE Logo Forum
    • Categories
    • Register
    • Login

    Get data from Woocommerce via server api

    Scheduled Pinned Locked Moved Scripting
    authenticationserverapiwoocommerce
    109 Posts 11 Posters 9.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • orangeO
      orange @d.healey
      last edited by orange

      @d-healey said in Get data from Woocommerce via server api:

      @orange So how do we do it securely? Embedding keys in the binary seems risky. I'm also going to see if making custom endpoints will work.

      If you won't create customers, create orders...etc with Woocommece API, you can give Read only permissions to the keys. So the keys can only be used for getting data. yes it is risky too but at least not on the website compromise level.

      develop Branch / XCode 13.1
      macOS Monterey / M1 Max

      1 Reply Last reply Reply Quote 1
      • orangeO
        orange
        last edited by orange

        I guess Application Passwords Plugin is the alternative to JWT Authentication Plugin. Some people says it is much more easy to use, maybe this method won't be blocked.

        develop Branch / XCode 13.1
        macOS Monterey / M1 Max

        1 Reply Last reply Reply Quote 1
        • orangeO
          orange
          last edited by orange

          By the way, you didn't try to get data with JWT header auth from Woocommerce REST API right?
          JWT can be used for Wordpress REST API only.
          And since every customer is a user at the same time, I think with Wordpress API you should get the user (customer) data.

          develop Branch / XCode 13.1
          macOS Monterey / M1 Max

          d.healeyD 1 Reply Last reply Reply Quote 0
          • d.healeyD
            d.healey @orange
            last edited by

            @orange I tried with both WooCommerce and Wordpress. JWT can be used with WooCommerce endpoints, but they block every user without admin rights. I can't see a way to get customer order details from the Wordpress API.

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            1 Reply Last reply Reply Quote 0
            • Christoph HartC
              Christoph Hart
              last edited by

              Have you tried this:

              Link Preview Image
              JWT on Woocommerce cannot work with "Customer" role user

              I developing mobile apps, that connect directly to woocommerce rest api. I use this plugin for authenticating, like registering, login, make an order, etc. So the issue come, when I register new u...

              favicon

              WordPress Development Stack Exchange (wordpress.stackexchange.com)

              d.healeyD 1 Reply Last reply Reply Quote 1
              • d.healeyD
                d.healey @Christoph Hart
                last edited by d.healey

                Sorry Christoph, somehow I missed your reply. Giving extra priviliages to the user didn't work for me, and seems like a hack anyway.

                @orange I have found the solution, make your own API with custom endpoints. It's actually not too difficult, I found some good information on the wordpress website and YouTube.

                Here is a custom plugin I just made that will get all of the downloadable file data for all of the customer's completed orders. It checks that the current user (via JWT for example) has the role of customer, I'll add more security later to make sure they have permission to download specific files etc. This is just a proof of concept and doesn't take arguments from HISE server calls yet.

                <?php
                /**
                 * Plugin Name: Custom API
                 * Plugin URI: http://localhost/wordpress
                 * Description: Custom REST API endpoints
                 * Version: 1.0
                 * Author: David Healey
                */
                
                function my_orders() {
                
                	$args = [
                		'status' => 'wc-completed',
                		'customer' => 'customer@example.com'
                	];
                
                	$orders = wc_get_orders($args);
                
                	$data = [];
                
                	foreach ($orders as $i => $order) {
                	
                		$items = $order->get_items();
                				
                		$data[$i] = [];
                		
                		foreach($items as $j => $item) {
                			$data[$i][$j] = $item->get_item_downloads();
                		}
                	}
                
                	return $data;
                }
                
                add_action('rest_api_init', function() {
                	register_rest_route('my/v1', 'orders', [
                        'methods' => 'GET',
                		'callback' => 'my_orders',
                    	'permission_callback' => function () {
                		     return wc_current_user_has_role( 'customer' );
                    	}
                    ]);
                });	
                

                The API documentation was tricky to find, every Google search lead to the REST API instead of the plugin API. If you need it here it is - https://developer.woocommerce.com/

                Libre Wave - Freedom respecting instruments and effects
                My Patreon - HISE tutorials
                YouTube Channel - Public HISE tutorials

                orangeO Dan KorneffD 2 Replies Last reply Reply Quote 1
                • orangeO
                  orange @d.healey
                  last edited by

                  @d-healey Thank you for the info. I'll check that out!

                  develop Branch / XCode 13.1
                  macOS Monterey / M1 Max

                  1 Reply Last reply Reply Quote 0
                  • orangeO
                    orange
                    last edited by orange

                    I was struggling with HTTP JWT token authentication for API requests. I edited .htaccess and wp-config files. Taking tokens was fine but http header authentication still was not working.

                    I've found the real cause that is Wordfence Security plugin for Wordpress. The Firewall was blocking the API requests.

                    So, I disabled Prevent discovery of usernames through '/?author=N' scans, the oEmbed API, the WordPress REST API, and WordPress XML Sitemaps option in Firewall Options of Wordfence, and then the issue is solved.

                    develop Branch / XCode 13.1
                    macOS Monterey / M1 Max

                    d.healeyD 1 Reply Last reply Reply Quote 1
                    • d.healeyD
                      d.healey @orange
                      last edited by

                      @orange Are you using a real server or are you using an offline test server like xampp?

                      Libre Wave - Freedom respecting instruments and effects
                      My Patreon - HISE tutorials
                      YouTube Channel - Public HISE tutorials

                      orangeO 1 Reply Last reply Reply Quote 0
                      • orangeO
                        orange @d.healey
                        last edited by orange

                        @d-healey said in Get data from Woocommerce via server api:

                        @orange Are you using a real server or are you using an offline test server like xampp?

                        First, for test purpose I used Xampp. That was ok, no problem.

                        But after that I also tested on the real server, and that was the problem. Sometimes Xampp and Real Server situations can be different.

                        develop Branch / XCode 13.1
                        macOS Monterey / M1 Max

                        1 Reply Last reply Reply Quote 1
                        • d.healeyD
                          d.healey
                          last edited by

                          Anyone here using Woocommerce Amazon S3 Storage extension and custom endpoints?

                          Libre Wave - Freedom respecting instruments and effects
                          My Patreon - HISE tutorials
                          YouTube Channel - Public HISE tutorials

                          LindonL Dan KorneffD 2 Replies Last reply Reply Quote 0
                          • LindonL
                            Lindon @d.healey
                            last edited by

                            @d-healey said in Get data from Woocommerce via server api:

                            Anyone here using Woocommerce Amazon S3 Storage extension and custom endpoints?

                            I think my client is using A3 - but not WooCommerce, its a custom coded end point.

                            HISE Development for hire.
                            www.channelrobot.com

                            1 Reply Last reply Reply Quote 0
                            • Dan KorneffD
                              Dan Korneff @d.healey
                              last edited by

                              @d-healey I use woo + Amazon. What's a custom endpoint?

                              Dan Korneff - Producer / Mixer / Audio Nerd

                              d.healeyD 1 Reply Last reply Reply Quote 0
                              • d.healeyD
                                d.healey @Dan Korneff
                                last edited by

                                @dustbro Custom endpoint is server side code that you can interact with through POST/GET requests.

                                Link Preview Image
                                Adding Custom Endpoints – REST API Handbook | Developer.WordPress.org

                                The WordPress REST API is more than just a set of default routes. It is also a tool for creating custom routes and endpoints. The WordPress…

                                favicon

                                WordPress Developer Resources (developer.wordpress.org)

                                Libre Wave - Freedom respecting instruments and effects
                                My Patreon - HISE tutorials
                                YouTube Channel - Public HISE tutorials

                                1 Reply Last reply Reply Quote 1
                                • orangeO
                                  orange
                                  last edited by orange

                                  I contacted the developer of Woocommerce License Manager plugin.

                                  He is now building a new API that uses JWT Authorization (without API secret or key usage) with much more usable than Woocommerce API stuff. If any of you are interested, there will be no need to make custom endpoints if this new API will be finished.

                                  Below POST/GET requests (such as Activate / Deactivate License with Machine ID, Get Purchased Products, Get Product Image / Meta Data, Add License that bought from a reseller...etc) can be made with only a user login and this API. User roles can be adjusted also.

                                  alt text

                                  develop Branch / XCode 13.1
                                  macOS Monterey / M1 Max

                                  1 Reply Last reply Reply Quote 1
                                  • d.healeyD
                                    d.healey
                                    last edited by

                                    I'll still need to custom endpoints, the APIs don't provide all the options I need.

                                    Libre Wave - Freedom respecting instruments and effects
                                    My Patreon - HISE tutorials
                                    YouTube Channel - Public HISE tutorials

                                    orangeO 1 Reply Last reply Reply Quote 0
                                    • orangeO
                                      orange @d.healey
                                      last edited by orange

                                      @d-healey said in Get data from Woocommerce via server api:

                                      I'll still need to custom endpoints, the APIs don't provide all the options I need.

                                      Which kind of endpoints do you need more?

                                      develop Branch / XCode 13.1
                                      macOS Monterey / M1 Max

                                      1 Reply Last reply Reply Quote 0
                                      • Y
                                        yall
                                        last edited by

                                        @d-healey I tried to read everything and understand it is still very difficult. would it be possible to have an example of how to load an image from a server? this could help to better understand this system. I have tried many times without success. I myself have a nas at home but I can't. if you want it of course :)

                                        d.healeyD 1 Reply Last reply Reply Quote 0
                                        • d.healeyD
                                          d.healey @yall
                                          last edited by d.healey

                                          @yall said in Get data from Woocommerce via server api:

                                          would it be possible to have an example of how to load an image from a server?

                                          Link Preview Image
                                          Images from server

                                          If I clear that list then it will download again. Does this list persist in the compiled plugin or is it just in HISE? Nope, that's also the functionality i...

                                          favicon

                                          Forum (forum.hise.audio)

                                          @orange said in Get data from Woocommerce via server api:

                                          Which kind of endpoints do you need more?

                                          A custom one :p It's a lot to explain but basically I'm letting the user log in from my plugin, I display all of the products available on my site that can be loaded as an expansion, I also indicate which products the user has a license for and allow them to install/uninstall those ones. I want to do this in as few server requests as possible, using a custom end point allows me to do everything except installing in 2 requests. And a few additional requests to download product images. The number of requests required for installing depends on the number of .hr files that are needed.

                                          Anyway the problem I'm having at the moment is the WooCommerce function to provide an S3 download link is not working properly. I've asked on the WooCommerce support page but no luck so far.

                                          Update: I solved my issue, I'd setup my Amazon credentials incorrectly.

                                          Libre Wave - Freedom respecting instruments and effects
                                          My Patreon - HISE tutorials
                                          YouTube Channel - Public HISE tutorials

                                          Dan KorneffD 1 Reply Last reply Reply Quote 3
                                          • Dan KorneffD
                                            Dan Korneff @d.healey
                                            last edited by

                                            @d-healey said in Get data from Woocommerce via server api:

                                            display all of the products available on my site that can be loaded as an expansion

                                            I can't wait to buy this from you :)

                                            Dan Korneff - Producer / Mixer / Audio Nerd

                                            1 Reply Last reply Reply Quote 2
                                            • First post
                                              Last post

                                            22

                                            Online

                                            1.7k

                                            Users

                                            11.8k

                                            Topics

                                            102.4k

                                            Posts