Created
February 28, 2014 22:14
-
-
Save alexcheng1982/9281109 to your computer and use it in GitHub Desktop.
Use Ruby to get Magento categories http://midgetontoes.com/blog/2014/03/01/use-web-service-to-get-magento-categories/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'savon' | |
categories = [] | |
def process(categories, category) | |
categories << {:id => get_category_value(category, 'category_id'), | |
:name => get_category_value(category, 'name')} | |
children = get_category_value(category, 'children') | |
return if children.nil? or children[:item].nil? | |
childrenItem = children[:item] | |
childrenItem = [childrenItem] unless childrenItem.kind_of?(Array) | |
childrenItem.each {|child| process(categories, child)} | |
end | |
def get_category_value(category, key) | |
items = category[:item].select {|item| item.has_key?(:key) and item[:key] == key } | |
return items[0][:value] unless items.empty? | |
end | |
client = Savon.client(wsdl: "http://magentolocal.com:8888/magento/api/soap/?wsdl") | |
sessionId = client.call(:login, :message => {:username=> 'demo', :apiKey=>'123456'}).body[:login_response][:login_return] | |
response = client.call(:call, :message => {:sessionId => sessionId, :resourcePath => 'catalog_category.tree'}) | |
category = response.body[:call_response][:call_return] | |
process(categories, category) | |
puts categories |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment