From da6a8ef7e29d8f878f8124587ec5fc5f81735808 Mon Sep 17 00:00:00 2001 From: BenjaminJoerger Date: Tue, 21 Nov 2023 11:10:31 +0100 Subject: [PATCH] fix: Add fallback for variants --- .../Product/VariantAttributesModifier.php | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Makaira/Connect/Modifier/Product/VariantAttributesModifier.php b/src/Makaira/Connect/Modifier/Product/VariantAttributesModifier.php index f20f759c..3c854dd8 100644 --- a/src/Makaira/Connect/Modifier/Product/VariantAttributesModifier.php +++ b/src/Makaira/Connect/Modifier/Product/VariantAttributesModifier.php @@ -87,20 +87,13 @@ public function __construct( } /** - * Modify product and return modified product - * - * @param BaseProduct|Type $product + * @param Type $product * - * @return BaseProduct|Type - * @SuppressWarnings(CyclomaticComplexity) + * @return array */ - public function apply(Type $product) + public function getVariantData(Type $product) { - if (!$product->id) { - throw new ConnectException("Cannot fetch attributes without a product ID."); - } - - $product->attributes = []; + $variants = [['id' => '']]; $variantName = $this->database->query( $this->selectVariantNameQuery, @@ -109,6 +102,7 @@ public function apply(Type $product) ], false ); + $single = ($variantName[0]['oxvarname'] === ''); if (!$single) { @@ -116,16 +110,39 @@ public function apply(Type $product) $hashArray = array_map('md5', $titleArray); $query = str_replace('{{activeSnippet}}', $this->activeSnippet, $this->selectVariantDataQuery); - $variants = $this->database->query( + $dbvariants = $this->database->query( $query, [ 'productId' => $product->id, ] ); - } else { - $variants = [['id' => '']]; + if ($dbvariants) { + $variants = $dbvariants; + } } + return $variants; + } + + + /** + * Modify product and return modified product + * + * @param BaseProduct|Type $product + * + * @return BaseProduct|Type + * @SuppressWarnings(CyclomaticComplexity) + */ + public function apply(Type $product) + { + if (!$product->id) { + throw new ConnectException("Cannot fetch attributes without a product ID."); + } + + $product->attributes = []; + + $variants = getVariantData($product); + foreach ($variants as $variant) { $id = $variant['id']; if ($id) {