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('Magento\Framework\Pricing\Helper\Data')->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 \Magento\Checkout\Model\Session
*/
protected $_checkoutSession;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Sales\Model\Order $salesOrderFactory
* @param \Magento\Checkout\Model\Session $checkoutSession
*/
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Model\Order $salesOrderFactory,
        \Magento\Checkout\Model\Session $checkoutSession,
        array $data = []
    ) {
        $this->_checkoutSession = $checkoutSession;
        parent::__construct($context, $data);
    }

/**
* Retrieve current order
*
* @return \Magento\Sales\Model\Order
*/
public function getOrder()
{
   $orderId = $this->_checkoutSession->getLastOrderId();
   return $this->_salesFactory->load($orderId);
}

Or

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $orderId = $objectManager->create('\Magento\Checkout\Model\Session')
                            ->getLastOrderId();       
 $order = $objectManager->create('\Magento\Sales\Model\Order')
                            ->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 = \Magento\Framework\App\ObjectManager::getInstance();
 $product = $objectManager->create('\Magento\Catalog\Model\Product')
                            ->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 = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Framework\App\Request\Http')
                            ->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(
       .....
       \Magento\Search\Helper\Data $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.

One thought on “Magento 2 quick tips and tricks

  1. Great post! How did you figure this out?
    Any chance you know how to escape this one?:

    When importing a csv file using pipe character (|) as delimiter, i have additional_attributes, which are separated by commas as required(?). But one of the additional_attributes is a text blurb that contains commas. I tried to wrap that attribute in double-quotes, tried backslash or is it forwardslash? Nothing works. Any ideas on how to do this?

    Oh, this is using the Admin System > Import

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.