Git Product home page Git Product logo

Comments (26)

jirikolarik avatar jirikolarik commented on April 29, 2024

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

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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?

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

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

git pull
DEBUG=true rake console

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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"

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

Good, glad to hear that :)

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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?

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

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.

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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?

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

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

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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?

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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.

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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?

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

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?

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

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..

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

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

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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.

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

jirikolarik avatar jirikolarik commented on April 29, 2024

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

from prestashop.

Haseeb717 avatar Haseeb717 commented on April 29, 2024

@tcurdt you find any solution of that?

from prestashop.

tcurdt avatar tcurdt commented on April 29, 2024

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

from prestashop.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.