Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace fostercommerce\shipstationconnect\models;

use craft\commerce\elements\Variant;
use craft\commerce\enums\LineItemType;
use craft\commerce\models\LineItem;
use craft\commerce\Plugin as CommercePlugin;
use craft\elements\Asset;
Expand Down Expand Up @@ -224,24 +225,34 @@ public static function fromCommerceLineItem(LineItem $lineItem): self
};

$imageUrl = null;
$productImagesHandle = Plugin::getInstance()?->settings->productImagesHandle;
/** @var ?Variant $purchasable */
$purchasable = $lineItem->getPurchasable();
if ($productImagesHandle !== null && $purchasable !== null) {
/** @var ?AssetQuery<int, Asset> $assetQuery */
$assetQuery = $purchasable->{$productImagesHandle};
if ($assetQuery === null) {
// Fallback to the product if the variant does not have an asset
$productImagesHandle = trim(Plugin::getInstance()?->settings->productImagesHandle ?? '');

if ($productImagesHandle === '') {
// Ensure that if the handle is an empty string, it is still considered empty.
$productImagesHandle = null;
}

// Custom line items do not have a Commerce purchasable.
if ($lineItem->type !== LineItemType::Custom && $productImagesHandle !== null) {
/** @var ?Variant $purchasable */
$purchasable = $lineItem->getPurchasable();

if ($purchasable !== null) {
/** @var ?AssetQuery<int, Asset> $assetQuery */
$assetQuery = $purchasable->getOwner()->{$productImagesHandle};
}
$assetQuery = $purchasable->{$productImagesHandle};
if ($assetQuery === null) {
// Fallback to the product if the variant does not have an asset
/** @var ?AssetQuery<int, Asset> $assetQuery */
$assetQuery = $purchasable->getOwner()->{$productImagesHandle};
}

if ($assetQuery !== null) {
/** @var ?Asset $asset */
$asset = $assetQuery->one();
$assetUrl = $asset?->getUrl();
if ($assetUrl !== null) {
$imageUrl = UrlHelper::siteUrl($assetUrl);
if ($assetQuery !== null) {
/** @var ?Asset $asset */
$asset = $assetQuery->one();
$assetUrl = $asset?->getUrl();
if ($assetUrl !== null) {
$imageUrl = UrlHelper::siteUrl($assetUrl);
}
}
}
}
Expand Down