-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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\Cart; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined class. Lets add |
||
} | ||
/** @var Quote $cart */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Undefined class. Lets add |
||
$cart = $value['model']; | ||
return $cart->getData(static::FAST_ORDER_ID); | ||
} | ||
} |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please see above |
||
} | ||
/** @var Item $cartItem */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets add |
||
$cartItem = $value['model']; | ||
return $cartItem->getData(static::FAST_ORDER_ITEM_UUID_NAME); | ||
} | ||
} |
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; | ||
} | ||
} |
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend using beforeResolve method here
|
||
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); | ||
} | ||
} |
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2022