Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Empty cart creation added #124

Merged
merged 14 commits into from Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,95 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver\Cart;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\GuestCartManagementInterface;
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;

/**
* {@inheritdoc}
*/
class CreateEmptyCart implements ResolverInterface
{
/**
* @var CartManagementInterface
*/
private $cartManagement;

/**
* @var GuestCartManagementInterface
*/
private $guestCartManagement;

/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @var QuoteIdMaskFactory
*/
private $quoteIdMaskFactory;

/**
* @var UserContextInterface
*/
private $userContext;

/**
* @param CartManagementInterface $cartManagement
* @param GuestCartManagementInterface $guestCartManagement
* @param ValueFactory $valueFactory
* @param UserContextInterface $userContext
* @param QuoteIdMaskFactory $quoteIdMaskFactory
*/
public function __construct(
CartManagementInterface $cartManagement,
GuestCartManagementInterface $guestCartManagement,
ValueFactory $valueFactory,
UserContextInterface $userContext,
QuoteIdMaskFactory $quoteIdMaskFactory
) {
$this->cartManagement = $cartManagement;
$this->guestCartManagement = $guestCartManagement;
$this->valueFactory = $valueFactory;
$this->userContext = $userContext;
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
}

/**
* {@inheritDoc}
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
{
$customerId = $this->userContext->getUserId();

if ($customerId) {
$cartId = $this->cartManagement->createEmptyCartForCustomer($customerId);
/** @var QuoteIdMask $quoteIdMask */
$quoteIdMask = $this->quoteIdMaskFactory->create();
$quoteIdMask->setQuoteId($cartId)->save();
$cartId = $quoteIdMask->getMaskedId();
} else {
$cartId = $this->guestCartManagement->createEmptyCart();
}

$result = function () use ($cartId) {
return $cartId;
};

return $this->valueFactory->create($result);
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/QuoteGraphQl/README.md
@@ -0,0 +1,4 @@
# QuoteGraphQl

**QuoteGraphQl** provides type and resolver information for the GraphQl module
to generate quote (cart) information endpoints. Also provides endpoints for modifying a quote.
27 changes: 27 additions & 0 deletions app/code/Magento/QuoteGraphQl/composer.json
@@ -0,0 +1,27 @@
{
"name": "magento/module-quote-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-authorization": "*",
"magento/module-quote": "*"
},
"suggest": {
"magento/module-graph-ql": "*",
"magento/module-catalog-graph-ql": "*"
},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magento\\QuoteGraphQl\\": ""
}
}
}
14 changes: 14 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/module.xml
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_QuoteGraphQl">
<sequence>
<module name="Magento_GraphQl"/>
</sequence>
</module>
</config>
6 changes: 6 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
@@ -0,0 +1,6 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type Mutation {
createEmptyCart: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\Cart\\CreateEmptyCart") @doc(description:"Creates empty shopping cart for guest or logged in user")
}
10 changes: 10 additions & 0 deletions app/code/Magento/QuoteGraphQl/registration.php
@@ -0,0 +1,10 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_QuoteGraphQl', __DIR__);
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -184,6 +184,7 @@
"magento/module-product-video": "*",
"magento/module-quote": "*",
"magento/module-quote-analytics": "*",
"magento/module-quote-graph-ql": "*",
"magento/module-release-notification": "*",
"magento/module-reports": "*",
"magento/module-require-js": "*",
Expand Down
78 changes: 44 additions & 34 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.