Skip to content

Expose Fast_Order_Id/Fast_Order_Item_Uuid to Cart/CartItem GraphQl #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions Model/Resolver/Cart/FastOrderId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Fast_Checkout
*
* PHP version 7.3
*
* @package Fast_Checkout
* @author Fast <[email protected]>
* @copyright 2021 Copyright Fast AF, Inc., https://www.fast.co/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2022

* @license https://opensource.org/licenses/OSL-3.0 OSL-3.0
* @link https://www.fast.co/
*/

declare(strict_types=1);

namespace Fast\Checkout\Model\Resolver\Cart;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import never used

use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import never used


class FastOrderId implements ResolverInterface
{
const FAST_ORDER_ID= 'fast_order_id';

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined class. Lets add use Magento\Framework\Exception\LocalizedException; under namespaces

}
/** @var Quote $cart */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undefined class. Lets add use Magento\Quote\Model\Quote; under namespace

$cart = $value['model'];
return $cart->getData(static::FAST_ORDER_ID);
}
}
40 changes: 40 additions & 0 deletions Model/Resolver/CartItem/FastOrderItemUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Fast_Checkout
*
* PHP version 7.3
*
* @package Fast_Checkout
* @author Fast <[email protected]>
* @copyright 2021 Copyright Fast AF, Inc., https://www.fast.co/
* @license https://opensource.org/licenses/OSL-3.0 OSL-3.0
* @link https://www.fast.co/
*/

declare(strict_types=1);

namespace Fast\Checkout\Model\Resolver\CartItem;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please see above

use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

class FastOrderItemUuid implements ResolverInterface
{
const FAST_ORDER_ITEM_UUID_NAME = 'fast_order_item_uuid';

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please see above

}
/** @var Item $cartItem */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add use Magento\Quote\Model\Quote\Item; under namespace

$cartItem = $value['model'];
return $cartItem->getData(static::FAST_ORDER_ITEM_UUID_NAME);
}
}
65 changes: 65 additions & 0 deletions Plugin/Resolver/AddProductsToCartWithFastItemUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Fast\Checkout\Plugin\Resolver;

use Closure;
use Magento\QuoteGraphQl\Model\Resolver\AddProductsToCart\Interceptor;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\App\ResourceConnection;

/**
* Class AddProductsToCartWithFastItemUuid
*/
class AddProductsToCartWithFastItemUuid
{
protected $resourceConnection;
private $logger;

public function __construct(
\Psr\Log\LoggerInterface $logger,
ResourceConnection $resourceConnection
) {
$this->logger = $logger;
$this->resourceConnection = $resourceConnection;
}

public function aroundResolve(
Interceptor $subject,
Closure $proceed,
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$result = $proceed($field, $context, $info, $value, $args);

$cart = $result['cart']['model'];
$cartItems = $args['cartItems'];
$connection = $this->resourceConnection->getConnection();
$skuToFastId = [];
foreach ($cartItems as $cartItem) {
if (isset($cartItem['fast_order_item_uuid'])) {
$skuToFastId[$cartItem['sku']] = $cartItem['fast_order_item_uuid'];
}
}

$this->logger->info('add product run');

foreach ($cart->getItems() as $cItem) {
$fastOrderItemUuid = $skuToFastId[$cItem->getSku()];

// update DB directly to save fast_order_item_uuid, similar to /Plugin/Uuid/AddFastOrderItemUuid
$query = "UPDATE `quote_item` SET `fast_order_item_uuid`= '" . $fastOrderItemUuid . "' WHERE item_id = " . $cItem->getItemId();
$connection->query($query);

$extensionAttributes = $cItem->getExtensionAttributes();
$cItem->setData('fast_order_item_uuid', $fastOrderItemUuid);
$extensionAttributes->setFastOrderItemUuid($fastOrderItemUuid);
$cItem->setExtensionAttributes($extensionAttributes);
}

return $result;
}
}
42 changes: 42 additions & 0 deletions Plugin/Resolver/CreateEmptyCartWithFastOrderId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Fast\Checkout\Plugin\Resolver;

use Closure;
use \Magento\Framework\App\Http\Context;
use Magento\QuoteGraphQl\Model\Resolver\CreateEmptyCart\Interceptor;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Class CreateEmptyCartWithFastOrderId
*/
class CreateEmptyCartWithFastOrderId
{
const CONTEXT_FAST_ORDER_ID_KEY = 'fast_order_id';

protected $httpContext;

public function __construct(
Context $httpContext
) {
$this->httpContext = $httpContext;
}

public function aroundResolve(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend using beforeResolve method here

/**
     * @param Interceptor $subject
     * @param Field $field
     * @param $context
     * @param ResolveInfo $info
     * @param array|null $value
     * @param array|null $args
     * @return null
     */
    public function beforeResolve(Interceptor $subject, Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
    {
        if (isset($args['input']['fast_order_id'])) {
            // pass along the fast_order_id, so Plugin/Api/CartManagement can pick it up
            $this->httpContext->setValue(self::CONTEXT_FAST_ORDER_ID_KEY, $args['input']['fast_order_id'], null);
        }

        //since we're not changing any parameters
        return null;
    }

Interceptor $subject,
Closure $proceed,
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (isset($args['input']['fast_order_id'])) {
// pass along the fast_order_id, so Plugin/Api/CartManagement can pick it up
$this->httpContext->setValue(self::CONTEXT_FAST_ORDER_ID_KEY, $args['input']['fast_order_id'], null);
}

return $proceed($field, $context, $info, $value, $args);
}
}
7 changes: 7 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@
</argument>
</arguments>
</virtualType>

<type name="Magento\QuoteGraphQl\Model\Resolver\CreateEmptyCart">
<plugin name="save_fast_order_id_to_cart_gql" type="Fast\Checkout\Plugin\Resolver\CreateEmptyCartWithFastOrderId"/>
</type>
<type name="Magento\QuoteGraphQl\Model\Resolver\AddProductsToCart">
<plugin name="save_fast_item_uuid_cart_item_gql" type="Fast\Checkout\Plugin\Resolver\AddProductsToCartWithFastItemUuid"/>
</type>
</config>
16 changes: 16 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

type Cart {
fast_order_id: String @resolver(class: "\\Fast\\Checkout\\Model\\Resolver\\Cart\\FastOrderId")
}

interface CartItemInterface {
fast_order_item_uuid: String @resolver(class: "\\Fast\\Checkout\\Model\\Resolver\\CartItem\\FastOrderItemUuid")
}

input createEmptyCartInput {
fast_order_id: String
}

input CartItemInput {
fast_order_item_uuid: String
}