Archive for the ‘Magento 2.0’ Category

Magento url redirect from a Controller

  <?php   namespace Imran\Test\Controller\Index;   use \Magento\Framework\App\Action\Action;   class Index extends Action { /** * @var \Magento\Framework\View\Result\Page */ protected $resultPageFactory;   /** * @param \Magento\Framework\App\Action\Context $context */ public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory) { parent::__construct($context); $this->resultPageFactory = $resultPageFactory; $this->resultRedirectFactory->create()->setUrl(’https://www.someurl.com’); } }

Magento2 post install permissions

find /var/www/magento2 -type f -print0 | xargs -r0 chmod 640 find /var/www/magento2 -type d -print0 | xargs -r0 chmod 750 chmod -R g+w /var/www/magento2/{pub,var}

Magento2 db config

Magento 2 keeps db configuration in app/etc/env.php

Magento 2 manually installing module

1. Run the following: mkdir $MAGENTODIR/app/code/vendorname/modulename cd $MAGENTODIR bin/magento module:status output: List of disabled modules: Vendorname_Modulename 2. Follow by: bin/magento module:enable Vendor_Module bin/magento setup:upgrade 3. If asks for compile do the following: bin/magento setup:di:compile

Magento2 Json Controller Example

<?php   namespace Imran\Test\Controller\Index;   use \Magento\Framework\App\Action\Action;   class Index extends Action { /** @var \Magento\Framework\View\Result\Page */ protected $resultPageFactory; /** * @param \Magento\Framework\App\Action\Context $context */ public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory) { parent::__construct($context); $this->resultJsonFactory = $resultJsonFactory; }   /** * Blog Index, shows a list of recent blog posts. * * @return \Magento\Framework\View\Result\JsonFactory */ public […]

Magento2 Select from Database

<?php … $subscribe = $this->_objectManager->create(’Imran\Test\Model\Subscriber’);   // Get by ID $item = $subscribe->load(19); echo $item->getName();   // Get by Email $subs = $subscribe->getCollection()->addFieldToFilter(’email’, ‘aa@bb.com’);   foreach ($subs as $sub) { var_dump($sub->getData()); } …

Magento2 Select Single Record From Db

<?php … $subscribe = $this->_objectManager->create(’Imran\Test\Model\Subscriber’);   // Method 1 $subs = $subscribe->getCollection()->addFieldToFilter(’email’, ‘aa@bb.com’); var_dump($subs->getFirstItem()->getData());   // Method 2 $subs = $subscribe->getCollection() ->setPageSize(1) ->addFieldToFilter(’email’, ‘aa@bb.com’);   foreach ($subs as $sub) { var_dump($sub->getData()); } …

Magento2 Design Customize

Design customizations kept under: app/design/Vendor/theme/Vendor_Modulename/web/css app/design/Vendor/theme/Vendor_Modulename/layout app/design/Vendor/theme/Vendor_Modulename/templates

Magento2 disable WYSIWYG Cms Editor

Go to Stores -> Configuration -> Content Management -> Enable WYSIWYG Editor: Disabled by Default / Disabled Completely

Magento2 db schema upgrade

1. change setup_version in app/code/Vendor/Module/etc/module.xml 2. run bin/magento setup:db-schema:upgrade