Orders.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\SalesGraphQl\Model\Resolver;
  8. use Magento\Framework\GraphQl\Config\Element\Field;
  9. use Magento\Framework\GraphQl\Query\ResolverInterface;
  10. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  11. use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
  12. use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
  13. /**
  14. * Orders data reslover
  15. */
  16. class Orders implements ResolverInterface
  17. {
  18. /**
  19. * @var CollectionFactoryInterface
  20. */
  21. private $collectionFactory;
  22. /**
  23. * @var CheckCustomerAccount
  24. */
  25. private $checkCustomerAccount;
  26. /**
  27. * @param CollectionFactoryInterface $collectionFactory
  28. * @param CheckCustomerAccount $checkCustomerAccount
  29. */
  30. public function __construct(
  31. CollectionFactoryInterface $collectionFactory,
  32. CheckCustomerAccount $checkCustomerAccount
  33. ) {
  34. $this->collectionFactory = $collectionFactory;
  35. $this->checkCustomerAccount = $checkCustomerAccount;
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function resolve(
  41. Field $field,
  42. $context,
  43. ResolveInfo $info,
  44. array $value = null,
  45. array $args = null
  46. ) {
  47. $customerId = $context->getUserId();
  48. $this->checkCustomerAccount->execute($customerId, $context->getUserType());
  49. $items = [];
  50. $orders = $this->collectionFactory->create($customerId);
  51. /** @var \Magento\Sales\Model\Order $order */
  52. foreach ($orders as $order) {
  53. $items[] = [
  54. 'id' => $order->getId(),
  55. 'increment_id' => $order->getIncrementId(),
  56. 'created_at' => $order->getCreatedAt(),
  57. 'grand_total' => $order->getGrandTotal(),
  58. 'status' => $order->getStatus(),
  59. ];
  60. }
  61. return ['items' => $items];
  62. }
  63. }