Developing an Extension for WooCommerce

August 4, 2017
admin

Undoubtedly, WordPress is the lion of CMS universe. Used by over 20% of all websites, this small blogging platform has risen its voice all over the web… And its popularity has been constantly higher than other platforms.

Among various ways of using WordPress, an eCommerce website is a highly practiced one. WordPress is an open source framework, therefore, a rich community as well. WordPress has fine solutions for each issue from its nerdy members around the world. Yes! For instance, WooCommerce is a boon for an eCommerce solution.

WooCommerce is a plugin for excelling eCommerce and according to this article published in the WooCommerce official site, it powers over 30% of all online shops. The amazing WooCommerce and WordPress together make a perfect platform for the large and small storekeepers anywhere. Not only that, you will get such incredible framework for FREE.

However, when choosing an eCommerce platform, We recommend that you don’t judge WooCommerce solely on the core’s strengths and weaknesses. After all, if you want to witness the true potential of WooCommerce – you will need extensions to unlock and extend its powers. In this article, we have discussed the technical aspects of developing an extension for WordPress based WooCommerce sites using Ruby on Rails.

Prerequisites

Choose Hosting:
You need to be running WordPress on a self-hosted site.

Install WordPress:
Now you have a space to start building your website. To lay the foundation, use the free WordPress software platform. Many hosting companies have one-click WordPress installation available, so be sure to look for this option or ask if it’s available. Otherwise, you can download from WordPress.org and then install by following instructions.

Pick a Theme:
Decide on the look of your website/store by selecting a theme. Use a free theme included with WordPress, or purchase a premium theme.

Activate WooCommerce:
With your website up and running, it’s time to install eCommerce functionality with WooCommerce. Search for the WooCommerce plugin from within your website:

  • Go to your website
  • Go to: Dashboard > Plugins > Add New > Search Plugins
  • Search for “WooCommerce”
  • Install
  • Activate
REST API keys

Pre-generated keys can be used to authenticate use of the REST API endpoints. New keys can be generated either through the WordPress admin interface or they can be auto-generated through an endpoint.

Generating API keys in the WordPress admin interface
To create or manage keys for a specific WordPress user, go to WooCommerce > Settings > API > Keys/Apps.

Click the “Add Key” button. In the next screen, add a description and select the WordPress user you would like to generate the key for. Use of the REST API with the generated keys will conform to that user’s WordPress roles and capabilities.

Choose the level of access for this REST API key, which can be Read access, Write access or Read/Write access. Then click the “Generate API Key” button and WooCommerce will generate REST API keys for the selected user.

Now that keys have been generated, you should see two new keys, a QRCode, and a Revoke API Key button. These two keys are your Consumer Key and Consumer Secret.

Auto generating API keys using our Application Authentication Endpoint
This endpoint can be used by any APP to allow users to generate API keys for your APP. This makes integration with WooCommerce API easier because the user only needs to grant access to your APP via a URL. After being redirected back to your APP, the API keys will be sent back in a separate POST request.

The following image illustrates how this works:

Auto generating API keys using our Application Authentication Endpoint

URL parameters
  • app_name: Your APP name
  • scope: Level of access. Available: read, write and read_write
  • user_id: User ID in your APP. For your internal reference, used when the user is redirected back to your APP. NOT THE USER ID IN WOOCOMMERCE
  • return_url: URL the user will be redirected to after authentication
  • callback_url: URL that will receive the generated API key. Note: this URL should be over HTTPS
Creating an authentication endpoint URL

You must use the /wc-auth/v1/authorize endpoint and pass the above parameters as a query string.

Example of how to build an authentication URL (using Ruby):

require "uri"

store_url = 'http://example.com' 
endpoint = '/wc-auth/v1/authorize' 
params = { app_name: "My App Name", 
           scope: "read_write", 
           user_id: 123, 
           return_url: "http://app.com/return-page", 
           callback_url: "https://app.com/callback-endpoint" 
          }

query_string = URI.encode_www_form(params) 
fullpage_redirect_to("#{store_url}#{endpoint}?#{query_string}")

After a GET request is sent to the authentication endpoint URL, the user will see a confirmation screen like below:

confirmation screen

While redirecting the user using return_url, you are also sent success and user_id parameters as query strings. Success sends 0 if the user denied, or 1 if authenticated successfully. Use user_id to identify the user when redirected back to the (return_url) and also remember to save the API Keys when your callback_url is posted to after auth.

Example of JSON posted with the API Keys

{ 
"key_id": 1, 
"user_id": 123, 
"consumer_key": "ck_xxxxxxxxxxxxxxxx", 
"consumer_secret": "cs_xxxxxxxxxxxxxxxx", 
"key_permissions": "read_write" 
}

You may use HTTP Basic Auth by providing the REST API Consumer Key as the username and the REST API Consumer Secret as the password.

require "woocommerce_api"
woocommerce = WooCommerce::API.new(
 "http://example.com",
 "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
 "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
 {
  wp_api: true,
  version: "wc/v2"
 }
)

GET
woocommerce.get(endpoint, query)

POST
woocommerce.post(endpoint, data)

PUT
woocommerce.put(endpoint, data)

DELETE
woocommerce.delete(endpoint, query)

endpoint: WooCommerce API endpoint, example: customers or order/12
data: Only for POST and PUT, data that will be converted to JSON
query: Only for GET and DELETE, request query string

Now we are all set. We can start consuming the WooCommerce API using the consumer key and consumer secret. For example, the products API allows you to create, view, update, and delete individual, or a batch, of products.

Example of creating a product (using Ruby):

data = {
  name: "Sample Product",
  type: "simple",
  regular_price: "21.99",
  description: "Sample Description", 
  images: [
  {
    src: "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/
06/T_2_front.jpg",
     position: 0
   }
 ]
}

woocommerce.post("products", data).parsed_response

You can find out the complete API reference here.

References
  1. https://docs.woocommerce.com/document/woocommerce-rest-api/
  2. http://woocommerce.github.io/woocommerce-rest-api-docs/
  3. https://github.com/woocommerce/wc-api-ruby

Contributor: Jumayel IslamNascenia

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.