Tag Archives: Magento tutorials online
Magento Get product Attribute Value
Magento Get product Attribute Value : If you are working with magento you often need to work with products and their attributes. Magento provides flexibility to add product attributes dynamically. Here we are going to explain how to get product attribute value. Syntax and example is given below –
Magento Get product Attribute Value Syntax
Here is syntax to get product value syntax-
Magento Get product Attribute Value
$productId = 55; $storeId = 1; $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId); $attrText = $product->getAttributeText('your_attribute'); |
If you want to use option id Here is another example of the set attribute and get value.
Magento Set product Attribute Value And Get Attribute Value
Here is syntax to set and get attribute value in magento product-
Magento Set product Attribute Value And Get Attribute Value
$attributeOptionId = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'your_attribute', $storeId); $product = Mage::getModel('catalog/product') ->setStoreId($storeId) ->setData('your_attribute', $attributeOptionId); $attrText = $product->getAttributeText('your_attribute'); |
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 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();
Magento save data in sales_flat_quote_item table
Magento save data in sales_flat_quote_item table : You can save/update data in magento sales_flat_quote_item table as easily using the model sales/quote_item.
Magento save data in sales_flat_quote_item table
Below is syntax to update data in magento sales_flat_quote_item table
Syntax
$quoteItem = Mage::getModel('sales/quote_item')->load(quote_item_id); $quoteItem->setColumnName(value); $quoteItem->save();
Where quote_item_id is quote item id (primary key in sales_flat_quote_item 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/quote_item')->load($quoteItemId); $quoteItem->setCustomPrice('10.99'); $quoteItem->save();