Skip to content

Commit

Permalink
Fixed configurable product adding to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredsgenkins committed May 30, 2019
1 parent 86f85b4 commit 0ae2b84
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/Model/Resolver/SaveCartItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@


use Exception;
use Magento\ConfigurableProduct\Model\Quote\Item\ConfigurableItemOptionValueFactory;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Model\Quote\ProductOptionFactory;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartItemInterface;
use \Magento\Quote\Api\Data\CartItemInterfaceFactory;
use Magento\Quote\Api\Data\ProductOptionExtensionFactory;
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Magento\Quote\Model\Webapi\ParamOverriderCartId;
use Magento\Catalog\Model\ProductRepository;
Expand All @@ -55,7 +48,7 @@ class SaveCartItem implements ResolverInterface
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;

/**
* @var CartRepositoryInterface
*/
Expand All @@ -79,7 +72,6 @@ class SaveCartItem implements ResolverInterface
/**
* SaveCartItem constructor.
* @param CartItemRepositoryInterface $cartItemRepository
* @param CartItemInterfaceFactory $cartItemFactory
* @param QuoteIdMaskFactory $quoteIdMaskFactory
* @param CartRepositoryInterface $quoteRepository
* @param ParamOverriderCartId $overriderCartId
Expand All @@ -88,31 +80,30 @@ class SaveCartItem implements ResolverInterface
*/
public function __construct(
CartItemRepositoryInterface $cartItemRepository,
CartItemInterfaceFactory $cartItemFactory,
QuoteIdMaskFactory $quoteIdMaskFactory,
CartRepositoryInterface $quoteRepository,
ParamOverriderCartId $overriderCartId,
ProductRepository $productRepository,
Repository $attributeRepository
) {
$this->cartItemRepository = $cartItemRepository; //
$this->quoteIdMaskFactory = $quoteIdMaskFactory; //
$this->quoteRepository = $quoteRepository; //
$this->overriderCartId = $overriderCartId; //
$this->productRepository = $productRepository; //
$this->cartItemRepository = $cartItemRepository;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
$this->quoteRepository = $quoteRepository;
$this->overriderCartId = $overriderCartId;
$this->productRepository = $productRepository;
$this->attributeRepository = $attributeRepository;
}

private function makeAddRequest(Product $product, $sku = null, $qty = 1)
private function makeAddRequest(Product $product, array $options)
{
$data = [
'product' => $product->getEntityId(),
'qty' => $qty
'qty' => $options['qty']
];

switch ($product->getTypeId()) {
case Configurable::TYPE_CODE:
$data = $this->setConfigurableRequestOptions($product, $sku, $data);
$data = $this->setConfigurableRequestOptions($options, $data);
break;
case Type::TYPE_CODE:
$data = $this->setBundleRequestOptions($product, $data);
Expand All @@ -125,17 +116,13 @@ private function makeAddRequest(Product $product, $sku = null, $qty = 1)
return $request;
}

private function setConfigurableRequestOptions(Product $product, $sku, array $data)
private function setConfigurableRequestOptions(array $options, array $data)
{
/** @var Type | Configurable $typedProduct */
$typedProduct = $product->getTypeInstance();

$childProduct = $this->productRepository->get($sku);
$productAttributeOptions = $typedProduct->getConfigurableAttributesAsArray($product);

$configurableOptions = $options["product_option"]["extension_attributes"]["configurable_item_options"] ?? [];
$superAttributes = [];
foreach ($productAttributeOptions as $option) {
$superAttributes[$option['attribute_id']] = $childProduct->getData($option['attribute_code']);

foreach ($configurableOptions as $option) {
$superAttributes[$option['option_id']] = $option['option_value'];
}

$data['super_attribute'] = $superAttributes;
Expand Down Expand Up @@ -195,8 +182,7 @@ public function resolve(
$product = $this->productRepository->get($requestCartItem['sku']);
$quote->addProduct($product, $this->makeAddRequest(
$product,
$requestCartItem['sku'],
$requestCartItem['qty']
$requestCartItem
));
$this->quoteRepository->save($quote);

Expand Down

0 comments on commit 0ae2b84

Please sign in to comment.