Magento 2 quick tips and tricks

magento 2 tips and tricks

As you might know our team is busy creating Magento 2 extensions and progress can be seen by clicking on the following link http://www.scommerce-mage.co.uk/extensions-for-magento-2.html. Also if you have any Magento 2 extension in mind then please feel free to send us your idea to us via email (core@scommerce-mage.co.uk) and if we like your idea then we might either do your extension at reasonable rate or for free 🙂

As part of extension building, we have been finding a lot of new and interesting things in Magento 2 which we would like to share with the community. We will keep on adding stuff to this page so don’t forget to bookmark it 🙂

Javascript quote escape in Magento 2

In Magento 1, you can escape the quotes in javascript using the following code snippet

$this->jsQuoteEscape ($item->getName());

But in Magento 2, you can do the same using the following code snippet

$this->escapeJsQuote($item->getName());

Escape site URL in Magento 2

In Magento 1, you can escape the site URL using the following code snippet

Mage::getSingleton('core/url')->escape($_SERVER['REQUEST_URI'])

But in Magento 2, you can do the same using the following code snippet

$this->escapeJsQuote($_SERVER['REQUEST_URI'])

Convert number into currency format in Magento 2

In Magento 1, you can convert number into currency format using the following code snippet

$this->helper('core')->currency(number_format(50,2),true,false)

But in Magento 2, you can do the same using the following code snippet

$this->helper('MagentoFrameworkPricingHelperData')->currency(number_format(50,2),true,false);

Retrieve order object on order confirmation page in Magento 2

In Magento 1, you can retrieve order object on success.phtml page using the following code snippet

$orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getModel('sales/order')->load($orderId); 

But in Magento 2, you can do the same using the following code snippet

/**
* Checkout session
*
* @var MagentoCheckoutModelSession
*/
protected $_checkoutSession; /**
* @param MagentoFrameworkViewElementTemplateContext $context
* @param MagentoSalesModelOrder $salesOrderFactory
* @param MagentoCheckoutModelSession $checkoutSession
*/
public function __construct( MagentoFrameworkViewElementTemplateContext $context, MagentoSalesModelOrder $salesOrderFactory, MagentoCheckoutModelSession $checkoutSession, array $data = [] ) { $this->_checkoutSession = $checkoutSession; parent::__construct($context, $data); } /**
* Retrieve current order
*
* @return MagentoSalesModelOrder
*/
public function getOrder()
{ $orderId = $this->_checkoutSession->getLastOrderId(); return $this->_salesFactory->load($orderId);
}

Or

 $objectManager = MagentoFrameworkAppObjectManager::getInstance(); $orderId = $objectManager->create('MagentoCheckoutModelSession') ->getLastOrderId(); $order = $objectManager->create('MagentoSalesModelOrder') ->load($orderId); 

Retrieve product object in Magento 2

In Magento 1, you can retrieve product object page using the following code snippet

 $product = Mage::getModel('catalog/product') ->load(291); 

But in Magento 2, you can do the same using the following code snippet

 $objectManager = MagentoFrameworkAppObjectManager::getInstance(); $product = $objectManager->create('MagentoCatalogModelProduct') ->load(291); 

Retrieve request object in Magento 2

In Magento 1, you can retrieve request object using the following code snippet

$productId = Mage::app()->getRequest()->getParam('product', 0);

But in Magento 2, you can do the same using the following code snippet

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product = $objectManager->create('MagentoFrameworkAppRequestHttp') ->getParam('product', 0); 

Search Text in Magento 2

In Magento 1, you can retrieve search query text

$this->helper('catalogsearch')->getEscapedQueryText();

But in Magento 2, you can do the same using the following code snippet

public function __construct( ..... MagentoSearchHelperData $searchData .....) { ...... $this->_searchData = $searchData; parent::__construct($context, $data); } public function getEscapedQueryText() { return $this->_searchData->getEscapedQueryText(); }

Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Discover more from WHO WILL CARE eCommerce

Subscribe now to keep reading and get access to the full archive.

Continue reading