How to get orders list by customer id in graphql magento 2?

From Magento 2.4 Out of the box, Magento supports Customer Orders History using GraphQL.

You can check the syntax by clicking Retrieve all the Order list of a customer by GraphQL Query

Given Article is used for the learning of GraphQL Practice with Magento. I have written an article before Magento supports the Sales GraphQL feature in the core code.

How to get a list of orders of a customer using GraphQl Magento 2?

We need to create a simple module for getting all the order lists of Customer using GraphQl. Default Magento 2, We can easily get all the orders of a customer by fetch OrderRepository.

Using Graphql, You can get an order list of a specific registered customer by customer id.

We need to create a Resolver model and add our custom logic for getting all the Customer Order list. We will display all the orders of a customer in the response to the query.

I hope you are aware of What is GraphQl and how GraphQL is used in Magento 2 If You are new to GraphQL check the link for GraphQl in Magento 2.

Now we can start the module using Magento 2 to fetch all orders of a specific customer. Continue reading “How to get orders list by customer id in graphql magento 2?”

Get Customer Custom attribute value in Magento 2.

We can get custom attribute value of Customer in magento 2 by simple way. If we have created any customer attribute in Magento 2 and we need to get that value in any place in code you can just get custom attribute value by below way,

Let’s we consider Mobile attribute value is custom attribute value created for Customer, attribute code for Mobile is mobile.

You can get customer custom_attribute value by,

$customer = $CUSTOMER_OBJECT; // GET customer object
$customer->getCustomAttribute('mobile')->getValue();

return will be your customer custom_attribute mobile value.

How to get customer group collection in Magento 2?

In Magento 2 We can easily get customer group programmatically by using below code snippets. You are aware of How to create customer and new address programmatically in Magento 2

Below code snippets display all the customer group in the system,
Call groupCollectionFactory using __construct() in block file,

<?php
namespace Rbj\CustomerGroup\Block;

class CustomerGroup extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory,
        array $data = []
    ) {
        $this->groupCollectionFactory = $groupCollectionFactory;
        parent::__construct($context, $data);
    }

    /**
     * Retrieve customer group collection
     *
     * @return GroupCollection
     */
    public function getCustomerGroupCollection()
    {
        if (!$this->hasData('customer_group_collection')) {
            $collection = $this->groupCollectionFactory->create();
            $this->setData('customer_group_collection', $collection);
        }

        return $this->getData('customer_group_collection');
    }

Call from template file,

<?php
$customerGroupCollection = $block->getCustomerGroupCollection();
foreach($customerGroupCollection as $customerGroup) {
	echo 'ID '.$customerGroup->getId();
	echo 'Code '.$customerGroup->getCode();
	echo 'Tax class Id '.$customerGroup->getTaxClassId();
	echo "<br>";
}

The output will be look like,

ID 0 Code NOT LOGGED IN Tax class Id 3
ID 1 Code General Tax class Id 3
ID 2 Code Wholesale Tax class Id 3
ID 3 Code Retailer Tax class Id 3