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 2 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,81 @@
<?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\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\GuestCartManagementInterface;
use Magento\Quote\Api\CartManagementInterface;
use Magento\Authorization\Model\UserContextInterface;

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

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

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

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

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

/**
* {@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);
} else {
$cartId = $this->guestCartManagement->createEmptyCart();
}

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

return $this->valueFactory->create($result);
}
}
25 changes: 25 additions & 0 deletions app/code/Magento/QuoteGraphQl/composer.json
@@ -0,0 +1,25 @@
{
"name": "magento/module-quote-graph-ql",
"description": "N/A",
"type": "magento2-module",
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*"
},
"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__);