0

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)
        {

            ....

        }
    }
}

0

I solved the problem with this:

Base class:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
use \Magento\Framework\AppInterface as AppInterface;
use Magento\Framework\App\State as State;
use Magento\Framework\ObjectManager\ConfigLoaderInterface;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Response\Http as ResponseHttp;

abstract class Magento_Abstract implements AppInterface
{
    public function __construct(
        State $state,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        ResponseHttp $response,
        ConfigLoaderInterface $configLoader
    ) {
        $state->setAreaCode('frontend');
        $this->_state = $state;
        $this->_objectManager = $objectManager;
        $this->_objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
        $this->_response = $response;
        $this->_configLoader = $configLoader;
    }
    public function launch()
    {
        $this->run();
        return $this->_response;
    }
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
    abstract public function run();
}

get products:

class getProducts extends Magento_Abstract
{
    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)
        {
                $product->getId(),
        }
    }
}

get customers:

class getCustomers extends Magento_Abstract
{
    public function run()
    {
        return $this->getCustomersXML();
    }
    private function getCustomersXML()
    {
        $partners = $this->_objectManager->create('\Magento\Customer\Model\Customer');
        $partners = $partners->getCollection()->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->joinAttribute('billing_firstname', 'customer_address/firstname', 'default_billing', null, 'left')
            ->joinAttribute('billing_lastname', 'customer_address/lastname', 'default_billing', null, 'left')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')
            ->joinAttribute('billing_street', 'customer_address/street', 'default_billing', null, 'left')
            ->joinAttribute('shipping_postcode', 'customer_address/postcode', 'default_shipping', null, 'left')
            ->joinAttribute('shipping_city', 'customer_address/city', 'default_shipping', null, 'left')
            ->joinAttribute('shipping_telephone', 'customer_address/telephone', 'default_shipping', null, 'left')
            ->joinAttribute('shipping_region', 'customer_address/region', 'default_shipping', null, 'left')
            ->joinAttribute('shipping_country_id', 'customer_address/country_id', 'default_shipping', null, 'left')
            ->joinAttribute('shipping_street', 'customer_address/street', 'default_shipping', null, 'left');
        foreach($partners as $partner)
        {
                $partner->getId(),
        }
    }
}

get products:

class getProducts extends Magento_Abstract
{
    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)
        {
                $product->getId(),
                $product->getName(),
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.