Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add fallback for variants #56

Merged
merged 1 commit into from
Nov 21, 2023
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
45 changes: 31 additions & 14 deletions src/Makaira/Connect/Modifier/Product/VariantAttributesModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -109,23 +102,47 @@ public function apply(Type $product)
],
false
);

$single = ($variantName[0]['oxvarname'] === '');

if (!$single) {
$titleArray = array_map('trim', explode('|', $variantName[0]['oxvarname']));
$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) {
Expand Down
Loading