Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

product variants? #3

Closed
tcurdt opened this issue Nov 10, 2015 · 26 comments
Closed

product variants? #3

tcurdt opened this issue Nov 10, 2015 · 26 comments

Comments

@tcurdt
Copy link

tcurdt commented Nov 10, 2015

Does the API support a full import/export of products including variants and maybe even images yet?
Looks like that's the "product options" case, right?

@jirikolarik
Copy link
Member

Hey, yes, it does.

You can do something like:

combination = Prestashop::Mapper::Combination.new(
  id_lang: 1, id_product: 123, reference: 'unique-identifier')
combination.id_product_options = Prestashop::Mapper::ProductOption
  .create_from_hash([{name: 'Color', value: 'Red'}, {name: 'Size', value: 'M'], 1)
combination.id_images = Prestashop::Mapper::Image.new(
  resource: 'products', id_resource: 123, source: ['http://lorempixel.com/400/200/cats/']).upload
combination.create

@tcurdt
Copy link
Author

tcurdt commented Nov 14, 2015

Hm. I cannot connect

Prestashop::Client::Implementation.create '69CHK6FQISWIIAYYBIUDD7KJR1T85N1X', 'http://192.168.99.100:8080/api/'

I am getting

connection.rb:19:in `initialize': Your credentials are invalid (Prestashop::Api::InvalidCredentials)

I checked the endpoint

$ curl -i "http://192.168.99.100:8080/api/"
HTTP/1.1 401 Unauthorized
Date: Sat, 14 Nov 2015 09:38:00 GMT
Server: Apache/2.4.10 (Debian)
WWW-Authenticate: Basic realm="Welcome to PrestaShop Webservice, please enter the authentication key as the login. No password required."
Set-Cookie: PrestaShop-3cd618dc39c755671f96cf31d8d2bcaf=cpiCOo9HhxOTe7oKsbceTSbpuSpwuYTQ6grKwCi3U32lgat5Gb0h4Wa30keE4uDm6HluuehLU8NDlUhZJE9rpQJpoJx1jC6W%2Bf1w3QewQHw%3D000079; expires=Fri, 04-Dec-2015 09:38:00 GMT; Max-Age=1728000; path=/; httponly
Content-Length: 16
Content-Type: text/html; charset=utf-8

and the key is copy & pasted.

The shop is version 1.6.1.2. Any idea?

@jirikolarik
Copy link
Member

You can track communication between your app and store to see what's happening.

git pull
DEBUG=true rake console

@tcurdt
Copy link
Author

tcurdt commented Nov 14, 2015

Thanks, that was helpful.

Seems it's getting a 503. Odd. Maybe I need to try with curl first.

$ DEBUG=true bundle exec ruby importer.rb 
I, [2015-11-14T11:12:54.641460 #46330]  INFO -- : get http://192.168.99.100:8080/api/
D, [2015-11-14T11:12:54.641539 #46330] DEBUG -- request: Authorization: "Basic NjlDSEs2RlFJU1dJSUFZWUJJVUREN0tKUjFUODVOMVg6"
User-Agent: "Faraday v0.9.2"
I, [2015-11-14T11:12:54.710332 #46330]  INFO -- Status: 503
D, [2015-11-14T11:12:54.710472 #46330] DEBUG -- response: date: "Sat, 14 Nov 2015 10:12:54 GMT"
server: "Apache/2.4.10 (Debian)"
access-time: "1447495974"
x-powered-by: "PrestaShop Webservice"
psws-version: "1.6.1.2"
execution-time: "0.002"
set-cookie: "PrestaShop-3cd618dc39c755671f96cf31d8d2bcaf=cpiCOo9HhxOTe7oKsbceTaH3sDXSGKvhpYpqcGTBZwKlgat5Gb0h4Wa30keE4uDm6HluuehLU8NDlUhZJE9rpZ2vnBBUExTnaCSUPcup7Sc%3D000079; expires=Fri, 04-Dec-2015 10:12:54 GMT; Max-Age=1728000; path=/; httponly"
content-length: "241"
connection: "close"
content-type: "text/xml;charset=utf-8"

@tcurdt
Copy link
Author

tcurdt commented Nov 14, 2015

All good. The "save" to turn on the webservice got lost :)

@jirikolarik
Copy link
Member

Good, glad to hear that :)

@tcurdt
Copy link
Author

tcurdt commented Nov 14, 2015

almost there... when I run this

id_lang = Prestashop::Mapper::Language.find_by_iso_code('de')
id_supplier = Prestashop::Mapper::Supplier.new(name: 'apple').find_or_create
product = Prestashop::Mapper::Product.new(id_lang: id_lang, id_supplier: id_supplier, reference: 'apple-macbook')

I run into

.../lib/prestashop/mapper/models/product.rb:70:in `fetch': key not found: :name (KeyError)
from .../lib/prestashop/mapper/models/product.rb:70:in `initialize'
from importer.rb:8:in `new'

form the code I could not yet figure out whether I need to provide the name - but it seems like that doesn't solve it anyway. Any suggestions what's still missing?

@jirikolarik
Copy link
Member

Yes, you have to have a name, when you want to create product, I forgot.. https://github.com/werein/prestashop/blob/master/lib/prestashop/mapper/models/product.rb#L70

This should solve the problem..

Prestashop::Mapper::Product.new(id_lang: id_lang, id_supplier: id_supplier, reference: 'apple-macbook', name: 'MacBook')

I'll update readme later on.

@tcurdt
Copy link
Author

tcurdt commented Nov 14, 2015

Yes, I tried that but that gives:

models/product.rb:90:in `link_rewrite': undefined method `parameterize' for "MacBook Pro":String (NoMethodError)
from .../lib/prestashop/mapper/models/product.rb:133:in `hash'

Seems there is some rails leakage. Looks like I could provide my own link_rewrite though.
Wouldn't it be smarter to test name for parameterize, too?

@jirikolarik
Copy link
Member

You're right, it's extracted from rails. Yes, that would be great

@tcurdt
Copy link
Author

tcurdt commented Nov 14, 2015

Slooowly getting there. For now I've just added ActiveSupport. Now it's

/lib/prestashop/mapper/models/product.rb:102:in `available_now': undefined method `plain' for nil:NilClass (NoMethodError)
from .../lib/prestashop/mapper/models/product.rb:137:in `hash'

Not sure I get the usage of available_now, available_for_order, available_date and available_later yet.

But most importantly - where is the plain coming from?

@tcurdt
Copy link
Author

tcurdt commented Nov 15, 2015

Almost there. Now came across

/lib/prestashop/api/converter.rb:38:in `[]': no implicit conversion of Symbol into Integer (TypeError)

  def self.parse_error response
    parsed = parse(response)
    if parsed[:errors] and parsed[:errors][:error] and parsed[:errors][:error][:message] // line 38
      parsed[:errors][:error][:message]
    else
      raise ParserError
    end
  end

@tcurdt
Copy link
Author

tcurdt commented Nov 15, 2015

parsed looks like this:

{:errors=>
    {:error=>
        [
            {:message=>"Internal error. To see this error please display the PHP errors."},
            {:message=>"Internal error. To see this error please display the PHP errors."},
            {:message=>"Internal error. To see this error please display the PHP errors."}
        ]
    }
}

so apparently [:errors][:error] is an array.

@tcurdt
Copy link
Author

tcurdt commented Nov 15, 2015

To debug further I've set _PS_MODE_DEV_ to true - now the error is gone. You gotta love PHP :-/
It creates the products either way though.

Do you see a reason why this could create an error?

require 'prestashop'
require 'active_support'
require 'active_support/all'

Prestashop::Client::Implementation.create '69CHK6FQISWIIAYYBIUDD7KJR1T85N1X', 'http://192.168.99.100:8080/api/'

id_lang = Prestashop::Mapper::Language.find_by_iso_code('de')

id_supplier = Prestashop::Mapper::Supplier.new(
  name: 'apple'
).find_or_create

product = Prestashop::Mapper::Product.new(
  id_lang: id_lang,
  id_supplier: id_supplier,
  reference: 'apple-macbook-' + (0...16).map { (65 + rand(26)).chr }.join,
  name: 'MacBook Pro'
)

if product.find?
  # product.update price: '1299'

  puts "product found"

else

  product.quantity = 1,
  # product.depends_on_stock = 0
  product.weight = 10
  product.on_sale = 0
  product.available_now = ''
  product.available_later = ''
  product.description = 'My description'
  product.price = '1299'
  product.create

  puts "product created"
end

@tcurdt
Copy link
Author

tcurdt commented Nov 15, 2015

Even a more complex example works in dev mode - only though. This is just odd.

There are still a few open questions:

  • what is a reference opposed to an idand why are they limited to only 32 characters?
  • how to set the standard supplier of a product?
  • how to set supplier article number and price of the product options?

@jirikolarik
Copy link
Member

screen shot 2015-11-17 at 18 48 15

screen shot 2015-11-17 at 18 49 44

Plain is sanitising string..

I'll go through open questions.

  • Reference is unique identifier, it's used to look up a product when you don't know the id. It's limited to 32 characters due to Prestashop limitations.
  • What do you mean by standard supplier?
  • Not sure what supplier article number is.

You can set price of products option like that:

@combination = Prestashop::Mapper::Combination.new(
  id_lang: 'id-lang', id_product: 'product-id', reference: 'combination-reference')
if @combination.find?
  @combination.price = 123
  @combination.create
end

And I've no clue why it works only in DEV mode, do you have your own hosting or you use cloud solutions?

@tcurdt
Copy link
Author

tcurdt commented Nov 17, 2015

Seems like the reference isn't really needed. If I don't know the id I would search for the name. Still not sure I get the idea behind this - but anway.

combination.price = 123

Great! The naming is a little of (as it's a price change) but that's fine :)

The supplier article number is the number you use for ordering the article by the supplier. Setting this makes it easier to map your article number to the supplier article number.

I've attached a screenshot.

screen shot 2015-11-17 at 21 19 35

@tcurdt
Copy link
Author

tcurdt commented Nov 17, 2015

Another head scratcher is the tax. On the product I found id_tax_rules_group but I am not quite sure what a "group" represents here? Looking at the model I am also not quite sure how use it

https://github.com/werein/prestashop/blob/master/lib/prestashop/mapper/models/tax_rule_group.rb

@jirikolarik
Copy link
Member

It's how Prestashop handle VAT in countries, take a closer look how PS handle this.

screen shot 2015-11-17 at 22 07 39

Anyway, if you need to set a tax for product, I'd suggest to do something like this:

Prestashop::Mapper::Tax.get_by_country('cz')['21'] # => Tax ID for given country at this rate

In any case you can use low level API and make easier requests to the server..

@tcurdt
Copy link
Author

tcurdt commented Nov 17, 2015

Hm - but that's getting me a tax id not a "tax_rules_group" id.

Low level you mean like using https://github.com/werein/prestashop/blob/master/lib/prestashop/api/connection.rb

@jirikolarik
Copy link
Member

Actually you will get tax_rules_group, for the country based on rate you specified my mistake.

It will go thought TaxRules for required country and maps Tax rates to TaxRuleGroup.id..

For example you will get something like:

# For all `TaxRule` in country
{
  "21": 40, # => "Tax.rate": "TaxRulesGroup.id"
  "15": 41
}

Yes that's what I mean

@tcurdt
Copy link
Author

tcurdt commented Nov 28, 2015

Perfect! I will give that a try.

Only thing that I am still struggling with are the supplier references. The docs here are pretty slim here:

http://doc.prestashop.com/display/PS16/Web+service+reference

Did you just call the endpoints and try? Or how did you come up with the impl?

I bet it hides behind the product_suppliers endpoint.

@tcurdt
Copy link
Author

tcurdt commented Nov 28, 2015

Found the model and trying like this

product_supplier = Prestashop::Mapper::ProductSupplier.new(
  id_supplier: supplier,
  id_product: product.id,
  product_supplier_reference: '',
  product_supplier_price_te: ''
).find_or_create

but getting

/lib/prestashop/mapper/models/product_supplier.rb:11:in `initialize': undefined local variable or method `settings' for #<Prestashop::Mapper::ProductSupplier:0x007fc13d4eff10> (NameError)
from importer2.rb:171:in `new'
from importer2.rb:171:in `<main>'

where are those settings are supposed to come from?

https://github.com/werein/prestashop/blob/master/lib/prestashop/mapper/models/product_supplier.rb#L11

@jirikolarik
Copy link
Member

Yes, it's behind that endpoint. I called API with ?synopsis and that returned XML template. Here you can see available resources and actions

ProductSupplier is ok now, you should pass id_supplier as parameter, we were using some application wide settings. As I remember correctly ProductSupplier wasn't allowed to create/update in earlier versions of PS, not sure how it is now

@Haseeb717
Copy link

@tcurdt you find any solution of that?

@tcurdt
Copy link
Author

tcurdt commented Oct 13, 2016

@Haseeb717 sorry, it's been a while - I've moved on and cannot remember the details. I do remember it was painful though :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants