Post Article, Category and Image using REST Api in Shopware

In last post, I show you how to retrieve atricles and categories using REST Api. This time i am here with next step.

I will show you that how can you post data using REST Api.

Post article, category and image using REST Api in shopware
For Article:

Follow these steps:

1. Create a page "postarticle.php" and put following code.

<?php
include 'config.php';     // Know more about config.php in last post. Must Read 
            //create an array $s2parray here
$client->post('articles', $s2parray);
?>
2. Run this code.

Some questions created at this point:

1. What is "$s2parray"?

Ans.  Actually $s2parray is an array carrying all information to post into database.Such as article's name, title, description, category, ordernumber, price etc.

2. What would be the structure of this array?

Check following example:


$s2parray = array(
    'name' => "NAME",
'description' => "DESCRIPTION",
'descriptionLong' => "DESCRIPTION LONG",
    'active' => "TRUE",
    'tax' => "TAX",
    'supplier' => "SUPPLIER NAME",
    'categories' => array(
        array('id' => "CATEGORY ID"),
     
    ),
    'mainDetail' => array(
        'number' => "YOUR ORDER NUMBER",
        'prices' => array(
            array(
                'customerGroupKey' => 'EK',
                'price' => "PRICE",
            ),
        )
    ),
);

 3. Is this fix stucture for array to post article?

Ans: No, You can extend this according to your requirement.

4. How can i extend this array?

Ans: You can do some trick to make a full array.First post an test article using above array structure.
After that you retrieve this article by article id. You will get a JSON output like below:


{"data":
{
"id":307,
"mainDetailId":862,
"supplierId":24,
"taxId":1,
"priceGroupId":null,
"filterGroupId":null,
"configuratorSetId":null,
"name":"Zaunfeldpfosten Classic 42 x 1100x 1030 mm",
"description":"Zaunfeldpfosten Classic 42 x 1100x 1030 mm",
"descriptionLong":"","added":"2014-01-16T00:00:00+0100",
"active":true,
"pseudoSales":0,
"highlight":false,
"keywords":"1",
"changed":"2014-01-16T17:20:23+0100",
"priceGroupActive":false,
"lastStock":false,
"crossBundleLook":0,
"notification":false,
"template":"","mode":0,
"availableFrom":null,
"availableTo":null,
"configuratorSet":null,
"mainDetail":
{
"id":862,
"articleId":307,
"unitId":null,
"number":"BZSEZ71X1I",
"supplierNumber":null,
"kind":1,
"additionalText":null,
"active":0,
"inStock":null,
"stockMin":null,
"weight":null,
"width":null,
"len":null,
"height":null,
"ean":null,
"position":0,
"minPurchase":null,
"purchaseSteps":null,
"maxPurchase":null,
"purchaseUnit":null,
"referenceUnit":null,
"packUnit":null,
"shippingFree":false,
"releaseDate":null,
"shippingTime":null,
"prices":[
{
"id":1058,
"articleId":307,
"articleDetailsId":862,
"customerGroupKey":"EK",
"from":1,
"to":"beliebig",
"price":58.739495798319,
"pseudoPrice":0,
"basePrice":0,
"percent":0
}
],
"attribute":
{
"id":896,
"articleId":307,
"articleDetailId":862,
"attr1":null,
"attr2":null,
"attr3":null,
"attr4":null,
"attr5":null,
"attr6":null,
"attr7":null,
"attr8":null,
"attr9":null,
"attr10":null,
"attr11":null,
"attr12":null,
"attr13":null,
"attr14":null,
"attr15":null,
"attr16":null,
"attr17":null,
"attr18":null,
"attr19":null,
"attr20":null
}
},
"tax":
{
"id":1,
"tax":"19.00",
"name":"19%"
},
"categories":
{
"1":
{
"id":1,
"name":"Root"
}
},
"links":[],
"images":[],
"downloads":[],
"related":[],
"propertyValues":[],
"similar":[],
"customerGroups":[],
"supplier":
{
"id":24,
"name":"GAH Alberts",
"image":"",
"link":"",
"description":null
},
"details":[],
"propertyGroup":null
},
"success":true
}

What are you thinking? Your JSON output is not like this. Please copy your JSON data and paste into any editor like Notepad++ or Dreamweaver. Select language as Javascript.
Now rearrange manually.
Wow, you get a similar JSON.
Next work to convert this into your appropriate array. OK, I am helping you little bit. I am replacing some codes from this json and output is below.


$s2parray = array(


"mainDetailId":862,
"name":"Zaunfeldpfosten Classic 42 x 1100x 1030 mm",
"description":"Zaunfeldpfosten Classic 42 x 1100x 1030 mm",
"descriptionLong":"",
"active":true,
"pseudoSales":0,
"highlight":false,
"keywords":"1",

"priceGroupActive":false,
"lastStock":false,
"crossBundleLook":0,
"notification":false,
"template":"","mode":0,
"availableFrom":null,
"availableTo":null,
"configuratorSet":null,
"mainDetail":
{

"unitId":null,
"number":"BZSEZ71X1I",
"supplierNumber":null,
"kind":1,
"additionalText":null,
"active":0,
"inStock":null,
"stockMin":null,
"weight":null,
"width":null,
"len":null,
"height":null,
"ean":null,
"position":0,
"minPurchase":null,
"purchaseSteps":null,
"maxPurchase":null,
"purchaseUnit":null,
"referenceUnit":null,
"packUnit":null,
"shippingFree":false,
"releaseDate":null,
"shippingTime":null,
"prices":[
{

"customerGroupKey":"EK",
"from":1,
"to":"beliebig",
"price":58.739495798319,
"pseudoPrice":0,
"basePrice":0,
"percent":0
}
],
"attribute":
{

"attr1":null,
"attr2":null,
"attr3":null,
"attr4":null,
"attr5":null,
"attr6":null,
"attr7":null,
"attr8":null,
"attr9":null,
"attr10":null,
"attr11":null,
"attr12":null,
"attr13":null,
"attr14":null,
"attr15":null,
"attr16":null,
"attr17":null,
"attr18":null,
"attr19":null,
"attr20":null
}
},
"tax":
{

"tax":"19.00",
"name":"19%"
},
"categories":
{
"1":
{
"id":1,
"name":"Root"
}
},
"links":[],
"images":[],
"downloads":[],
"related":[],
"propertyValues":[],
"similar":[],
"customerGroups":[],
"supplier":
{

"name":"GAH Alberts",
"image":"",
"link":"",
"description":null
},
"details":[],
"propertyGroup":null
);


I did my work now your turn. Replace all colon (:) with => . I have removed all id attributes except unitid and category id.

Now this is your complete code to post article. If you face any problem. Don't hesitate to comment.



1 Comments
Disqus
Fb Comments
Comments :

1 comment: