Useful Magento 2 Development Tricks To Build Better Online Store

We all know Magento 1 has deprecated and Magento 2 migration has become mandatory for all Magento store owners. That is definite that some Magento developers are facing new development problems as Magento 2 has come with most big updates and some things have become complex. In this case, we got you some useful Magento 2 development tricks which will help you to fasten your development plus help you build an exclusive eCommerce store.

Tip/Trick 1: Escape javascript quote

There might be a need where you want to use javascript which might contain multiple quotes single and double-quotes. And these quotes need to be escaped and it can be achieved with below function.
$this->jsQuoteEscape ($item->getName());

Tip/Trick 2: Escape website url

You can escape the site url using the below function.
$this->escapeJsQuote($_SERVER['REQUEST_URI'])

Tip/Trick 3: Get request object

You can retrieve the request object with the below code snippet.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Framework\App\Request\Http')
                           ->getParam('product', 0);

Tip/Trick 4: Get product object

At any instance of the project you might need to get product information and manipulate data. Magento 2 provides a very easy to go option to fetch product information. Below is the code snippet to get the task done. You just have to provide product id to the load function.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\Product')
                          ->load(291);

Tip/Trick 5: Format number into currency

You can format numbers as currency using the below code snippet.
$this->helper('Magento\Framework\Pricing\Helper\Data')-  >currency(number_format(50,2),true,false);

Tip/Trick 6: Get order object on the confirmation page

On the confirmation page you might need to show all order details and confirm with the customer. With the below code snippet you can get the order object on .phtml page.
/**
* 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);

Above Magento 2 development tips and tricks might help you to do better coding. If you want to hire dedicated and certified Magento 2 developer for your web store standard development, do reach us by dropping an email at info@adorncommerce.com or contact us. We will be more than happy to assist you and help you to get more success.

Other Posts