From cb9fc55bdb8e9d0d9f70d60866e747d9de378cdb Mon Sep 17 00:00:00 2001 From: Tyrone Tudehope Date: Fri, 24 May 2024 15:51:40 +0200 Subject: [PATCH] feat: Add ImageUrl to line items --- CHANGELOG.md | 7 ++++++ composer.json | 2 +- src/models/Settings.php | 1 + src/services/Xml.php | 25 +++++++++++++++++++++- src/templates/settings/index.twig | 19 ++++++++++------ src/web/twig/filters/IsFieldTypeFilter.php | 21 ++++++++++++------ 6 files changed, 60 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb37c7..7f8ce2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2.1.0 - 2024-05-24 + +### Added + +- Support for a single asset field to fetch a product image for order line items during order export. + ## 2.0.4 - 2024-03-22 - Update plugin branding @@ -15,6 +21,7 @@ ### Fixed - Fixed issue where custom shipping methods were not showing up in generated XML ([#54](https://github.com/FosterCommerce/shipstation-connect/pull/54)) +- ## 2.0.1 - 2022-07-19 ### Added diff --git a/composer.json b/composer.json index 670f86e..05f8f04 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": "2.0.4", + "version": "2.1.0", "keywords": ["craft","plugin","shipstation"], "license": "proprietary", "support": { diff --git a/src/models/Settings.php b/src/models/Settings.php index 0d0d346..ce5eb89 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -11,6 +11,7 @@ class Settings extends Model public $shipstationPassword = ''; public $ordersPageSize = 25; public $orderIdPrefix = ''; + public $productImagesHandle = null; public $shippedStatusHandle = 'shipped'; public $matrixFieldHandle = 'shippingInfo'; public $blockTypeHandle = 'shippingInfo'; diff --git a/src/services/Xml.php b/src/services/Xml.php index 68141fe..d58ccbc 100644 --- a/src/services/Xml.php +++ b/src/services/Xml.php @@ -1,6 +1,7 @@ salePrice, 2); }, 'cdata' => false, + ], + 'ImageUrl' => [ + 'callback' => function ($item) { + $productImagesHandle = Plugin::getInstance()->getSettings()->productImagesHandle; + $purchasable = $item->getPurchasable(); + if ($productImagesHandle !== null && $purchasable !== null) { + $assetQuery = $purchasable->{$productImagesHandle}; + if ($assetQuery === null) { + // Fallback to the product if the variant does not have an asset + $assetQuery = $purchasable->product->{$productImagesHandle}; + } + if ($assetQuery !== null) { + $asset = $assetQuery->one(); + if ($asset !== null) { + return UrlHelper::siteUrl($asset->getUrl()); + } + } + } + + return null; + }, + 'cdata' => false, ] ]; $this->mapCraftModel($item_xml, $item_mapping, $item); @@ -483,7 +506,7 @@ public function customOrderFields(\SimpleXMLElement $order_xml, Order $order) [OrderFieldEvent::FIELD_GIFT_MESSAGE, 1000], ]; - foreach ($customFields as list($fieldName, $charLimit)) { + foreach ($customFields as [$fieldName, $charLimit]) { $orderFieldEvent = new OrderFieldEvent([ 'field' => $fieldName, 'order' => $order, diff --git a/src/templates/settings/index.twig b/src/templates/settings/index.twig index 071b0cc..acef9c2 100644 --- a/src/templates/settings/index.twig +++ b/src/templates/settings/index.twig @@ -38,7 +38,7 @@ Other ShipStation statuses can be left as is, as Commerce doesn't have any corresponding statuses. See below for the statuses currently enabled.

- {% set fields = craft.app.fields.allFields %} + {% set fields = craft.app.fields.allFields %} {% set storeFields = fields|filter(f => is_dropdown(f))|map(f => { label: f.name, value: f.handle }) %} {% set storeFields = [{ label: 'Default', value: '' }]|merge(storeFields) %} @@ -83,7 +83,7 @@ {% endif %} {% if isUsingCraftAuth %}

- This site is using Craft's enableBasicHttpAuth feature. A dedicated user + This site is using Craft's enableBasicHttpAuth feature. A dedicated user should be added to access ShipStation Connect.

{% endif %} @@ -148,6 +148,14 @@ + {% set assetFields = [{label: 'None', value: null}]|merge(fields|filter(f => is_asset(f))|map(f => { label: f.name, value: f.handle })) %} + {{ forms.selectField({ + label: "Product Images Field Handle"|t('shipstationconnect'), + name: 'settings[productImagesHandle]', + value: settings.productImagesHandle, + options: assetFields + }) }} +

Shipped Order Status

{{ forms.selectField({ @@ -159,10 +167,9 @@

Shipping Info Matrix Field

- {% set fields = craft.app.fields.allFields %} - {% set fields = fields|filter(f => is_matrix(f))|map(f => { label: f.name, value: f.handle }) %} + {% set matrixFields = fields|filter(f => is_matrix(f))|map(f => { label: f.name, value: f.handle }) %} - {% if fields|length == 0 or settings.matrixFieldHandle is null or settings.matrixFieldHandle|length == 0 %} + {% if matrixFields|length == 0 or settings.matrixFieldHandle is null or settings.matrixFieldHandle|length == 0 %}
A matrix field with a block type and the following fields is required: