Skip to content

Commit

Permalink
feat: Add ImageUrl to line items
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynotsolucky committed May 24, 2024
1 parent 2581dd2 commit cb9fc55
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
25 changes: 24 additions & 1 deletion src/services/Xml.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace fostercommerce\shipstationconnect\services;

use craft\helpers\UrlHelper;
use fostercommerce\shipstationconnect\Plugin;
use fostercommerce\shipstationconnect\events\OrderFieldEvent;
use craft\commerce\Plugin as CommercePlugin;
Expand Down Expand Up @@ -220,6 +221,28 @@ public function item(\SimpleXMLElement $xml, LineItem $item, $name='Item'): \Sim
return round($item->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);
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 13 additions & 6 deletions src/templates/settings/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
</p>
</div>
{% 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) %}

Expand Down Expand Up @@ -83,7 +83,7 @@
{% endif %}
{% if isUsingCraftAuth %}
<p class="error">
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.
</p>
{% endif %}
Expand Down Expand Up @@ -148,6 +148,14 @@
</ul>
</div>

{% 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
}) }}

<h2>Shipped Order Status</h2>

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

<h2>Shipping Info Matrix Field</h2>

{% 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 %}
<div class="error">
A matrix field with a block type and the following fields is required:
<ul style="list-style:inherit">
Expand All @@ -177,7 +184,7 @@
label: "Matrix Field Handle"|t('shipstationconnect'),
name: 'settings[matrixFieldHandle]',
value: settings.matrixFieldHandle,
options: fields
options: matrixFields
}) }}

{{ forms.textField({
Expand Down
21 changes: 14 additions & 7 deletions src/web/twig/filters/IsFieldTypeFilter.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace fostercommerce\shipstationconnect\web\twig\filters;

use Craft;
use craft\base\Field;
use craft\fields\Assets;
use craft\fields\Matrix;
use craft\fields\Dropdown;
use Twig\Extension\AbstractExtension;
Expand All @@ -19,27 +19,34 @@ public function getName(): string
public function getFilters(): array
{
return [
new TwigFilter('is_matrix', [$this, 'is_matrix']),
new TwigFilter('is_dropdown', [$this, 'is_dropdown']),
new TwigFilter('is_matrix', [$this, 'isMatrixField']),
new TwigFilter('is_dropdown', [$this, 'isDropdownField']),
new TwigFilter('is_asset', [$this, 'isAssetField']),
];
}

public function getFunctions(): array
{
return [
new TwigFunction('is_matrix', [$this, 'is_matrix']),
new TwigFunction('is_dropdown', [$this, 'is_dropdown']),
new TwigFunction('is_matrix', [$this, 'isMatrixField']),
new TwigFunction('is_dropdown', [$this, 'isDropdownField']),
new TwigFunction('is_asset', [$this, 'isAssetField']),
];
}

public function is_matrix(Field $field)
public function isMatrixField(Field $field)
{
return $field instanceof Matrix;
}

public function is_dropdown(Field $field)
public function isDropdownField(Field $field)
{
return $field instanceof Dropdown;
}

public function isAssetField(Field $field)
{
return $field instanceof Assets;
}
}

0 comments on commit cb9fc55

Please sign in to comment.