Skip to content

Instantly share code, notes, and snippets.

@skillio
Last active January 30, 2019 15:56
Show Gist options
  • Save skillio/83d8910e4248500a4289410fe06ba300 to your computer and use it in GitHub Desktop.
Save skillio/83d8910e4248500a4289410fe06ba300 to your computer and use it in GitHub Desktop.
Woocommerce Shipstation Integration tiny patch--xml output filter
/* Apologies for formatting, this was originally intended to be a ticket, but it errored. */
// Line 295 of Woocommerce Shipstation Integration 4.1.27 includes/api-requests/class-wc-shipstation-api-export.php is currently:
$orders_xml->appendChild( $order_xml );
// Proposed Change:
$orders_xml->appendChild( apply_filters( 'woocommerce_shipstation_export_order_xml', $order_xml) );
/*
Lots of ways this can be used, for example, pulling from a staging site and using an internal email so as not to notify customers when testing.
Or customizing "free shipping" output to be mapped, or any other fields, including the order items.
I've whipped up some code examples that could go in the documentation and show the utility of this:
*/
// changing customer email conditionally with simpleXML
public function export_order_xml( DOMElement $order_xml, DOMDocument $dom) {
if ( is_dev_site() ) {
$xml = simplexml_import_dom($order_xml);
$xml->Customer->BillTo->Email = 'shiptest@mydomain.com;
$order_xml = dom_import_simplexml($xml);
}
return $order_xml;
}
/*
Note that the $xml (DOMDocument) isn't used in this example, but is included in case devs aren't using simpleXML.
This lets them creating a new DOMDocument and receiving the "Wrong Document" Error" when appending. Example code for this (I'm inexpert with DOMDocument, bear with me):
*/
// Changing customer email conditionally with DOMDocument
public function export_order_xml( DOMElement $order_xml) {
if ( is_dev_site() ) {
$dom = $order_xml->ownerDocument;
$order_node = $dom->appendChild($order_xml);
$xp = new DOMXPath($dom);
$email = $xp->query('/Order/Customer/BillTo/Email')->item(0);
$new_email = $email->cloneNode();
$new_email->nodeValue = 'shiptest@mydomain.com';
$email->parentNode->replaceChild($new_email, $email);
}
return $order_node;
}
@skillio
Copy link
Author

skillio commented Oct 20, 2017

As an update to this, I don't believe the DOMDocument is necessary. Being inexpert with DOMDocument as I mentioned, I wasn't aware of the usage of ownerDocument. Example and hook have been updated to not include the document itself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment