How to get Shipping and Billing address from Order in magento 2?

In Magento 2 we can get Customer billing and Shipping address by order id using below code snippet,
Sometimes we required billing or shipping address of specific order placed by the customer we can get billing and shipping address by order id,
Using Block file get customer order’s billing and shipping address,

<?php

namespace Rbj\Address\Block;

class AddressFromOrder extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory $addressCollection
        array $data = []
    ) {
        $this->orderRepository = $orderRepository;
        $this->addressCollection = $addressCollection;
        parent::__construct($context, $data);
    }

    /**
      *@param int $id The order ID.
      */
    public function getOrderData($id)
    {
        try {
            $order = $this->orderRepository->get($id);
        } catch (NoSuchEntityException $e) {
            throw new \Magento\Framework\Exception\LocalizedException(__('This order no longer exists.'));
        }
    }

    /* get Shipping address data of specific order */
    public function getShippingAddress($orderId) {
        $order = $this->getOrderData($orderId);
        /* check order is not virtual */
        if(!$order->getIsVirtual()) {
            $orderShippingId = $order->getShippingAddressId();
            $address = $this->addressCollection->create()->addFieldToFilter('entity_id',array($orderShippingId))->getFirstItem();
            return $address;
        }
        return null;
    }

    /* get Billing address data of specific order */
    public function getBillingAddress($orderId) {
        $order = $this->getOrderData($orderId);
        $orderBillingId = $order->getBillingAddressId();
        $address = $this->addressCollection->create()->addFieldToFilter('entity_id',array($orderBillingId))->getFirstItem();
        return $address;

    }
}

Call a required function in the template file, You need to pass Order id to below function. Using Order Id we can get Customer billing and shipping address id and based on respective id we can get billing and shipping address.

<?php
$orderId = 1;

$shippingAddress = $block->getShippingAddress($orderId); // return shipping address array
$billingAddress = $block->getBillingAddress($orderId);// return billing address array
?>

 

Get orders collection between a date range in magento 2.

We just need to pass start date and end date to get collection between Specific time in Magento 2. We need to filter created_at field using addAttributeToFilter(). Create Block file.
By default created_at field in  sales_order table represent the time of order creation in Magento 2.

<?php
namespace Rbj\Order\Block;

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

    /* Order collection between start and end date */
    public function getOrderCollectionByDateRange(){
        $startDate = date("Y-m-d h:i:s",strtotime('2018-1-1')); // start date
        $endDate = strtotime("Y-m-d h:i:s", strtotime('2018-10-1')); // end date

        $orders = $this->orderCollectionFactory->create()
            ->addAttributeToFilter('created_at', array('from'=>$startDate, 'to'=>$endDate));
        return $orders;
    }
?>

Call Function from template file,

$orders = $block->getOrderCollectionByDateRange();

if($orders->getTotalCount() > 0) { 
    foreach($orders as $_order) {
        $orderId = $_order['increment_id'];
        echo "<pre>";print_r($_order);
    }
}

You can get Order collection by date range by the above tricks.

How to get customer data by customer email in magento 2?

You can get customer information by just passing the customer email using below code snippet, Create Block file,

To Fetch Customer Data in Magento by customer email ID, you required a customer email id and an optional website id to fetch the correct data.

<?php
declare(strict_types=1);

namespace Rbj\Customer\Model;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class CustomerDetail
{
    public function __construct(CustomerRepositoryInterface $customerRepository )
    {
        $this->customerRepository = $customerRepository;
    }

    /**
     * @param string $email
     * @param ?int $websiteId
     * @return CustomerInterface
     * @throws LocalizedException
     */
    public function getCustomerByEmail(string $email, int $websiteId = null): CustomerInterface
    {
        try {
            $customer = $this->customerRepository->get($email, $websiteId);
        } catch (NoSuchEntityException $exception) {
            throw new LocalizedException(__('Provided Customer no longer exists.'));
        }
        
        return $customer;
    }
}

Continue reading “How to get customer data by customer email in magento 2?”