2012-09-17 39 views
0

我使用shopify網絡掛接時,爲了更新來更新我的SQL Server的「數量」字段更新網絡掛接Shopify行項目, 下面是我的PHP代碼使用時,爲了

?php 
$xmlData = fopen('php://input' , 'rb'); 
while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); } 
fclose($xmlData); 
$xml = new SimplexmlElement($xmlString); 


file_put_contents('orders/order' . '.xml', $xmlString); 



$dom = new DomDocument(); 

$dom->load('orders/order.xml'); 

$itemList = $dom->getElementsByTagName('line-item'); 
foreach($itemList as $item) { 
    $sku=''; 
    $qty=''; 
    foreach($item->childNodes as $child) { 
    if ($child->localName == 'sku') { 
     $sku = $child->textContent; 
    } 
    if ($child->localName == 'quantity') { 
     $qty = $child->textContent; 
    } 
    } 
    mysql_connect ("?????????????????????????"); 
mysql_select_db("??????????"); 

$query = "UPDATE xcart_categories SET product_count = product_count - $qty WHERE description='$sku';"; 

mysql_query($query); 
} 

及以下的xml文件我得到從shopify網絡掛接

<?xml version="1.0" encoding="UTF-8"?> 
<order> 
    <buyer-accepts-marketing type="boolean">true</buyer-accepts-marketing> 
    <closed-at type="datetime" nil="true"></closed-at> 
    <currency>USD</currency> 
    <email>[email protected]</email> 
    <financial-status>pending</financial-status> 
    <fulfillment-status>fulfilled</fulfillment-status> 
    <gateway>Local Pick-Up</gateway> 
    <id type="integer">140303247</id> 
    <name>#1012</name> 
    <note></note> 
    <number type="integer">12</number> 
    <subtotal-price type="decimal">0.2</subtotal-price> 
    <taxes-included type="boolean">false</taxes-included> 
    <total-discounts type="decimal">0.0</total-discounts> 
    <total-line-items-price type="decimal">0.2</total-line-items-price> 
    <total-price type="decimal">0.2</total-price> 
    <total-price-usd type="decimal">0.2</total-price-usd> 
    <total-tax type="decimal">0.0</total-tax> 
    <total-weight type="integer">0</total-weight> 
    <updated-at type="datetime">2012-09-16T21:20:07-04:00</updated-at> 
    <created-at type="datetime">2012-09-16T21:08:30-04:00</created-at> 
    <token>dcf523d93c68159c15a7c8d1fabbee07</token> 
    <landing-site>/products/test</landing-site> 
    <referring-site></referring-site> 
    <cancelled-at type="datetime" nil="true"></cancelled-at> 
    <cancel-reason nil="true"></cancel-reason> 
    <cart-token>a9a7bc5d8103f6a3bb45e827f0cb8928</cart-token> 
    <browser-ip nil="true"></browser-ip> 
    <landing-site-ref nil="true"></landing-site-ref> 
    <order-number type="integer">1012</order-number> 
    <discount-codes type="array"/> 
    <note-attributes type="array"> 
    </note-attributes> 
    <processing-method>manual</processing-method> 
    <line-items type="array"> 
    <line-item> 
     <id type="integer">228531213</id> 
     <requires-shipping type="boolean">false</requires-shipping> 
     <fulfillment-service>manual</fulfillment-service> 
     <grams type="integer">0</grams> 
     <price type="decimal">0.2</price> 
     <quantity type="integer">1</quantity> 
     <sku>1234567</sku> 
     <title>test</title> 
     <product-id type="integer">104663831</product-id> 
     <variant-id type="integer">240660979</variant-id> 
     <vendor>5 Second</vendor> 
     <variant-title nil="true"></variant-title> 
     <fulfillment-status>fulfilled</fulfillment-status> 
     <name>test</name> 
     <variant-inventory-management></variant-inventory-management> 
     <properties type="array"> 
     </properties> 
    </line-item> 
    </line-items> 
    <shipping-lines type="array"/> 
    <tax-lines type="array"> 
    <tax-line> 
     <title>NY State Tax</title> 
     <price type="decimal">0.0</price> 
     <rate type="float">0.04</rate> 
    </tax-line> 
    <tax-line> 
     <title>Queens County Tax</title> 
     <price type="decimal">0.0</price> 
     <rate type="float">0.04875</rate> 
    </tax-line> 
    </tax-lines> 
    <billing-address> 
    <first-name>Yeongju</first-name> 
    <last-name>Lee</last-name> 
    <address1>14809 northern blvd</address1> 
    <address2></address2> 
    <city>Flushing</city> 
    <company></company> 
    <country>United States</country> 
    <phone></phone> 
    <province>New York</province> 
    <zip>11354</zip> 
    <latitude type="decimal">40.76529</latitude> 
    <longitude type="decimal">-73.81831</longitude> 
    <name>Yeongju Lee</name> 
    <country-code>US</country-code> 
    <province-code>NY</province-code> 
    </billing-address> 
    <fulfillments type="array"> 
    <fulfillment> 
     <id type="integer">67712419</id> 
     <order-id type="integer">140303247</order-id> 
     <created-at type="datetime">2012-09-16T21:20:07-04:00</created-at> 
     <updated-at type="datetime">2012-09-16T21:20:07-04:00</updated-at> 
     <tracking-number nil="true"></tracking-number> 
     <tracking-company nil="true"></tracking-company> 
     <status>success</status> 
     <service>manual</service> 
     <tracking-url>http://www.google.com/search?q=</tracking-url> 
     <receipt> 
     </receipt> 
     <line-items type="array"> 
     <line-item> 
      <id type="integer">228531213</id> 
      <requires-shipping type="boolean">false</requires-shipping> 
      <fulfillment-service>manual</fulfillment-service> 
      <grams type="integer">0</grams> 
      <price type="decimal">0.2</price> 
      <quantity type="integer">1</quantity> 
      <sku>1234567</sku> 
      <title>test</title> 
      <product-id type="integer">104663831</product-id> 
      <variant-id type="integer">240660979</variant-id> 
      <vendor>5 Second</vendor> 
      <variant-title nil="true"></variant-title> 
      <fulfillment-status>fulfilled</fulfillment-status> 
      <name>test</name> 
      <variant-inventory-management></variant-inventory-management> 
      <properties type="array"> 
      </properties> 
     </line-item> 
     </line-items> 
    </fulfillment> 
    </fulfillments> 
    <customer> 
    <id type="integer">96489088</id> 
    <email>[email protected]</email> 
    <accepts-marketing type="boolean">true</accepts-marketing> 
    <first-name>Yeongju</first-name> 
    <last-name>Lee</last-name> 
    <orders-count type="integer">12</orders-count> 
    <total-spent type="decimal">16.26</total-spent> 
    <note nil="true"></note> 
    <created-at type="datetime">2012-08-17T11:31:50-04:00</created-at> 
    <updated-at type="datetime">2012-09-16T21:20:07-04:00</updated-at> 
    <state>enabled</state> 
    <last-order-id type="integer">140303509</last-order-id> 
    <tags>C</tags> 
    <last-order-name>#1013</last-order-name> 
    </customer> 
</order> 

就像你看到我有兩個SKU因爲重複的行項目的數量,以便例如當客戶爲了一個「產品名稱 - 測試在這種情況下,我得到了‘在-2’量更新我的SQL服務器sku測試字段,但是當我使用webhook事件時訂單創建它的工作我的意思是我看到只有一個行項目,但所有其他情況下(當訂單更新,當訂單付款,當訂單fullfillment ..)顯示我重複行項目,即使只有一個項目訂購

任何人有想法解決這個問題? 非常感謝你提前給我們傢伙......

回答

1

你正在嚴重地解析你的XML。您會注意到其中一個line-items標記位於響應的根目錄中,而另一個嵌套在fulfillments節點下。前者是訂單的訂單項的完整列表,後者是針對訂單進行的單項履行的記錄(可能有幾個)。

我會找到一個XML解析器並將它放在數據上,以便您可以以結構化的方式訪問它,並只從第一個節點中提取行項目。

+0

謝謝大衛你能教我正確的代碼來從第一個行項目節點拉'行項目'我真的很感激它!謝謝..again .. – Max

+0

在PHP中解析XML並不是我所熟悉的,但是快速的Google搜索顯示[SimpleXML](http://ca2.php.net/manual/en/simplexml.examples- basic.php)可能是一個不錯的選擇。祝你好運! –

相關問題