/   /   /  Magento 2 Useful snippets for frontend developers

Note:

For more extensions and themes visit our store

Magento 2 Useful snippets for frontend developers


1 - Infos About Store

Get Current store Id :
inject \Magento\Store\Model\StoreManagerInterface $storeManage

  1.  
  2. $this->storeManager = $storeManager;
  3.  

and get the id with :
  1.  
  2. $id = $this->storeManager->getStore()->getId();
  3.  

get base Url :

  1.  
  2. $baseUrl = $this->storeManager->getStore()->getBaseUrl();
  3.  

you can inject \Magento\Framework\UrlInterface and use use constants of class as parameters (UrlInterface::URL_TYPE_LINK) :
  1.  
  2.     const URL_TYPE_LINK = 'link';
  3.     const URL_TYPE_DIRECT_LINK = 'direct_link';
  4.     const URL_TYPE_WEB = 'web';
  5.     const URL_TYPE_MEDIA = 'media';
  6.     const URL_TYPE_STATIC = 'static';
  7.     const URL_TYPE_JS = 'js';
  8.  

get base currency code  or (getBaseCurrency() , getDefaultCurrencyCode(), getDefaultCurrency()):

  1.  
  2. $baseCurrency = $this->storeManager->getStore()->getBaseCurrencyCode();
  3.  

get current currency code or (getCurrentCurrency(), getAvailableCurrencyCodes(),getAllowedCurrencies()) :

  1.  
  2. $currentCurrency = $this->storeManager->getStore()->getCurrentCurrencyCode();
  3.  

Retrieve base media directory path :

  1.  
  2. $baseMediaDir  = $this->storeManager->getStore()->getBaseMediaDir();
  3.  

Retrieve base static directory path :

  1.  
  2. $baseStaticDir  = $this->storeManager->getStore()->getBaseStaticDir();
  3.  

For other function like website_id , group_id, code name or secure test got to class Magento\Store\Model\Store and explore …

2 – Cart Data

inject Magento\Checkout\Model\Cart $cart

  1.  
  2. $this->cart = $cart;
  3.  

 

get total number items:

  1.  
  2. $cartCount = $this->cart->getItemsCount();
  3.  

- get quote data :

  1.  
  2. $cartQuote= $this->cart->getQuote()->getData();
  3. print_r($cartQuote);
  4.  

you cant get  [base_currency_code]  [store_currency_code]  [quote_currency_code]  [grand_total] or [base_grand_total] and many others see all details inside print_r
  1.  
  2. echo $cartQuote['base_grand_total'];
  3.  

- iterate items of cart from session :
inject Magento\Checkout\Model\Session $session
$this->session  =  $session;

  1.  
  2. foreach ($this->session->getQuote()->getAllItems() as $item) {
  3.     echo $item->getName();
  4. }
  5.  


-clear quote
  1.  
  2. $this->session ->clearQuote();
  3.  

 

3 - Catalog:

inject \Magento\Framework\Registry $registry
$this->registry = $registry;
-get current category :

  1.  
  2. $category = $this->registry->registry('current_category');
  3.  

get cuurent category id:
  1.  
  2. $cid = $this->registry->registry('current_category')->getId();
  3.  

get current category level:
  1.  
  2. $level = $this->registry->registry('current_category')->getLevel();
  3.  

 

-get current product :

  1.  
  2. $pid  =$this->registry->registry('current_product');
  3.  

retrieve crosssell products from current product:
  1.  
  2.         $products = [];
  3.         foreach ($this->registry->registry('current_product')->getCrossSellProducts() as $product) {
  4.             $products[$product->getId()] = ['position' => $product->getPosition()];
  5.         }
  6.         print_r($products);
  7.  

retrieve related products from current product:
  1.  
  2.         $products = [];
  3.         foreach ($this->registry->registry('current_product')->getRelatedProducts() as $product) {
  4.             $products[$product->getId()] = ['position' => $product->getPosition()];
  5.         }
  6.         print_r($products);
  7.  

retrieve upsell products from current product:
  1.  
  2.         $products = [];
  3.         foreach ($this->registry->registry('current_product')->getUpSellProducts() as $product) {
  4.             $products[$product->getId()] = ['position' => $product->getPosition()];
  5.         }
  6.         print_r($products);
  7.  

test if current product is registered:
  1.  
  2.             if ($this->registry->registry('current_product')) {
  3.                 $product = $this->registry->registry('current_product');
  4.             } else {
  5.                 throw new \LogicException('Product is not defined');
  6.             }
  7.  

4 - CMS

inject \Magento\Framework\Registry $registry
$this->registry = $registry;
-get current cms page :

  1.  
  2. $page = $this->registry->registry('cms_page');
  3.  

get id of current cms page:
  1.  
  2. $cmsId = $this->registry->registry('cms_page')->getId();
  3.  

test if cms page exist in table :
  1.  
  2.         if ($this->registry->registry('cms_page')->getId()) {
  3.             return $this->registry>registry('cms_page')->getTitle();
  4.         } else {
  5.             return __('New Page');
  6.         }
  7.  

 

5 - View

inside any block that extend \Magento\Framework\View\Element\Template you can use :

  1.  
  2. //getting assets elements url from your extension in current theme
  3. $loader = $this->getViewFileUrl("ibnab_lazy::images/loader.svg");
  4. //or getting from assets of current theme :
  5. $js = $this->getViewFileUrl("js/my.js");
  6.  

 

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.