Category Archives: Magento
Magento get quote details
Magento get quote details : Magento quote is basically an offer for user if it is accepted by user it converts to order. If you are working with magento quote you need various thing to work on such as quote items, quote total, shipping method etc. We are going to explain the quote details with example.
Magento get quote details
Here are details about the magento get quote details.
First of all let us get quote session. Here you have to option whether you want to get quote session on front or on magento admin. We will go through both in this tutorial.
Get magento Quote Session
If you are working with magento front end use the following method to get Quote Session.
Get magento Quote Session
$quote = Mage::getSingleton('checkout/session')->getQuote(); |
If you are working with magento admin Quote. Use the below method to get the quote session-
Get magento admin Quote Session
Get magento admin Quote Session
$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); |
Once the quote is retrieved either from front end or admin we will access other attributes in the same way.
Magento get quote id
You can get Quote Id as below –
magento get quote id from session
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use Mage::getSingleton('adminhtml/session_quote') for admin quote $quoteId = $quote->getId(); |
Magento Get Quote Items
You can get quote items on both frontend and backend as below-
Get magento Quote Session
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); //use for admin quote $quoteItems = $quote->getAllVisibleItems(); foreach ($quoteItems as $item) { $productId = $item->getProductId(); $productName = $item->getProductName(); } |
Get Payment Method From Magento Quote Session
You can get Payment Detail as below –
Get Payment Method From Magento Quote Session
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $quote = Mage::getModel('sales/quote')->load($quote->getQuoteId()); $paymentCode = $quote->getPayment()->getMethodInstance()->getCode(); $paymentTitle = $quote->getPayment()->getMethodInstance()->getTitle(); |
Get Shipping Method From Magento Quote Session
You can get Shipping Method as below –
Get Shipping Method From Magento Quote Session
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $shippingMethod = $quote->getShippingAddress()->getShippingMethod(); |
Magento Get Shipping And Billing Address Quote Session
You can get Shipping And Billing Address as below –
Magento Get Shipping And Billing Address Quote Session
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $shippingAddress = $quote->getShippingAddress(); $billingAddress = $quote->getBillingAddress(); |
Magento get quote shipping amount
You can get Shipping Amount as below –
Magento get quote shipping amount
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $shippingAmount = $quote->getShippingAddress()->getShippingAmount(); |
Magento get Quote Grand Total
You can get Grand Total as below –
Magento get Quote Grand Total
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $grandTotal = $quote->getGrandTotal(); |
Magento get Quote Tax
You can get Tax as below –
Magento get Quote Tax
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $tax = $quote->getShippingAddress()->getData('tax_amount'); |
Magento get Quote Discount Amount
Magento get Quote Discount Amount
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $totals = $quote->getTotals(); $totalDiscount = $totals["discount"]->getValue(); |
For ‘subtotal’, ‘shipping’, ‘tax’, ‘discount’ and ‘grand_total’ You can use following syntax to get the values –
$quote = Mage::getSingleton('checkout/session')->getQuote(); //$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); // use this for admin quote $totals = $quote->getTotals(); $totalDiscount = $totals["discount"]->getValue(); $tax = $totals["tax"]->getValue(); $subtotal = $totals["subtotal"]->getValue(); $shipping = $totals["shipping"]->getValue(); $grandTotal = $totals["grand_total"]->getValue();
Magento Get Form Key
Magento Get Form Key : Magento form keys are very important when you are working with forms or working on admin side. If you working with forms either on front end of back end form key is required and it’s supposed to be valid key for that instance. These keys expires so you need to keep the updated form key to perform operations.
Magento Get Form Key Syntax
Here is syntax to get form key
Form Key Syntax
Mage::getSingleton('core/session')->getFormKey(); |
The above syntax will give you the updated form key from the magento core session.
When you are working with form key you get most common error message ie. invalid form key.
Invalid Form Key In Magento
This error occurs when you are using an outdated key, it says you to refresh page. Check that you are using the updated key if not then to overcome with this problem use the above session Mage::getSingleton(‘core/session’)->getFormKey(); to get updated key.
Magento Admin Secret Key
If you are working with magento admin for accessing each url you need to generate form key for each url. You can’t access any url without secret key in magento.
Let us add screct key to urls in magento.
Magento add secret key to urls
Here is syntax to append form key in admin-
Secrect Key to Urls
echo Mage::helper("adminhtml")->getUrl("admin/sales_order_create/index"); |
The above syntax will generate url with secrect key.
www.example.com/index.php/admin/sales_order_create/index/key/c969fe5ee307932aa4569607ae7e5200/
Magento get current category details
Magento get current category details in 1.x and 2.x both version: Here in this tutorial, we are going to cover all details about the current category such as category name, category id, category url etc. We will explain how to get details in Magento 1 and Magneto 2. Here are the things which we will cover in this tutorial.
- Id
- Name
- Url
- Description
- Level
- Image Url
- Thumbnail Image Url
- Product Collection
Magento get current category Details | Magento 2
There are followings conditions to get category details –
1 – You want to get current selected category detail.
2 – Or You want load category detail by Category Id.
Let us start with above both conditions –
Get Details in Magento 2
In Magento 2 you can get the details simly as below-
Magento 2 get current category Id
You can use object managet to get the current category id in Magento 2.
Get current Category Id In Magento 2 :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category $catId = $category->getId(); |
Get Details in Magento 1
In Magento 1 you can get the details simly as below-
Magento get current category Id
Syntax to get current Category Id :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); |
Magento Load Category By Id
Now we will use the above category id to load category details and other information-
Load Category By Id :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($catId); |
Now you can get category details such as name, url etc.
Magento get category Name By Category Id
Syntax to get Category Name Id :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($catId); $catName = $category->getName(); |
Magento get category Url By Category Id
Syntax to get Category Url :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($catId); $catUrl = $category->getUrl(); |
Magento get category Description By Category Id
Syntax to get Category Description :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($catId); $catDesc = $category->getDescription(); |
Magento get category Level By Category Id
Syntax to get Category Level :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($catId); $catLevel = $category->getLevel(); |
Magento get category Image Url By Category Id
Syntax to get Category Image Url :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($catId); $catImage = $category->getImageUrl(); |
Magento get category Thumbnail Image Url By Category Id
Syntax to get Category Thumbnail Image Url :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($catId); $catThumbnail = $category->getThumbnail(); |
Magento Load Category Products Collection
If you want to load products in individual category by id you can get all products as below .
Magento Load Category Products Collection By Category Id
Category Products Collection :
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $categoryProducts = Mage::getModel('catalog/category')->load($catId) ->getCollection() ->addAttributeToSelect('*'); foreach( $categoryProducts as $product){ echo $product->getId()." |
The above example will load all products of the category.
->addAttributeToSelect(‘*’);. We suggest not to select all attributes while getting the products collection, select the attributes which you need to display because it may slow down the system performance.
Magento get order details
Magento get order details : If you are working with magento order and looking for order details you can load order in two ways –
- Order – Load By Increment Id
- Order – Load By Entity Id
You can use either method to load order in magento. We are going to explain the magento order details with example.
Note : For Magento 2.x version Order Details Click here – https://www.tutorialsplane.com/magento-2-get-order-details/
Magento get order details
We will explain both methods to load order detials. Let us start one by one. First let us go with entity id ie. real order id –
Magento load order detail by Order Id (Entity id)
If you know real order id ie. entity id you can load order detail as below-
Load Order by Order Id :
$orderId = 111; // this is entity id $order = Mage::getModel('sales/order')->load($orderId); |
Magento load order detail by Order Increment Id
If you know real order increment id you can load order detail as below-
Load Order by Increment Id :
$orderIncrementId = 223; // this is entity id $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId); |
Magento get order items collection
Let us get order items collection from above order details-
Get Order Items Detail:
$orderItemsCollection = $order->getItemsCollection(); foreach $orderItemsCollection as $item){ $productId = $item->Id(); $productSku = $item->sku(); $productName = $item->getName(); } |
Magento get Order Id From Increment Id
You can get magento real order id from increment id as below-
Get Real Order Id From Increment Id:
//real order id ie. entity id $orderId = Mage::getModel('sales/order') ->loadByIncrementId($incrementId) ->getEntityId(); |
-More Details From Order-
Let us have a look on other details from magento order –
Magento get Order Status
You can get order status in magento as below –
Get Order Status :
$orderId = 111; // this is entity id $order = Mage::getModel('sales/order')->load($orderId); $status = $order->getStatusLabel(); |
Magento get Shipping And Billing Address From Order
You can get Shipping And Billing Address From Order in magento as below –
Get Shipping And Billing Address From Order :
$orderId = 111; // this is entity id $order = Mage::getModel('sales/order')->load($orderId); $shippingAddress = $order->getShippingAddress(); $billingAddress = $order->getBillingAddress(); |
Magento get Shipping Method From Order
You can get Shipping Method from order in magento as below –
Get Shipping Method from Order:
$orderId = 111; // this is entity id $order = Mage::getModel('sales/order')->load($orderId); $shippingMethod = $order->getShippingMethod(); |
Magento get Payment Method From Order
You can get Payment Method from order in magento as below –
Get Payment Method from Order:
$orderId = 111; // this is entity id $order = Mage::getModel('sales/order')->load($orderId); $paymentMethod = $order->getPayment()->getMethodInstance()->getTitle(); |
Magento get current store information
Magento get current store information : If you are looking about the current store information in magento you can use $store = Mage::app()->getStore(); to get current store instance and details such as –
- Store Id
- Store Name
- Store Code
- Store Url
- Store Status
- Store Categories
- Store Language
- Store Currency Symbol
- Store Country
- Store Website Id
- Store Group Id
Magento get current store information
Here We are going to show the store information listed above-
Magento get current store
Get Current Store :
$store = Mage::app()->getStore(); |
Magento get current store Id
Get Current Store Id:
$storeId = Mage::app()->getStore()->getStoreId(); |
Magento get current store Name
Get Current Store Name:
$storeName = Mage::app()->getStore()->getName(); |
Magento get current store Code
Get Current Store Code:
$storeCode = Mage::app()->getStore()->getCode(); |
Magento get current store Url
Get Current Store Code:
$storeUrl = Mage::app()->getStore()->getHomeUrl(); |
Magento get current store Status
Get Current Store Status:
$storeStatus = Mage::app()->getStore()->getIsActive(); |
Magento get current store Categories
You Can Use following method to get store categories – first get store id, get root category id then load category collection as below –
Get Current Store Categories:
$storeId = Mage::app()->getStore()->getStoreId(); $rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId(); $categoriesCollection = Mage::getModel('catalog/category') ->getCollection() ->setStoreId($storeId) ->addFieldToFilter('is_active', 1) ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%")) ->addAttributeToSelect('*'); foreach($categoriesCollection as $cat) { $id = $cat->getId(); $name = $cat->getName(); } |
Magento get current store Locale
Get Current Store Locale:
$storeStatus = Mage::app()->getStore()->getLocaleCode(); |
Magento get current Currency code And Symbol
First get currency code then get corresponding symbol.
Get Current Store Currency Code And Symbol:
$currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode(); $currencySymbol = Mage::app()->getLocale()->currency( $currencyCode )->getSymbol(); |
Magento get current Country Code
Get Current Country Code:
$countryCode = Mage::getStoreConfig('general/country/default'); |
Magento get Current Store website Id
You can get website Id as below –
Get Current Store Website Id:
$storeWebsiteId = Mage::app()->getStore()->getWebsiteId(); |
Magento get Current Store Group Id
You can get website Id as below –
Get Current Store Group Id:
$storeGroupId = Mage::app()->getStore()->getGroupId(); |
Magento get product collection filter by category
Magento get product collection filter by category : You can load products by the root category. First of all you need to get the root category id and then join product collection with the category product to get the products in that category. We have explained this with the following example
Magento get product collection filter by category
Below example shows the product list from root catgory-
$rootCatId = Mage::app()->getStore()->getRootCategoryId(); $collection = Mage::getResourceModel('catalog/product_collection') ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left') ->addAttributeToFilter('category_id', array('in' => $rootCatId)) ->addAttributeToSelect('*') ->load(); foreach($collection as $product){ echo $product->getName()."
"; echo $product->getId()."
"; };
Magento get product collection filter by Multiple category
If you want to load multiple categories products Use the below collection join and filter to get product list-
$collection = Mage::getResourceModel('catalog/product_collection') ->joinField('category_id','catalog/category_product','category_id','product_id=entity_id',null,'left') ->addAttributeToFilter('category_id', array('in' => array(1,5,9))) ->addAttributeToSelect('*') ->load(); foreach($collection as $product){ echo $product->getName()."
"; echo $product->getId()."
"; };
Where 1,5 and 9 is category id. The above example will get the products from the above categories.
Magento get full product image url
Magento get full product image url – You can get full image url image using the Mage::helper(‘catalog/image’). You can get thumbnail, full image or you can resize this image as per your need. Here is an example of magento full product image url.
Magento get full product image url Example with Syntax
Steps to get full image url in from magento product collection.
Here we are going to get product image urls from collection
$collection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSort('created_at', 'DESC') ->load(); foreach ($collection as $product){ $fullPath = Mage::helper('catalog/image')->init($product, 'image')->resize(135,135); }
Magento get Quote Session Items Product List
Magento get Quote Session Items Product List : You can get the list of products in quote from the quote session.
You get product detail from the quote session.
Magento get Quote Session Items Product List From Admin Quote
If you are working on magento admin quote you can get the Quote details – Product list as below
$Quote = Mage::getModel('adminhtml/session_quote')->getQuote(); foreach $Quote->getAllItems() as $item) { $product = $item->getProduct(); $productId = $product->getId(); $name = $product->getName(); $price = $product->getPrice(); }
Magento get Quote Session Items Product List From Cart Quote
If you are working on magento cart quote you can get the Quote details – Product list with detail as below
$Quote = Mage::getModel('checkout/cart')->getQuote(); foreach $Quote->getAllItems() as $item) { $product = $item->getProduct(); $productId = $product->getId(); $name = $product->getName(); $price = $product->getPrice(); }
Thus you can detail of products which has been in cart’s/Quote’s. This can be helpful when you are working with quote items.
Magento Save data in sales_flat_order table
Magento Save data in sales_flat_order table : You can save/update data in magento sales_flat_order table as easily using the model sales/order.
Magento Save data in sales_flat_order table
Below is syntax to update data in magento sales_flat_order table
Syntax
$quoteItem = Mage::getModel('sales/order')->load(order_id); $quoteItem->setColumn(value); $quoteItem->save();
Where order_id is quote oder id (primary key in sales_flat_order table) used to load the row you want to update.
ColumnName : Name of column you want to update.
Value : Value You want to update.
Example
$quoteItem = Mage::getModel('sales/order')->load($orderId); $quoteItem->setStatus('Complete'); $quoteItem->save();