R plus Magento 2 REST API revisited: part 2 – filtered search

[This article was first published on R – Alex Levashov – eCommerce Consultant (Melbourne, Australia), and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In previous post I wrote about how to set connection with Magento 2 REST API from R and make queries to universal search. Now let’s see how  use filtered search. Also we’ll use other than universal search endpoints that provide access to more specific Magento 2 store information.

Using filtered responses is a good way to get only a subset of the data you really need. Why it makes sense? As we seen the full object complex object like invoice pulled from Magento can be 10-12 Kb per record.  Filtered responses:

  • Save bandwidth
  • Reduce time – you get your results faster because of lower time to transmit the data
  • Save memory used by R to store data

In Magento 2 REST API filtered responses for most endpoints are defined through fields parameter, where you can list only fields you want to received. So if you need just couple pieces of the information you don’t have to get and store the whole object.

Below examples assume that you set your store connection details (url) and received authentication token and stored it in auth variable as described in previous post.

Filtered product details

Let’s see an example, a function that return filtered product details for specific SKU.

getm2productdata <- function (url, sku, fields, auth){ url <- paste0(urlbase, "index.php/rest/V1/products/", sku, "?fields=", fields) request <- GET (url, add_headers ("Content-Type" = "application/json", "Authorization" = auth)) product <- content(request, as = "parsed") return (product) }

To use this function and get product SKU, price, name and type we can make a next call

product <- getm2productdata (url=urlbase, sku="24-MB01", fields="sku,price,name,type_id", auth=auth)
With Magento 2 store populated by official sample data the resulting object will look the next way:

product $`sku` [1] "24-MB01" $name [1] "Joust Duffle Bag" $price [1] 34 $type_id [1] "simple"

Much more concise and simple, isn’t it?

Filtered product information for specific order

Define a function
getm2customerdata <- function(url, order_id, fields, auth) { url <- paste0(urlbase, "/rest/default/V1/orders/", order_id, "?fields=", fields) request <- GET (url, add_headers ("Content-Type" = "application/json", "Authorization" = auth)) customer <- content(request, as ="parsed") return (customer) }
Important to note that order_id parameter here refers to internal order id (entity id), not order number that you see in customer receipt.
Call the function to get customer name, id and billing address

order_id <-"1" fields <- "billing_address,customer_firstname,customer_lastname,customer_id" customer <- getm2customerdata (urlbase, order_id, fields, auth)

The resulting object will be a more complex list now, because billing address is a list itself with 15 objects stored, but still much more compact than full, unfiltered order object.

More complex filtering – shipment data

Let’s get the filtered data about shipment. Now instead of getting full complex object (like billing address previously), we will filter what we want to get.

First we define a function that call shipment endpoint, similar way we did with other endpoints
getm2shipmentdata <- function(url, shipment_id, fields, auth) { url <- paste0(url, "/rest/default/V1/shipment/", shipment_id, "?fields=", fields) request <- GET (url, add_headers ("Content-Type" = "application/json", "Authorization" = auth)) shipment_data <- content(request, as ="parsed") return (shipment_data) }

Then we call it to get only name, qty and sku of the products shipped.

shipment_id <- "1" fields <-"items[name,qty,sku]" shipped_products <- getm2shipmentdata (urlbase, shipment_id, fields, auth)
Note that we defined the data we want to get from items in square brackets. In Magento 2 API it is referred as nested object call.
If we print shipped_products, the result looks like below:
> shipped_products $`items` $`items`[[1]] $`items`[[1]]$`name` [1] "Iris Workout Top" $`items`[[1]]$sku [1] "WS03-XS-Red" $`items`[[1]]$qty [1] 1

Another nested object call example – product information

Function:
getm2singleproductinfo <- function (url, sku, fields, auth){ url <- paste0(url, "rest/default/V1/products/", sku, "/?fields=", fields) request <- GET (url, add_headers ("Content-Type" = "application/json", "Authorization" = auth)) product_data <- content(request, as ="parsed") return (product_data) }

Call – get product sku, name, category link and stock item objects (last 2 defined in extension_attributes)

fields <- "name,sku,extension_attributes[category_links,stock_item[item_id,qty]]" sku <- "24-WB02" product_nested <- getm2nestedproductinfo(urlbase, sku, fields, auth)

Resulting object is a complex list, screenshot from Rstudio:

Nested product - Rstudio screenshot

Another example call of the same function – get product sku, name, type and images urls
fields <- "name,sku,type_id,media_gallery_entries[media_type,file]" sku <- "acj003" product_nested3 <- getm2nestedproductinfo(urlbase, sku, fields, auth)

Filtered search helps to get specific data from Magento rather than pull the whole often bulky object, saving your request time, bandwidth and memory. It is possible to filter results of universal search, just with a small difference of the request format. It will be a subject of the next article, where I also work with more complex sample tasks:

  • Get names and emails of all customers who have placed no orders in the given period of time
  • Compare sales for products in 2 categories for the period of time

Stay tuned!

The post R plus Magento 2 REST API revisited: part 2 – filtered search appeared first on Alex Levashov - eCommerce Consultant (Melbourne, Australia).

To leave a comment for the author, please follow the link and comment on their blog: R – Alex Levashov – eCommerce Consultant (Melbourne, Australia).

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)