Skip to content

Instantly share code, notes, and snippets.

@edkoster
Last active May 30, 2022 11:11
Show Gist options
  • Save edkoster/3b9174e0915bfa19f9e9d74c1cc5516b to your computer and use it in GitHub Desktop.
Save edkoster/3b9174e0915bfa19f9e9d74c1cc5516b to your computer and use it in GitHub Desktop.
Magento 2 sanbox file
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
try {
require __DIR__ . '/app/bootstrap.php';
} catch (\Exception $e) {
echo $e->getMessage();
exit(1);
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$orderId = 1810;
$orderIds = [1811,1812];
$s = new Sandbox();
$s->getOrder($orderId);
$s->searchOrders($orderIds);
$s->rawSql();
class Sandbox
{
protected $bootstrap;
protected $objectManager;
public function __construct()
{
$this->bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$this->objectManager = $this->bootstrap->getObjectManager();
$appState = $this->objectManager->get('Magento\Framework\App\State');
$appState->setAreaCode('frontend');
}
/**
* Row sql query example
*/
public function rawSql()
{
$resource = $this->objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('core_config_data');
$result = $connection->fetchAll("SELECT * FROM {$tableName} WHERE path = 'web/secure/base_url'");
print_r($result);
}
/**
* Get single order from repository
*
* @param $orderId
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getOrder($orderId)
{
/** @var \Magento\Sales\Model\OrderRepository $orderRepository */
$orderRepository = $this->bootstrap->getObjectManager()->get('\Magento\Sales\Api\OrderRepositoryInterface');
$order = $orderRepository->get($orderId);
echo 'getOrder: ' . PHP_EOL . '#' . $order->getIncrementId() . PHP_EOL . PHP_EOL;
// print_r($order->debug());
}
/**
* Get Ordercollection using search criteria
*/
public function searchOrders($orderIds)
{
$orderRepository = $this->bootstrap->getObjectManager()->get('\Magento\Sales\Api\OrderRepositoryInterface');
$searchCriteriaBuilder = $this->bootstrap->getObjectManager()->get('\Magento\Framework\Api\SearchCriteriaBuilder');
$searchCriteria = $searchCriteriaBuilder
->addFilter('entity_id', $orderIds, 'in')->create();
$orders = $orderRepository->getList($searchCriteria);
echo 'searchOrders;' . PHP_EOL;
foreach ($orders as $order) {
echo '#' . $order->getIncrementId() . PHP_EOL;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment