Magento 2 REST API Customer Custom Attribute - rest

The Magento 2 REST API Documentation explains a way to set custom_attributes on a customer when updating or creating it. http://devdocs.magento.com/swagger/index_20.html#/
Unfortunately I couldn't get this to work...
My POST and PUT JSON request data is :
{
"customer": {
"custom_attributes": [
{
"attribute_code": "firstname",
"value": "TEST"
}
],
"email": "someone#exaxmple.com",
"extension_attributes": [],
"firstname": "Someone",
"gender": null,
"lastname": "Else",
"middlename": null,
"taxvat": null,
"website_id": "1"
}
}
The Customer is created but the Firstname is not "TEST".
Is there anyone who had the same problem and fixed it? Please let me know how.

My best guess is that, since Firstname is an existing Out-Of-The-Box attribute - the OOTB attribute name-value mapping assignment will take precedence.
Could you try again, with a unique custom attribute name (i.e. something that doesn't clash with OOTB attribute names)
You will need to DEFINE a custom-customer-attribute before you can use the M2 API to perform operations on that custom-customer-attribute.
This StackExchange thread - https://magento.stackexchange.com/questions/88245/magento2-create-a-customer-custom-attribute - has additional information on how to go about setting up a custom-customer-attribute.

Related

Magento 2.4 API, POST product custom_attributes

I need to add/update products using the REST API. I cannot figure how to add a custom_attributes, knowing only the value/label of the attribute and not the ID (attribute of type 'dropdown' or 'selectable')
POST /rest/all/V1/products
{
"product": {
"sku": "D119",
"custom_attributes": [{
"attribute_code": "year",
"value": "2015"
}]
}
}
Upper code works fine if I just put in the ID in the value field. But a sourcing system wouldnt know the id's....
Side note: The M2 backend import function is capable of importing value/label of the attribute? How come?

How do I use the Loopback where filter correctly on REST-API

Hi there I seem to miss some information about Loopback where clause or syntax. My problem is, that I receive an empty array if I try to get a list of matching model instances, does no difference if in loopback api explorer or trhough http api request.
My "subject" model:
[
{
"user": "string",
"semester": "string",
"subject_name": "string",
"subject_relevance": 1,
"subject_details": 0,
"id": "string"
}
]
One Instance of Subjects:
{
"user": "59eef907cc199c1cf8f99296",
"semester": "59eef6f8ebf12e1a37ee898b",
"subject_name": "Mathematik",
"subject_relevance": "1",
"subject_details": "",
"id": "59eef96aebf12e1a37ee898f"
}
My Loopback API Explorer filter:
{"where" : {"semester" : "59eef6f8ebf12e1a37ee898b"}}
My http request:
http://localhost:3000/api/subjects?filter[where][semester]=59eef6f8ebf12e1a37ee898b
The result I receive:
[]
If I try to search for this:
{"where" : {"subject_name" : "Mathematik"}}
or in browser:
http://localhost:3000/api/subjects?filter[where][subject_name]=Mathematik
I reveive the instance correctly or as expected. So I went thrugh the documentation and I strugle to find some hints or solutions to my problem. Can someone help me out or suggest me something to get the right instance back? Do I miss something? Has it to do that these are ID-fields or what is it?
Thanks for any help in advance!!
There are 2 ways to get your records based on where clause
one is by using 'findOne' method
get request
http://localhost:3000/api/subjects/findOne?filter[where][semester]=59eef6f8ebf12e1a37ee898b
Other one is using normal get request
http://localhost:3000/api/subjects?filter={"where":{"semester":"59eef6f8ebf12e1a37ee898b"}}
I am using loopback 4 and I had faced the same issue.
Here is the issue in loopback git
Solution is the add this setting in your #model decorator
#model({settings: {"strict":false, "strictObjectIDCoercion": true}})

datatype of complextype entity is null when returning an array of complex types

We have created a complextype field "carriers" which is an array of Carrier objects. See below metadata
"dataProperties": [
{
"name": "carriers",
"complexTypeName":"Carrier#Test",
"isScalar":false
}]
The Carrier entity is defined as below:
{
"shortName": "Carrier",
"namespace": "Test",
"isComplexType": true,
"dataProperties": [
{
"name": "Testing",
"isScalar":true,
"dataType": "String"
}
]
}
We are trying to return an array of complextype in breeze from a REST service call. We get an error in breeze.debug.js in the method proto._updateTargetFromRaw. The error is because the datatype is null.
Any idea how to fix this issue?
I'm guessing the problem is in your "complexTypeName". You wrote "Carrier#Test" when I think you meant to write "Carrier:#Test". The ":#" combination separates the "short name" from the namespace; you omitted the colon.
Hope that's the explanation.

Show fields of mongolab record with angularjs

I wanna display the fields names of my records dynamically :
Example:
if i have this three records
{
"_id": {
"$oid": "blahblah"
},
"firstName": "Jon",
"lastName": "Doe",
"age" : "55"
}
{
"_id": {
"$oid": "blahblahblah"
},
"firstName": "Johnny",
"lastName": "Doedoe",
"weight" : "555lb"
}
What i want to display in my web page with angularjs is :
firstname : Jon
lastname : Doe
age : 55
firstname : Johnny
lastname : Doedoe
weight : 555lb
I know how to get specific values, for example to get the firstname i can do this : $scope.person.firstname (this is of course an example), but how can i get the label "firstname". How can i ask angularjs to get all the labels ?
Thanks
You'll have to write a query to your DB to get these records, depending on your server that query will look different. The below is a node version of the call
function getNames() {
db.collection.find().toArray(function(data, err) {
if (err)
//do something
else
res.send(data);
});
}
Now, from Angular, make a GET call to your server:
$http.get(url).success(function(data) {
$scope.names = data;
});
Now you have $scope.names set to your Array of data from the serv. Use an ng-repeat to display it all:
<div ng-repeat="name in names">
<span ng-repeat="(key, val) in name">{{key}}:{{val}}</span>
</div>
And that's it (well, basically, undoubtedly you'll have to tweak this).
Not sure if you're using Express and Jade with Angular, but alternatively if you don't want to make a separate http request (depends on how your app is configured) you could write an angular service that can be reused with any controller.
The purpose of the service would be to grab json and put it into your current scope so your controller can use it. Also you can inject this service dependency on every page (i.e. in every controller) and it'll load your documents for you.
Blog post and full repo here.

Convert Facebook names to id numbers

I've noticed some difference in Facebook profiles :
Some of them have the following string format at browser nav bar :
http://facebook.com/john.smith
and the others look like this :
http://www.facebook.com/profile.php?id=100455*
Can someone explain why there is a difference ?
And more important is how can I convert those john.smith like names to id numbers ?
These are alias urls that Facebook offers its users (and pages) - its basically a vanity thing, both the id and the alias url will work the same way.
You can translate the alias (or username) by doing a lookup for that user using the Facebook Graph API. Simply make a GET request to https://graph.facebook.com/John for example - this will serve the following response:
{
"id": "779695190",
"name": "John Chan",
"first_name": "John",
"last_name": "Chan",
"link": "http://www.facebook.com/John",
"username": "John",
"gender": "male",
"locale": "en_US",
"updated_time": "2011-12-27T05:01:06+0000",
"type": "user"
}
response.id is what your interested in.
Test it out: http://developers.facebook.com/tools/explorer?method=GET&path=john
You're not really want to converting the ids to names but getting id by username which is possible by issuing request to Graph API:
GET https:/graph.facebook.com/john.smith?fields=id
Here is a JQuery based solution, where callback is an arbitrary function:
$.get("https://graph.facebook.com/" + name + "?fields=id", function(response){
if(response.error){
callback(false)
}else{
callback(response.id);
}
});