Magento 2 get order collection to update customers id using csv.
For example we can take magento 1 we can update using test.php file like that how we can update in magento 2.
I have no idea what you want to tell us here, but I can answer the question:
I am creating new module I am planing to update using controller.But in that how can i get orders collection
Collections are now a mere implementation detail, you work with them using SearchCriteria
and Repository
objects.
First, inject the OrderRepository
and the SearchCriteriaBuilder
into your controller, by adding it to the constructor parameters:
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->orderRepository = $orderRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct(
$context
);
}
Then you can use it like this:
$criteria = $this->searchCriteriaBuilder
// ->addSortOrder(...)
// ->addFilter(...)
// ...
->create();
$orderResult = $this->orderRepository->getList($criteria);
$orders = $orderResult->getItems();
The authentic and recommended way to fetch product collection is to use API interface
Magento\Catalog\Api\ProductRepositoryInterface
method
getList(\Magento\Framework\Api\SearchCriteriaInterface
$searchCriteria)
for this you need to build the search criteria first
Your class should include below classes -
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
Class constructor should be -
/** @var ProductRepositoryInterface */
protected $productRepository;
/** @var SearchCriteriaBuilder */
protected $searchCriteriaBuilder;
/** @var Visibility */
protected $productVisibility;
/** @var Status */
protected $productStatus;
/**
* Edit constructor.
* @param ProductRepositoryInterface $productRepositoryInterface
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param Visibility $visibility
* @param Status $status
*/
public function __construct(
ProductRepositoryInterface $productRepositoryInterface,
SearchCriteriaBuilder $searchCriteriaBuilder,
Visibility $visibility,
Status $status
) {
$this->productRepository = $productRepositoryInterface;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->productVisibility = $visibility;
$this->productStatus = $status;
}
Get list method should be -
/**
* @return array
*/
public function getProductList()
{
$searchCriteriaBuilder = $this->searchCriteriaBuilder
->addFilter('type_id', Type::TYPE_SIMPLE, 'eq')
->addFilter('status', $this->productStatus->getVisibleStatusIds(), 'in')
->addFilter('visibility', $this->productVisibility->getVisibleInCatalogIds(), 'in')
->create();
$productList = $this->productRepository->getList($searchCriteriaBuilder);
//echo $productList->getTotalCount();
$items = $productList->getItems();
return $items;
}
Best Answer
Overview of getting product collection in Magento 2
Step 1: Declare in Vendor_ModuleName Block
Step 2: Display product collection in phtml file
..
Step 1: Declare in Vendor_ModuleName Block
You will use a block class of the module Vendor_ModuleName, then possibly inject the object of
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
in the constructor of the module’s block class.add code this code
You can request the number of the product collection, that is a limited or unlimited number.
Step 2: Display product collection in phtml file
Print out the product collection in phtml file with the below code:
If that you have any queries about the article or any questions in general, use the comment section below!