Jul/090
3. Working with attributes
The application of attributes seems to be the most complicated part of writing ebay applications.
Unfortunately it is not possible to simply avoid it, because they are required even by such basic options as setting item condition to new and specifying the return policy.
The target of attributes is (as I it understand it) is to set properties that are not common and are not applicable to each category by default.
For example, defining an article condition as new makes no sense in such categories as Antiques or Specialty Services and therefore is not applicable.
Let us consider how to set the article condition to new.
Let us take as example the category Shoes / Girls/ Clothing, Shoes & Accessories -> Girls -> Shoes -> Sandals, Flip-Flops.
The category number is 57985.
1. Send the request GetCategory2CS to get information about this category;
2. Parse the received file GetCategory2CS.xml to find the AttributeSetID for this category.
For example, with XPath:
$dom->load(...path to the received file...); $xpath = new DOMXpath($dom); $xpath->registerNamespace('c', 'urn:ebay:apis:eBLBaseComponents'); $str='//c:MappedCategoryArray/c:Category/c:CharacteristicsSets/c:Name[.="Item Condition"]'; $elements = $xpath->query($str); $elt=$elements->item(0); $cs=$test->item(0)->parentNode; $attsetId=$cs->getElementsByTagName('AttributeSetID')->item(0); $attsetId_value=$attsetId->nodeValue;
2. If $attsetId exists, add the new node in your VerifyItem/Additem request:
$attsetarray=$dom->createElement(’AttributeSetArray’) ;
$item->appendChild($attsetarray);
Remember that we use this construction:
<AttributeSetArray>
<AttributeSet attributeSetID="int" attributeSetVersion="string">
<Attribute attributeID="int">
<Value>
<ValueID> int </ValueID>
<ValueLiteral> string </ValueLiteral>
</Value>
<!-- ... more Value nodes here ... -->
</Attribute>
<!-- ... more Attribute nodes here ... -->
</AttributeSet>
<!-- ... more AttributeSet nodes here ... -->
</AttributeSetArray>
(see http://developer.ebay.com/DevZone/XML/docs/Reference/ebay/AddItem.html)
- and therefore we should append here the sub-nodes AttributeSet, Attribute with the attribute attributeId, Value and ValueID.
$aset=$dom->createElement('AttributeSet') ; $attr=$dom->createAttribute('attributeSetID'); $aset_id=$attsetId_value; $txt=$dom->createTextNode($aset_id); $attr->appendChild($txt); $aset->appendChild($attr); //now append attribute element to attribute set element $att=$dom->createElement('Attribute') ; $attr=$dom->createAttribute('attributeID'); //now get the attributeID for the attribute 'Item Condition'
3. To get the attribute ID for Item Condition, request the GetAttributesCS with the parameter AttributeSetID=$aset_id.
Parse the received file GetAttributesCS.xml to find the attribite ID for Item Condition and Value ID for Condition New.
$s=simplexml_loaf_file(...path to the received file GetAttribute2CS...) $attdata=$s->AttributeData; $attdata_decoded= html_entity_decode($attdata); $dom=new DomDocument(); $dom->loadXML($attdata_decoded); $xpath = new DOMXpath($dom); $str='//Characteristics/CharacteristicsSet/CharacteristicsList/Initial/Attribute/Label[.="Item Condition'"]'; $elements = $xpath->query($str); $elt=$elements->item(0); $attribut=$elt->parentNode; $att_id=$attribut->getAttribute('id'); $str='ValueList/Value/Name[.="New"]'; $elements = $xpath->query($str,$attribut); $elt=$elements->item(0); $value=$elt->parentNode; $value_id=$value->getAttribute('id');
4. Now we have all the necessary values and can append them:
$txt=$dom->createTextNode($att_id); $attr->appendChild($txt); $att->appendChild($attr); $aset->appendChild($att); //append value element to attribute element $val=$dom->createElement('Value') ; $att->appendChild($val); //append ValueID element to value $val_id=$dom->createElement('ValueID',$value_id) ; $val->appendChild($val_id);
5. Append the ready AttributeSet to AttributeSetArray and append this to the Item node of the request body:
$attsetarray->appendChild($aset); $item->appendChild($attsetarray);
As you can see, this process is quite confusing. Maybe the known ebay API frameworks have a quick solution for attributes operating. I don’t know, as I haven’t tested it. A simple but not safe alternative is to receive the attributeset ID, attribute ID and value ID once and append them as hard-coded parameters. This will certainly work for some time, until they are changed.
