inject \Magento\Store\Model\StoreManagerInterface $storeManage
$this->storeManager = $storeManager;
$id = $this->storeManager->getStore()->getId();
$baseUrl = $this->storeManager->getStore()->getBaseUrl();
$baseCurrency = $this->storeManager->getStore()->getBaseCurrencyCode(); //get base currency code
$baseCurrency = $this->storeManager->getStore()->getBaseCurrency();
$baseCurrency = $this->storeManager->getStore()->getDefaultCurrencyCode();
$baseCurrency = $this->storeManager->getStore()->getDefaultCurrency();
$currentCurrency = $this->storeManager->getStore()->getCurrentCurrencyCode(); //get current currency code
$currentCurrency = $this->storeManager->getStore()->getCurrentCurrency();
$currentCurrency = $this->storeManager->getStore()->getAvailableCurrencyCodes();
$currentCurrency = $this->storeManager->getStore()->getAllowedCurrencies();
// To convert currency
print_r($this->currency->getCurrencyRates($this->currency->getConfigBaseCurrencies(), $this->currency->getConfigAllowCurrencies()));
$baseMediaDir = $this->storeManager->getStore()->getBaseMediaDir();
$baseStaticDir = $this->storeManager->getStore()->getBaseStaticDir();
inject Magento\Checkout\Model\Cart $cart
$this->cart = $cart;
$cartCount = $this->cart->getItemsCount();
##### Get quote data #####
$cartQuote= $this->cart->getQuote()->getData();
##### clear quote #####
```php
iterate items of cart from session :
inject Magento\Checkout\Model\Session $session
$this->session = $session;
foreach ($this->session->getQuote()->getAllItems() as $item) {
echo $item->getName();
}
$this->session ->clearQuote();
inject \Magento\Framework\Registry $registry
$this->registry = $registry;
$category = $this->registry->registry('current_category');
$cid = $this->registry->registry('current_category')->getId();
$level = $this->registry->registry('current_category')->getLevel();
$pid =$this->registry->registry('current_product');
$products = [];
foreach ($this->registry->registry('current_product')->getCrossSellProducts() as $product) {
$products[$product->getId()] = ['position' => $product->getPosition()];
}
print_r($products);
$products = [];
foreach ($this->registry->registry('current_product')->getRelatedProducts() as $product) {
$products[$product->getId()] = ['position' => $product->getPosition()];
}
print_r($products);
$products = [];
foreach ($this->registry->registry('current_product')->getUpSellProducts() as $product) {
$products[$product->getId()] = ['position' => $product->getPosition()];
}
print_r($products);
if ($this->registry->registry('current_product')) {
$product = $this->registry->registry('current_product');
} else {
throw new \LogicException('Product is not defined');
}
inject \Magento\Framework\Registry $registry $this->registry = $registry;
$page = $this->registry->registry('cms_page');
$cmsId = $this->registry->registry('cms_page')->getId();
if ($this->registry->registry('cms_page')->getId()) {
return $this->registry>registry('cms_page')->getTitle();
} else {
return __('New Page');
}
inside any block that extend \Magento\Framework\View\Element\Template you can use :
//getting assets elements url from your extension in current theme
$loader = $this->getViewFileUrl("ibnab_lazy::images/loader.svg");
//or getting from assets of current theme :
$js = $this->getViewFileUrl("js/my.js");
Convert number into currency format using the following code snippet:
$this->helper('Magento\Framework\Pricing\Helper\Data')- >currency(number_format(50,2),true,false);
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);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Catalog\Model\Product')
->load(291)
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('\Magento\Framework\App\Request\Http')
->getParam('product', 0);
namespace Excellence\Hello\Controller\Index;
use Magento\Framework\Controller\ResultFactory;
class Actionname name extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl()); // Previous URL
$resultRedirect->setPath('excellence/index/add'); // URL path
return $resultRedirect;
}
}
return $this->resultRedirectFactory->create()->setPath('excellence/index/add/', ['_current' => true]);
If we use [‘_current’ => true] then the objects being passed via query string will remain in the redirected URL but if we don’t use [‘_current’ => true] or use [‘_current’ => false], then objects will not be passed in the redirected URL.
// URL Builder:
$this->_urlBuilder->getUrl("excellence/index/add/")
// To read data passed via get Method we simply use following two methods:
$this->getRequest()->getParams()
// this will read all the get data but to read any specific data we use
$this->getRequest()->getParam('data');
// To read data passed via POST method, we use following method:
$this->request->getPost()
this will read all the the data being passed via post. But if we want to read specific data then we will use
// $this->getRequest()->getPost('data');
$post = $this->getRequest()->getPostValue();
// The above methods will work if you are trying this from a controller that extends Magento\Framework\App\Action\Action you can get the request. In other cases you need to inject request in the constructor as:
class ClassName
{
protected $request;
public function __construct(
\Magento\Framework\App\Request\Http $request,
....//rest of parameters here
) {
$this->request = $request;
...//rest of constructor here
}
public function getPost()
{
return $this->request->getPost();
}
}