Skip to content

Commit

Permalink
fix: Add null check for product images
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnynotsolucky committed Sep 5, 2024
1 parent 51ed9ee commit 51bbcd9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
20 changes: 20 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# EditorConfig is awesome: https://EditorConfig.org

# Top-most EditorConfig file
root = true

# All files
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

# Markdown files
[*.md]
trim_trailing_whitespace = false

[*.{yaml,yml}]
indent_size = 2
indent_style = space
25 changes: 14 additions & 11 deletions src/services/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace fostercommerce\craftfostercheckout\services;

use craft\commerce\elements\Product;
use craft\commerce\elements\Variant;
use craft\commerce\models\LineItem;
use craft\commerce\Plugin;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function note($page): ?string
return $note;
}

/*
/**
* Gets the line items image field based on the products settings
*/
public function lineItemImageField($productType): ?array
Expand Down Expand Up @@ -116,28 +117,30 @@ public function lineItemImageField($productType): ?array
return $fieldData;
}

/*
/**
* Gets a line items image asset based on the config settings for the product type
*
* @throws InvalidConfigException
*/
public function lineItemImage(LineItem $lineItem): ?Asset
{
$image = null;
$sku = $lineItem->getSku();
$variant = Variant::find()->sku($sku)->one();
/** @var Product $product */
$product = $variant->getOwner();

$fieldInfo = $this->lineItemImageField($product->type->handle);

if ($fieldInfo['level'] === 'variant') {
if ($fieldInfo !== null) {
try {
$image = $variant->getFieldValue($fieldInfo['handle'])->one();
} catch (InvalidFieldException) {
$image = null;
}
} else {
try {
$image = $product->getFieldValue($fieldInfo['handle'])->one();
if ($fieldInfo['level'] === 'variant') {
$image = $variant->getFieldValue($fieldInfo['handle'])->one();
} else {
$image = $product->getFieldValue($fieldInfo['handle'])->one();
}
} catch (InvalidFieldException) {
$image = null;
// Do nothing
}
}

Expand Down

0 comments on commit 51bbcd9

Please sign in to comment.