Pregunta

I am using shopify webhook to update ms sql server when order fullfilled the belos are the code and xml file i get from shopify webhook but in order.xml file there are two key tags of "line-item" one in the "order" tag and the other is in "fullfillments" tag so i got duplicated value from order.xml file. How can i get only one value from "line-item" tag, please help me. Thank you so much in advance.

The php code:

$itemList2 = $dom->getElementsByTagName('line-item');


foreach($itemList2 as $item) {

$qty='';
$sku='';
$lineitemprice='';
$linetax='';

foreach($item->childNodes as $child) {

if ($child->localName == 'quantity') {
$qty = $child->textContent;
}
if ($child->localName == 'sku') {
$sku = $child->textContent;
}
if ($child->localName == 'price') {
$lineitemprice = $child->textContent;
}
if ($child->localName == 'linetax') {
$linetax = $child->textContent;
}
}   

and the order.xml file from shopify webhook

<?xml version="1.0" encoding="UTF-8"?>
<order>

<line-items type="array">
<line-item>
<fulfillment-service>manual</fulfillment-service><fulfillment-status>fulfilled</fulfillment-status><gift-card type="boolean">false</gift-card><grams type="integer">0</grams><id type="integer">470618337</id><price type="decimal">0.99</price><product-id type="integer">103461847</product-id><quantity type="integer">5</quantity><requires-shipping type="boolean">true</requires-shipping><sku>695019010135</sku><taxable type="boolean">true</taxable><title>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</title><variant-id type="integer">238666967</variant-id><variant-title></variant-title><vendor>Beauty Town</vendor><name>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</name><variant-inventory-management>shopify</variant-inventory-management><properties type="array"></properties><product-exists type="boolean">true</product-exists><fulfillable-quantity type="integer">0</fulfillable-quantity><tax-lines type="array"/>
</line-item>
</line-items>


<fulfillments type="array">
<fulfillment>
<created-at type="dateTime">2014-06-03T12:15:27-04:00</created-at><id type="integer">184397429</id><order-id type="integer">265290017</order-id><service>manual</service><status>success</status><tracking-company>Other</tracking-company><updated-at type="dateTime">2014-06-03T12:15:27-04:00</updated-at><tracking-number>123456789</tracking-number><tracking-numbers type="array"><tracking-number>123456789</tracking-number></tracking-numbers><tracking-url>http://www.google.com/search?q=123456789</tracking-url><tracking-urls type="array"><tracking-url>http://www.google.com/search?q=123456789</tracking-url></tracking-urls><receipt></receipt><line-items type="array">

<line-item>
<fulfillment-service>manual</fulfillment-service><fulfillment-status>fulfilled</fulfillment-status><gift-card type="boolean">false</gift-card><grams type="integer">0</grams><id type="integer">470618337</id><price type="decimal">0.99</price><product-id type="integer">103461847</product-id><quantity type="integer">5</quantity><requires-shipping type="boolean">true</requires-shipping><sku>695019010135</sku><taxable type="boolean">true</taxable><title>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</title><variant-id type="integer">238666967</variant-id><variant-title></variant-title><vendor>Beauty Town</vendor><name>Beauty Town 250Pcs Rubber Band (Light Brown) - 01013</name><variant-inventory-management>shopify</variant-inventory-management><properties type="array"></properties><product-exists type="boolean">true</product-exists><fulfillable-quantity type="integer">0</fulfillable-quantity><tax-lines type="array"/></line-item></line-items>

</fulfillment>
</fulfillments>
</order>
¿Fue útil?

Solución

You could use XPath, for example:

$xpath = new DOMXpath($dom);
$itemList2 = $xpath->query("/order/line-items/line-item"); // absolute path

Using a relative path like this should work too:

$itemList2 = $xpath->query("line-items/*");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top