Created
January 25, 2022 12:37
-
-
Save magevision/693d5227b0c3664cde3bf2b338cd3f2d to your computer and use it in GitHub Desktop.
GetOrdersByPaymentMethod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd"> | |
<extension_attributes for="Magento\Sales\Api\Data\OrderInterface"> | |
<attribute code="payment_method_code" type="string"> | |
<join reference_table="sales_order_payment" reference_field="parent_id" join_on_field="entity_id"> | |
<field>method</field> | |
</join> | |
</attribute> | |
</extension_attributes> | |
</config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace MageVision\Blog72\Block; | |
use Magento\Framework\Api\FilterBuilder; | |
use Magento\Framework\Api\SearchCriteriaBuilder; | |
use Magento\Framework\View\Element\Template; | |
use Magento\Framework\View\Element\Template\Context; | |
use Magento\OfflinePayments\Model\Checkmo; | |
use Magento\Sales\Api\OrderRepositoryInterface; | |
class Orders extends Template | |
{ | |
private SearchCriteriaBuilder $searchCriteriaBuilder; | |
private FilterBuilder $filterBuilder; | |
private OrderRepositoryInterface $orderRepository; | |
/** | |
* @param Context $context | |
* @param SearchCriteriaBuilder $searchCriteriaBuilder | |
* @param FilterBuilder $filterBuilder | |
* @param OrderRepositoryInterface $orderRepository | |
* @param array $data | |
*/ | |
public function __construct( | |
Context $context, | |
SearchCriteriaBuilder $searchCriteriaBuilder, | |
FilterBuilder $filterBuilder, | |
OrderRepositoryInterface $orderRepository, | |
array $data = [] | |
) { | |
parent::__construct($context, $data); | |
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | |
$this->filterBuilder = $filterBuilder; | |
$this->orderRepository = $orderRepository; | |
} | |
/** | |
* Get orders purchased with checkmo payment method | |
* | |
* @return void | |
*/ | |
public function getOrderByPaymentMethod() | |
{ | |
$searchCriteria = $this->searchCriteriaBuilder | |
->addFilters( | |
[ | |
$this->filterBuilder->setField('extension_attribute_payment_method_code.method')->setValue(Checkmo::PAYMENT_METHOD_CHECKMO_CODE)->create() | |
] | |
) | |
->create(); | |
$orders = $this->orderRepository->getList($searchCriteria); | |
foreach ($orders->getItems() as $order) { | |
//Your logic | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment