Whether you are a Magento newbie or an experienced Magento developer, when you are working on any project, you come across many small issues. This blog is designed to share such small Magento Tips and Tricks which are useful to everyone who are working on Magento 2 development.
1. Javascript quote escape
Escape the quotes in javascript using the following code snippet:
$this->jsQuoteEscape ($item->getName());
2. Escape site URL
Escape the site URL using the following code snippet:
$this->escapeJsQuote($_SERVER[‘REQUEST_URI’])
3. Change number into currency format
Convert number into currency format using the following code snippet:
$this->helper(‘Magento\Framework\Pricing\Helper\Data’)->currency(number_format(50,2),true,false);
4. Restore the order object on order confirmation page
Retrieve order object on success .phtml page 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 you can do by other code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderId = $objectManager->create(‘\Magento\Checkout\Model\Session’)
->getLastOrderId();
$order = $objectManager->create(‘\Magento\Sales\Model\Order’)
->load($orderId);
5. Retrieve product object
Retrieve product object page using the following code snippet:
$objectManager =
\Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create(‘\Magento\Catalog\Model\Product’)
->load(291);
6. Retrieve request object
You can retrieve request object using the following code snippet:
$objectManager =
\Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create(‘\Magento\Framework\App\Request\Http’)
->getParam(‘product’, 0);
Hope that these tips may help you run out the stuck from Magento 2. As a best magento development company you can find more tips and tricks for Magento 2 , please visit and get them.