Get your Ecommerce flexibility using Magento 2
Magento 2 is the newest member of the Magento family, an open-source platform to accelerate your e-commerce goals. It has new and improved features that helps to increase user interaction and perform all business functions smoothly.
It basically integrates object-oriented programming with Relational DataBase Management System, while providing a Zend Framework in the background.
Just follow some simple steps described below to get site URL’s and all the category, products, and customers data right on your screen.
1. Programmatically Get URLs in Magento
Step 1:
The initial step is to Create scripts/magentoUrl.php file.
Now place this file in the Magento root directory and just enter the code displayed below.
<?php require dirname(__FILE__) . '/../app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); // For Site Base url $baseUrl= $storeManager->getStore()->getBaseUrl(); // For Link url: $linkUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK); //For Media url: $mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); //For Static Folder url: $staticUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC); echo “Site URL: ”.$baseUrl.”<br>”; echo “Link url: ”.$linkUrl.”<br>”; echo “Media URL: ”.$mediaUrl.”<br>”; echo “Static Folder URL: ”.$staticUrl.”<br>”; ?>
2. Programmatically Get Category, Products and Customer data in Magento 2
With Magento 2 you can organise and administer your business data. Moreover its scalability feature allows you to make changes effortlessly.
Follow these simple steps given below to get data related to category, products and customers.
Step 1: First Create the scripts/abstract.php file in magento root directory. Place the following code in the file.
<?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; } } ?>
Step 2: To avail the category Data
Follow these subsequent steps
Step 2.1: Create scripts/categoryData.php file in magento root directory and place the code below to get category data.
<?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().' '; foreach($_subcategories as $_subcategory){ echo "sub category: ".$_subcategory->getId().'=>'.$_subcategory->getName().' '; } } } /* for load Specific category uncomment below code */ //$_category = $this->_objectManager->create('\Magento\Catalog\Model\Category'); //$_category = $_category->load(4); // your category Id //echo '
';print_r($category->getData()); // for get category data //$category->delete(); // for delete category } } /** @var \Magento\Framework\App\Http $app */ $app = $bootstrap->createApplication('CategoriesDataApp'); $bootstrap->run($app); ?>
Step 2.2: Run the command http://www.yourdomian.com/scripts/categoryData.php in your browser. This will provide you all the data related to category right on your screen.
Step 3: For Product Data
Follow the display of all information related to products follow these easy steps.
Step 3.1: Create scripts/productData.php file in Magento root directory and place the code shown below in that file.
<?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') // for filter simple product (remove it if want to all products) ->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); ?>
Step 3.2: Run productData.php command in your browser. Your screen will be displayed with All products related data.
Step 4: For Customer Data:
Step 4.1: Create scripts/customerData.php file in magento root directory and put below code to get all the Customer data.
<?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); ?>
Step 4.2: Run http://www.yourdomian.com/scripts/customerData.php command in your browser..All the customer data will be presented on your screen.
Hope this will help you!
Thank you.