I want to make a PHP file to get Orders and Customers from a Magento 2 based webshop (like exporting). I have a full code for Magento 1, but for M2 I have tried a lot of things.
I also needed this function for Products but now I have this.
If anybody can add a full code for Orders and Customers I would be glad, if I found one I will paste it.
Here is the code to get products, base on this answer: https://magento.stackexchange.com/a/91193/34384
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
\Magento\Framework\Profiler::start('session_start');
use \Magento\Framework\AppInterface as AppInterface;
use Magento\Framework\App\State as State;
use \Magento\Framework\App\Http as Http;
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Event;
use Magento\Framework\Filesystem;
use Magento\Framework\App\AreaList as AreaList;
abstract class Magento_ClearAdmin_Abstract_getProducts implements AppInterface
{
public function __construct(
State $state,
\Magento\Framework\ObjectManagerInterface $objectManager,
Event\Manager $eventManager,
AreaList $areaList,
RequestHttp $request,
ResponseHttp $response,
ConfigLoaderInterface $configLoader,
Filesystem $filesystem,
\Magento\Framework\Registry $registry
) {
$state->setAreaCode('frontend');
$this->_state = $state;
$this->_objectManager = $objectManager;
$this->_objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
$this->_eventManager = $eventManager;
$this->_areaList = $areaList;
$this->_request = $request;
$this->_response = $response;
$this->_configLoader = $configLoader;
$this->_filesystem = $filesystem;
$this->registry = $registry;
}
public function launch()
{
$this->run();
return $this->_response;
}
public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
{
return false;
}
abstract public function run();
}
class getProducts extends Magento_ClearAdmin_Abstract_getProducts
{
public function run()
{
return $this->getProductsXML();
}
private function getProductsXML()
{
$products = $this->_objectManager->create('\Magento\Catalog\Model\Product');
$products = $products->getCollection()->addAttributeToSelect('name');
foreach($products as $product)
{
....
}
}
}