Wednesday 16 December 2015

Magento 2 - Get Category, Products and Customer data using outside script (Programatically get & edit data)

First Create scripts/abstract.php file and add below code

<?php
use \Magento\Framework\AppInterface as AppInterface;
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;
use Magento\Framework\App\State as State;

abstract class AbstractApp implements AppInterface

{
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        Event\Manager $eventManager,
        AreaList $areaList,
        RequestHttp $request,
        ResponseHttp $response,
        ConfigLoaderInterface $configLoader,
        State $state,
        Filesystem $filesystem,
        \Magento\Framework\Registry $registry
    ) {
        $this->_objectManager = $objectManager;
        $this->_eventManager = $eventManager;
        $this->_areaList = $areaList;
        $this->_request = $request;
        $this->_response = $response;
        $this->_configLoader = $configLoader;
        $this->_state = $state;
        $this->_filesystem = $filesystem;
        $this->registry = $registry;
    }

    public function launch()

    {
        $this->run();
        return $this->_response;
    }

    abstract public function run();


    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)

    {
        return false;
    }
}
?>

For category Data:

create scripts/categoryData.php and add below code for get category data and edit it programetically.

<?php
require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class CategoriesDataApp extends AbstractApp
{

    public function run()
    {
        $this->_objectManager->get('Magento\Framework\Registry')
            ->register('isSecureArea', true);

        $category = $this->_objectManager->create('\Magento\Catalog\Helper\Category');
        $_categories = $category->getStoreCategories();
        foreach($_categories as $cat){
            $_category = $this->_objectManager->create('\Magento\Catalog\Model\Category');
            $_category = $_category->load($cat->getId());
            $_subcategories = $_category->getChildrenCategories();
            if (count($_subcategories) > 0){
                echo $_category->getId().'=>'.$_category->getName().'<br/>';      
                foreach($_subcategories as $_subcategory){
                     echo "sub category: ".$_subcategory->getId().'=>'.$_subcategory->getName().'<br/>';
                }
            }
        }
        
        /* for load Specific category uncomment below code */
        //$_category = $this->_objectManager->create('\Magento\Catalog\Model\Category');
        //$_category = $_category->load(4); // your category Id
        //echo '<pre>';print_r($category->getData());
        //$category->delete();
    }
}

/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CategoriesDataApp');
$bootstrap->run($app);
?>

Then just run command like php scripts/categoryData.php or direct run categoryData.php in your browser.

For Product Data:

create scripts/productData.php and add below code for get category data and edit it programetically.

<?php
require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class ProductsDataApp extends AbstractApp
{

    public function run()
    {
        $this->_objectManager->get('Magento\Framework\Registry')
            ->register('isSecureArea', true);

        $products = $this->_objectManager->create('\Magento\Catalog\Model\Product');
        
        $products = $products->getCollection()
                    ->addAttributeToFilter('type_id', 'simple')
                    ->addAttributeToSelect('*')
                    ->load();
         foreach($products as $product)
         {
            echo $product->getId().'=>'.$product->getName().'=>'.$product->getData('price').'<br/>';
            
            $_product = $this->_objectManager->create('\Magento\Catalog\Model\Product')->loadByAttribute('entity_id',$product->getId());
            
         }
        
    }
}

/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('ProductsDataApp');
$bootstrap->run($app);
?>

Then just run command like php scripts/productData.php or direct run productData.php in your browser.

For Customer Data:

create scripts/customerData.php and add below code for get category data and edit it programetically.

<?php
require dirname(__FILE__) . '/../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/abstract.php';

class CustomersDataApp extends AbstractApp
{

    public function run()
    {
        $this->_objectManager->get('Magento\Framework\Registry')
            ->register('isSecureArea', true);

        $customers = $this->_objectManager->create('\Magento\Customer\Model\Customer');
        $customers = $customers->getCollection();
        
        foreach($customers as $customer)
        {
            //echo '<pre>';print_r($customer->getData());
            echo $customer->getId().'=>'.$customer->getName().'<br/>';    
            //$_customer = $this->_objectManager->create('\Magento\Customer\Model\Customer')->load($customer->getId()); /// Customer Object for change data or load Customer
        }
        
    }
}

/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CustomersDataApp');
$bootstrap->run($app);
?>

Then just run command like php scripts/customerData.php or direct run customerData.php in your browser.

Hope this will help you, Thank you.