:::: MENU ::::
Posts tagged with: order collection

Magento 2 get order information by increment id

Today we discuss about Magento 2 get order information by increment id.This tutorial include how to change or get information of order by order increment id, how to load order by increment id. There are three ways to do that Api Repository, Factory method and object manager. So let start with our example.

Factory Method:

Best practice method always use this method

namespace QaisarSatti\Module\Block;
class Product extends \Magento\Framework\View\Element\Template
{
 protected $orderFactory;
public function __construct(

        \Magento\Sales\Model\OrderFactory $orderFactory,

    ) {


        $this->orderFactory = $orderFactory;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);

}


}

Api Repository:

This was introduce in Magento 2.1 by magento.

namespace QaisarSatti\Module\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Product extends \Magento\Framework\View\Element\Template
 {
     protected $order;
public function __construct(

\Magento\Sales\Api\Data\OrderInterface $order,

    ) {


        $this->order = $order;

    }

public function getOrderByIncrementId(){
        $orderIncrementId = 10000003;
        $order = $this->order->loadByIncrementId($orderIncrementId);

}
}

Object Manager:

Sometime you need to load order record in phtml file. Not recommended method to use in magento2 but so magento use it so here it is just for knowledge.

$orderIncrementId=10000003;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderInfo = $objectManager->create('Magento\Sales\Model\Order')->load($orderIncrementId);
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 17, 2018


Magento 2 addAttributeToFilter

Today we discuss Magento 2 addAttributeToFilter. addAttributeToFilter is a method that can be called on EAV collections in Magento 2. That will be use in product collection, category collection, order collection customer collection and other collection too. addAttributeToFilter is adding WHERE condition in Mysql question to get collection.

addAttributeToFilter condition

There are many condition you can use with addAttributeToFilter. Example of condition equal, not equal, like , not like, in, not in, null, not null, greater than , less than , greater than equal to and less than equal to.

protected $_productCollectionFactory;

public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
    ) {
        $this->_productCollectionFactory = $productFactory;
    }
    public function getProductCollection()
    {
      return   $this->_productCollectionFactory->create()->addAttributeToSelect('*')->addAttributeToFilter('sku’,’test’);
     
     }

Equal: eq

Now we use equal to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$this->_productCollectionFactory->addAttributeToFilter('status', 1); // Without using the operator

Not Equals – neq

Now we use not equal to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('sku', array('neq' => 'test-product'));

Like – like

Now we use Like to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('sku', array('like' => 'UX%'));

Not Like – nlike

Now we use not like to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));

In – in

Now we use In to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('entity_id', array('in' => array(1,4,98)));

Not In – nin

Now we use not In to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('entity_id', array('nin' => array(1,4,98)));

NULL – null

Now we use null to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('description', array('null' => true));

Not NULL – notnull

Now we use not null to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('description', array('notnull' => true));

Greater Than – gt

Now we use greater than to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('entity_id', array('gt' => 5));

Less Than – lt

Now we use less than to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('entity_id', array('lt' => 5));

Greater Than or Equals To- gteq

Now we use greater than to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('entity_id', array('gteq' => 5));

Less Than or Equals To – lteq

Now we use less than equal to filter production collection.

$this->_productCollectionFactory->addAttributeToFilter('entity_id', array('lteq' => 5));
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 30, 2018


Magento 2 programmatically add custom order status

Today we talk about how in Magento 2 programmatically add custom order status. This tutorial includes set custom order status and also set the default magento order status too. You have to inject Magento\Sales\Model\Order to set order status. So let start with our example.

Dependency Injection

protected $orderFactory;
public function __construct(
           
        \Magento\Sales\Model\OrderFactory $orderFactory,

    ) {


        $this->orderFactory = $orderFactory;

    }

public function customOrderStatus(){
        $orderId = 3;
        $order = $this->orderFactory->create()->load($orderId);
        $state = $order->getState();
        $status = 'custom_cancel';
        $comment = 'This is test comment';
        $isNotified = false;
        $order->setState($state);
        $order->setStatus($status);
        $order->addStatusToHistory($order->getStatus(), $comment);
        $order->save();
}

Object Manager

        $orderId = 3;
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
        $state = $order->getState();
        $status = 'custom_cancel';
        $comment = '';
        $isNotified = false;
        $order->setState($state);
        $order->setStatus($status);
        $order->addStatusToHistory($order->getStatus(), $comment);
        $order->save();
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 sort collection by given ids

Today we discuss how in Magento 2 sort collection by give ids. Sometime we need to get product collection by given ids form example random ids 5,4,13. We take the example of product collection and filter it by give ids. You can use this on any kind of collection like order collection, category collection and any custom collection too. So here is our example.

protected $_productCollectionFactory;
public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
    ) {
         $this->_productCollectionFactory = $productFactory;
    }
    public function getProductCollection()
    {
        $productIds=array(9,21,5,3);
        $productCollection = $this->_productCollectionFactory->create();
        $productCollection->getSelect()->order(new \Zend_Db_Expr('FIELD(entity_id,' . implode(',', $productIds).')'));
        return $productCollection;
     
     }

Note: You can sort any collection following this example.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 get single column values from collection

Today we learn about how to get single column values from collection. For this example we use the product collection to get names of product. You can get product collection by following the Magento 2 get product collection.

protected $_productCollectionFactory;

public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
    ) {
        $this->_productCollectionFactory = $productFactory;
    }
    public function getProductCollection()
    {
      $productCollection = $this->_productCollectionFactory->create();
      return $productCollection->getColumnValues(name);
     
     }

Note: You can use same method with any collection.

Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018