使用 shopify webhook 行项目的 XML 解析器 [英] XML parser using shopify webhooked line-items

查看:40
本文介绍了使用 shopify webhook 行项目的 XML 解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 shopify webhook 在订单更新时更新我的​​ sql server 的 'qty' 字段,下面是我的 php 代码

I am using shopify webhook to update my sql server's 'qty' field when order updated, below is my php code

?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);
}

以下是我从 shopify webhook 获取的 xml 文件

and below is xml file i am getting from shopify webhook

<?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>yeongju_l@yahoo.com</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>yeongju_l@yahoo.com</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>

就像你看到的,由于重复的行项目,我有两个 sku 和数量,例如,当客户订购一个产品名称 - 在这种情况下测试我在我的 sql server sku 测试字段中得到了-2"数量更新,但是当我在创建订单时使用 webhook 事件时,我的意思是我只看到一个订单项,但所有其他情况(订单更新时、订单付款时、订单完成时......)显示重复的订单项,即使只有一个项目订购我想我解析我的 XML 很糟糕任何能教我正确代码从第一个行项目节点中提取行项目"的人,我将非常感激!谢谢..再次

like you see i got two sku and qty because of duplicated line-item so for example when customer order one "product name - test in this case i got "-2" quantity update in my sql server sku test field , but when i am using webhook event when order creation it worked i mean i see only one line item but all the other cases(when order updated, when order payment, when order fullfillment..) show me duplicated line item even there is only one item ordered i think i am parsing my XML badly anyone who can teach me correct code to pull 'line items' from the first line-items node i will really appreciate it! Thanks..again

推荐答案

您正在执行大量冗余步骤来解析您的 XML.您不需要在处理数据之前将数据保存到文件中,并且您有一个未使用的对 SimpleXML 的杂散调用.你只需要这个:

You're doing a lot of redundant steps to parse your XML there. You don't need to save the data into a file before processing it, and you have a stray call to SimpleXML that you're not using. All you need is this:

$xmlString = file_get_contents('php://input');
$dom = new DomDocument();
$dom->loadXML($xmlString);

之后,您的解析逻辑看起来不错但是您只运行了一个 SQL 查询,其中包含一个 SKU.

After that, your parsing logic looks fine but you are only ever running one SQL query, with one SKU in it.

在你的 foreach 循环中,你定义了变量 $sku$qty,但是你不会在里面对它们做任何事情环形.因此,下一次循环时,您将覆盖它们的值,而旧值将一无所知.

Inside your foreach loop, you define the variables $sku and $qty, but you don't do anything with them inside the loop. So next time round the loop, you will over-write their values, and nothing will ever know about the old values.

有几种方法可以做到这一点:

There are few ways to do this:

  • 在循环内运行 SQL(效率不高)
  • 建立一个 SKU 和数量的数组 ($sku[] = ...; $qty[] = ...;),然后从这些数组中建立你的 SQL
  • 稍微整洁一点,使用 SKU 数量对作为嵌套数组构建单个数组 ($sku_list[] = array('sku' => ..., 'qty' => ...))
  • 在循环中逐步构建 SQL 字符串 ($sql .= '...') 并在最后执行一次
  • run SQL inside the loop (not very efficient)
  • build up an array of SKUs and quantities ($sku[] = ...; $qty[] = ...;) and then build your SQL from these arrays
  • slightly tidier, build a single array with the SKU-quantity pairs as nested arrays ($sku_list[] = array('sku' => ..., 'qty' => ...))
  • build your SQL string progressively inside the loop ($sql .= '...') and execute it once at the end

这篇关于使用 shopify webhook 行项目的 XML 解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆