How to use php curl as best practice in magento 2?

In Magento 2, We can get details about curl from Curl.php file at location Magento\Framework\HTTP\Adapter\Curl.

Magento gives native functionality for CURL instead of using default php curl_init().
We can send a request to third party services using curl by below way,
Continue reading “How to use php curl as best practice in magento 2?”

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
?>

 

How to fetch getUrl() in Plugin or Observer file in magento 2?

By default you cant call $this->getUrl() method in Plugin or Observer file. When you need to call getUrl() function of Magento in plugin or observer file you need to pass dependancy as Magento\Framework\UrlInterface to construct() method.

public function __construct(
    \Magento\Framework\UrlInterface $urlBuilder
) {
    $this->urlBuilder = $urlBuilder;
}

Now In plugin or Observer function we can get getUrl() as below way,
$this->urlBuilder->getUrl(‘sales/order/view’, [‘order_id’ => 1]);

Run below command for get effect of our Dependancy Injection to code,

php bin/magento setup:di:compile

Above url will return as,
http://{yoururl.com}/admin/sales/order/view/order_id/1/