From 2887d1a49e985febac590204976a3e4ad8047cea Mon Sep 17 00:00:00 2001 From: Tyrone Tudehope Date: Mon, 25 Nov 2019 10:21:28 +0200 Subject: [PATCH] Serialize line item options which are array values to JSON; Resolves #12 --- CHANGELOG.md | 7 +++++++ composer.json | 2 +- src/services/Xml.php | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7774e4c..e08432d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 1.2.5 - 2019-11-25 + +### Updated + +- Line Item options which are an array or object are serialized to a JSON string. +- Order Line Item options are now limited to a maximum of 10 per line item. Limit set by ShipStation. + ## 1.2.4 - 2019-10-28 ### Updated diff --git a/composer.json b/composer.json index 3cb4418..78a560d 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "A Craft CMS plugin for integrating Craft Commerce with ShipStation", "homepage": "https://github.com/fostercommerce/shipstation-connect", "type": "craft-plugin", - "version": "1.2.4", + "version": "1.2.5", "keywords": ["craft","plugin","shipstation"], "license": "proprietary", "support": { diff --git a/src/services/Xml.php b/src/services/Xml.php index 6e889bb..e676d2b 100644 --- a/src/services/Xml.php +++ b/src/services/Xml.php @@ -13,6 +13,7 @@ class Xml extends Component { + const LINEITEM_OPTION_LIMIT = 10; const ORDER_FIELD_EVENT = 'orderFieldEvent'; public function shouldInclude($order) @@ -293,10 +294,23 @@ public function options(\SimpleXMLElement $xml, $options, $name='Options') { $options_xml = $xml->getName() == $name ? $xml : $xml->addChild($name); + + $index = 0; foreach ($options as $key => $value) { $option_xml = $options_xml->addChild('Option'); - $this->addChildWithCDATA($option_xml, 'Name', $key); + $option_xml->addChild('Name', $key); + + if (is_array($value) || !is_object($value)) { + $value = json_encode($value); + } + $this->addChildWithCDATA($option_xml, 'Value', $value); + + // ShipStation limits the number of options on any line item + $index++; + if ($index === self::LINEITEM_OPTION_LIMIT) { + break; + } } return $xml;