Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #109 from nikunjkotecha/get_parent_sku_updates
Browse files Browse the repository at this point in the history
If multiple parents available, use the one with display node.
  • Loading branch information
miromichalicka authored Jan 31, 2019
2 parents 3f7fdf9 + 6021da7 commit f34719a
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions modules/acm_sku/src/AcquiaCommerce/SKUPluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,52 @@ public function cartName(SKU $sku, array $cart, $asString = FALSE) {
* @throws \Exception
*/
public function getParentSku(SKU $sku) {
$static = &drupal_static(__FUNCTION__, []);

$langcode = $sku->language()->getId();
$sku_string = $sku->getSku();

if (isset($static[$langcode], $static[$langcode][$sku_string])) {
return $static[$langcode][$sku_string];
}

// Initialise with empty value.
$static[$langcode][$sku_string] = NULL;

$query = $this->connection->select('acm_sku_field_data', 'acm_sku');
$query->addField('acm_sku', 'sku');
$query->join('acm_sku__field_configured_skus', 'child_sku', 'acm_sku.id = child_sku.entity_id');
$query->condition('child_sku.field_configured_skus_value', $sku->getSku());
$query->condition('child_sku.field_configured_skus_value', $sku_string);

$parent_sku = $query->execute()->fetchField();
$parent_skus = array_keys($query->execute()->fetchAllAssoc('sku'));

if (empty($parent_sku)) {
if (empty($parent_skus)) {
return NULL;
}

return SKU::loadFromSku($parent_sku, $sku->language()->getId());
if (count($parent_skus) > 1) {
\Drupal::logger('acm_sku')->warning(
'Multiple parents found for SKU: @sku, parents: @parents',
[
'@parents' => implode(',', $parent_skus),
'@sku' => $sku_string,
]
);
}

foreach ($parent_skus as $parent_sku) {
$parent = SKU::loadFromSku($parent_sku, $langcode);
if ($parent instanceof SKU) {
$node = $this->getDisplayNode($parent, FALSE, FALSE);

if ($node instanceof Node) {
$static[$langcode][$sku_string] = $parent;
break;
}
}
}

return $static[$langcode][$sku_string];
}

/**
Expand Down

0 comments on commit f34719a

Please sign in to comment.