diff --git a/src/BagistoGraphql.php b/src/BagistoGraphql.php index ad505489..377663bc 100755 --- a/src/BagistoGraphql.php +++ b/src/BagistoGraphql.php @@ -14,6 +14,7 @@ use Webkul\Product\Repositories\ProductGroupedProductRepository; use Webkul\Product\Repositories\ProductImageRepository; use Webkul\Product\Repositories\ProductVideoRepository; +use Webkul\Product\Helpers\ConfigurableOption; class BagistoGraphql { @@ -52,7 +53,8 @@ public function __construct( protected ProductDownloadableSampleRepository $productDownloadableSampleRepository, protected ProductGroupedProductRepository $productGroupedProductRepository, protected ProductImageRepository $productImageRepository, - protected ProductVideoRepository $productVideoRepository + protected ProductVideoRepository $productVideoRepository, + protected ConfigurableOption $configurableOption, ) {} /** @@ -776,8 +778,18 @@ public function manageInputForCart($product, $data) $superAttribute[$attribute['attribute_id']] = $attribute['attribute_option_id']; } } + $data['super_attribute'] = $superAttribute; + + if (! isset($data['selected_configurable_option'])) { + $selectedConfigurableOption = $this->getConfigurableOption($product, $superAttribute); + + if($selectedConfigurableOption){ + $data['selected_configurable_option'] = $selectedConfigurableOption; + } + } } + break; case 'grouped': if (! empty($data['qty'])) { @@ -881,6 +893,39 @@ public function manageInputForCart($product, $data) return $data; } + /** + * Get product variant by attribute. + */ + protected function getConfigurableOption($product, $superAttribute){ + + $configurableIndexes = $this->configurableOption->getConfigurationConfig($product)['index']; + + $matchedKey = null; + + foreach ($configurableIndexes as $parentKey => $values) { + + // Check if ALL selected attributes match + $isMatch = true; + + foreach ($superAttribute as $attrId => $optionId) { + if ( + ! isset($values[$attrId]) || + $values[$attrId] != $optionId + ) { + $isMatch = false; + break; + } + } + + if ($isMatch) { + $matchedKey = $parentKey; + break; + } + } + + return $matchedKey; + } + /** * To save image using path/url/base64 *