Category Archives: Magento 2.0 Tutorial
Magento 2 get category image url
Magento 2 get category image url By Id– We can get category url from category object in Magento 2. Here in this article we are going to explain how you can fetch the url and display the image on phtml.
Magento 2 get category image url By Id Example
Here in the below example we have fetched the category url from category object and displayed it on phtml-
Magento 2 get category image url Example:
$categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); $_outputhelper = $this->helper('Magento\Catalog\Helper\Output'); $_imgHtml = ''; if ($_imgUrl = $category->getImageUrl()) { $_imgHtml = ''; $_imgHtml = $_outputhelper->categoryAttribute($category, $_imgHtml, 'image'); echo $_imgHtml; } |
Magento 2 set session timeout
Magento 2 set session timeout– We can set the session timeout in Magento from Backend Configuration settings for frontend and backend.
Magento 2 set frontend session timeout Frontend | Backend Example
You can set frontend and backend timeout from backend settings. Go to the below settings and set Session Lifetime –
Admin Session Timeout
Using this setting we can increase the admin user login time.
Go to Stores->Settings->Configuration->Advanced->Admin->Security->Admin Session Lifetime
Now add value in seconds in Admin Session Lifetime setting.
Frontend Session Timeout
We can increase the frontend user login time using this setting.
Go to Stores->Settings->Configuration->Advanced->Admin->Security->Frontend Session Lifetime
Now add value in seconds in Frontend Session Lifetime setting.
Magento 2 Create Coupon Code Programmatically
Magento 2 Create Coupon Code Programmatically It is very simple to create coupon code programmatically in magento2. Here in this article we are going to explain how you can create coupons in Magento2.
Magento 2 Create Coupon Code Programmatically | Script | Example
You can create coupon code simply as below-
Inject Rule Factory in Constructor
Magento 2 Create Coupon Code Programmatically Example:
protected $ruleFactory public function __construct(\Magento\SalesRule\Model\RuleFactory $ruleFactory) { $this->rulesFactory = $ruleFactory } |
Create Coupon Code
Now use the below code to create coupon programmatically-
Create Coupon Programmatically Example:
$ruleData = [ "name" => "Custom Discount", "description" => "Get Custom Discount", "from_date" => null, "to_date" => null, "uses_per_customer" => "0", "is_active" => "1", "stop_rules_processing" => "0", "is_advanced" => "1", "product_ids" => null, "sort_order" => "0", "simple_action" => "fixed_amount", "discount_amount" => "50", "discount_qty" => null, "discount_step" => "3", "apply_to_shipping" => "0", "times_used" => "0", "is_rss" => "1", "coupon_type" => "COUPON", "use_auto_generation" => "0", "uses_per_coupon" => "0", "simple_free_shipping" => "0", "customer_group_ids" => [0, 1, 2, 3], "website_ids" => [1], "coupon_code" => 'FLAT_50', "store_labels" => [], "conditions_serialized" => '', "actions_serialized" => '' ]; $ruleModel = $this->ruleFactory->create(); $ruleModel->setData($ruleData); $ruleModel->save(); |
Magento 2 Collection get last Item
Magento 2 Collection get last Item: There are many ways to get last item from Magento collection. Here in this article we are going to explain one of the method to get last item from collection using order by and setPageSize.
Magento 2 Collection get last Item Example
You can add order by desc and setPageSize to get the last item from the collection. Here is an example to get last item-
Magento 2 Collection get last Item Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $firstItem = $objectManager->create('Mypackage\MyModule\Model\MyData') ->getCollection() ->setOrder('id DESC') ->setPageSize('1'); |
The above example will give last item so on the same way you can get the first item by just adding ->setOrder(‘id ASC’)
Magento 2 get Store Details
Magento 2 get Store Details– It is very simple to get the store details in Magento 2. There are two ways to get the store information in Magento 2 – 1. Using dependency Injection, 2. Using Object Manager. Here in this post we are going to get the – store id, store code, store name, store website, store url etc.
Magento 2 get Store Details | Id, Name ,Code
Let us get the following details from current store in Magento2-
Get Information From Current Store
We need following details about current store while working with magento.
- Store Name
- Store Id
- Store Code
- Store Website Id
- Store Url
We can get the above information using object manager. Here is simple Example.
Get Store Information Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $storeName = storeManager->getStore()->getName(); $storeId = $storeManager->getStore()->getStoreId(); $websiteId = $storeManager->getStore()->getWebsiteId(); $storeCode = $storeManager->getStore()->getCode(); $storeUrl = $storeManager->getStore()->getStoreUrl(); |
So on the same way you can get other details of the current store.
Magento 2 get current Category Name
Magento 2 get current Category Name We can get current Category name by category object. Using object manager we can get current category details such as Category Name, Id etc.
How to get current Category Name,Id In Magento 2?
We can get currenct category details from registry simply as below-
Get Current category Name, Id Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); echo $category->getName(); echo " |
Magento 2 Get Current Currency Symbol
Magento 2 Get Current Currency Symbol & Currency Code– We can get currency symbol using currency code so we first need to get current currency code from currency object then get currency symbol. Here in this tutorial, we are going to explain how you can get .
Magento 2 Get Current Currency Symbol & Currency Code Example
You can get Currenct Currency Code and Symbol simply as below-
How to get currency Symbol in Magento 2?
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); $currencyCode = $storeManager->getStore()->getCurrentCurrencyCode(); $currency = $objectManager->create('Magento\Directory\Model\CurrencyFactory')->create()->load($currencyCode); echo $currencySymbol = $currency->getCurrencySymbol(); |
So using above example we can get currency code and currency symbol.
Magento 2 get child categories
Magento 2 get child categories of specific category – We can get all sub categories of the specific category in Magento2. Sub categories can be get from parent category. Here in this article how we can get child categories by parent category id,we will also cover how to get current category’s child/sub categories in Magento 2.
Magento 2 get child categories Example
You can get child categories simply as below. Let us go one by one-
Get Sub Categories By Id
You can get sub categories simply as below-
Get Sub Categories Example:
$categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); foreach ($category->getChildCategories() as $child) { echo $child->getId(); } |
Get Child Categories From Current Category
You can get child categories from current category simply as below-
Get Sub Categories Example:
/* $block \Magento\Catalog\Block\Category\View */ foreach ($block->getCurrentCategory()->getChildCategories() as $child) { echo $child->getId(); } |
So the above example will give you the child categories of current category.
Magento 2 get parent categories
Magento 2 get parent categories We can use dependency injection or object manager to get all parent categories. Here in this article we are going to create one example in which we will get all parent categories using object manager.
Magento 2 get parent categories Example
Using object Manager You can get all parent categories simply below-
Magento 2 get parent categories Example:
$categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); foreach ($category->getParentCategories() as $parent) { echo $parent->getId(); } |
The above example will return all parent categories.