Skip to content
Open
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
47 changes: 46 additions & 1 deletion src/BagistoGraphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
) {}

/**
Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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
*
Expand Down