I want to add product to different website in magento 2 using rest api. I am able to add product to the store but I am not getting any information as how to add product to different websites inside magento.
Magento – Add product to different website using rest API in magento 2
catalogmagento2restwebsites
Related Solutions
Not sure if this is the problem for you but I had a similar issue the other day.
My problem was actually 2 problems.
- The authorization url for admin actions should be
ROOT/admin/oauth_authorize
notROOT/oauth/authorize
like it is for guest or customer requests. - An admin role should be created in the backend and at least an user must be assigned to it. For that go to
System->Web Services->REST Roles
click onAdd Admin Role
.Set the permissions and after the page reloads you should see a tab calledRole Users
. Click onReset Search
and add at least one admin to the role. Save and try again.
I hope that at least one of the points above solves your problem
Example script that create downloadable product
<?php
$curl = curl_init();
$URL = "http://magento.dev";
curl_setopt_array($curl, array(
CURLOPT_URL => $URL . "/rest/V1/integration/admin/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"username\":\"admin\", \"password\":\"123123q\"}",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json",
"postman-token: 654c3084-0e0a-b3a1-043f-0960e695e520"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
die();
} else {
$key = $response;
}
$data = [
"product"=> [
"sku"=> "DownloadableProduct_18sdsd5",
"name"=> "DownloadableProduct_185",
"attribute_set_id"=> 4,
"price"=> "1",
"status"=> 1,
"visibility"=> 4,
"type_id"=> "downloadable",
"extension_attributes"=> [
"stock_item"=> [
"manage_stock"=> 1,
"is_in_stock"=> 1,
"qty"=> "10"
],
"downloadable_product_samples"=> [[
"title"=> "sample1185869143",
"sort_order"=> "0",
"sample_type"=> "url",
"sample_url"=> "http://example.com"
]],
"downloadable_product_links"=> [[
"title"=> "link-1-185862143",
"sort_order"=> "1",
"is_shareable"=> 0,
"price"=> 2.43,
"number_of_downloads"=> "2",
"link_type"=> "url",
"link_url"=> "http://example.com",
"sample_type"=> "url",
"sample_url"=> "http://example.com"
]]
],
"custom_attributes"=> [[
"attribute_code"=> "tax_class_id",
"value"=> 2
], [
"attribute_code"=> "quantity_and_stock_status",
"value"=> [
"qty"=> "10",
"is_in_stock"=> 1
]
], [
"attribute_code"=> "is_virtual",
"value"=> 1
], [
"attribute_code"=> "url_key",
"value"=> "downloadableproduct-185892143"
], [
"attribute_code"=> "links_title",
"value"=> "Links title 185862143"
], [
"attribute_code"=> "links_purchased_separately",
"value"=> 1
], [
"attribute_code"=> "samples_title",
"value"=> "Samples185692143"
], [
"attribute_code"=> "links_exist",
"value"=> 1
]]
]
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $URL . "/rest/admin/V1/products/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"content-type: application/json",
"authorization: Bearer " . $key,
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Best Answer
As far as I know, this cannot be done in the initial adding of the product through the API (it will always be added just to your default website), but you can then use the API to add it to other websites.
Documentation on catalogueProductWebsiteLinkRepositoryV1
Send a
POST
request to/V1/products/{sku}/websites
(changing{sku}
for the item's sku).For the body of the request, use the format:
e.g.