How can I get guest user order list by email using rest API "rest/V1/orders" using searchCriteria ?
Version:-- Magento 2.4.5
You can filter on "customer_is_guest", here is an example:
GET <magento_host>/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=customer_email&searchCriteria[filter_groups][0][filters][0][value]=<guest_user_email>&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&searchCriteria[filter_groups][1][filters][0][field]=customer_is_guest&searchCriteria[filter_groups][1][filters][0][value]=1&searchCriteria[filter_groups][1][filters][0][condition_type]=eq
Do not forget to replace <magento_host> with the base URL of your Magento 2 store and <guest_user_email> with the desired email address.
Create below custom code
Create file :app/code/Vendor/Module/etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/orders" method="GET">
<service class="Vendor\Module\Api\GustordersInterface" method="getGustOrder"/>
<resources>
<resource ref="anonymous" />
</resources>
</route>
</routes>
Create file :app/code/Vendor/Module/etc/di.xml.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Vendor\Module\Api\GustordersInterface" type="Vendor\Module\Model\Gustorders" />
</config>
Create file :app/code/Vendor/Module/Api/GustordersInterface.php
<?php
namespace Vendor\Module\Api;
/**
* orders information acquirer interface
*
* @api
* @since 100.0.2
*/
interface GustordersInterface
{
/**
* Get orders information for the store.
*
* @param string $email_id
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return \Magento\Directory\Api\Data\CountryInformationInterface
*/
public function getGustOrder();
}
Create file :app/code/Vendor/Module/Model/Gustorders.php
<?php
namespace Vendor\Module\Model;
use Magento\Framework\Exception\NoSuchEntityException;
class Gustorders implements \Vendor\Module\GustordersInterface
{
public function __construct(
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
)
{
$this->orderCollectionFactory = $orderCollectionFactory;
}
public function getOrderCollection()
{
$email_id = $_GET['email_id'];
$orderCollecion = $this->orderCollectionFactory->create()->addFieldToSelect('*');
$orderCollecion->addAttributeToFilter('customer_email', $email_id);
echo "<pre>";
print_r($orderCollecion->getData());
echo "</pre>";
die('test');
}
}
Test in postman : https://extample.com/rest/V1/[email protected]