Category Archives: Magento
Magento Get All Visible Items From Order
Magento Get All Visible Items From Order : You can get the all visible items from the order simply using $order->getAllVisibleItems() which will give you all visible items of order. Here in this example we are going to explain how to get visible items of order on magento with different example.
Magento Get All Visible Items From Order
First load order by order id or increment id and then get all visible items as below –
Magento Get All Visible Items From Order:
// Load by order id $order=Mage::getModel('sales/order')->load($orderId); //Or load by Increment Id $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId); $orderItems = $order->getAllVisibleItems(); foreach ($orderItems as $item) { $productId = $item->getProductId(); $product = Mage::getModel('catalog/product')->load($productId); $name = $product->getName(); $sku = $product->getSku(); } |
The above example will give you all visible items of order.
Magento Get All Visible Items From Quote
Magento Get All Visible Items From Quote : You can get the all visible items from the quote simply using $quote->getAllVisibleItems() which will give you all visible items of quote. Here in this example we are going to explain how to get visible items of quote on magento admin & front end. We will explain with different example.
Magento Get All Visible Items From Quote
You Can get Quote visible items as –
Magento get all visible items From Cart Quote
If you are working with front end checkout then you can get quote’s visible items as below-
Magento Get All Visible Items From Quote:
$quote = Mage::getSingleton('checkout/session')->getQuote(); $cartItems = $quote->getAllVisibleItems(); foreach ($cartItems as $item) { $productId = $item->getProductId(); $product = Mage::getModel('catalog/product')->load($productId); $name = $product->getName(); $sku = $product->getSku(); } |
The above example will give you all visible items of cart on front end.
Magento get all visible items From Admin Quote
If you are working with admin quote then you can get quote’s visible items as below-
Magento Get All Visible Items From Quote:
$quote = Mage::getSingleton('adminhtml/session_quote')->getQuote(); $cartItems = $quote->getAllVisibleItems(); foreach ($cartItems as $item) { $productId = $item->getProductId(); $product = Mage::getModel('catalog/product')->load($productId); $name = $product->getName(); $sku = $product->getSku(); } |
The above example will give you the all visible items from admin quote.
Magento create shipment programmatically
Magento create shipment programmatically : While working with magento projects sometimes you need to create shipments programmatically. You can create shipments from order if the shipments are not created already.
Here in this tutorial we are going to explain the steps to create shipments programmatically.
Magento create shipment programmatically
Here are the steps to create shipments-
- Load Order By Order Id Or Increment Id.
- Check If Order Can Create Shipment.
- Check If items can be shipped & Set Quantity For this.
Magento create shipment programmatically:
// Load by order id $order=Mage::getModel('sales/order')->load($orderId); //Or load by Increment Id $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId); $email = true; $addComment=true; $comment="Demo Shipment"; // Check if order Can Be shipped. if ($order->canShip()) { $qty = array(); foreach($order->getAllVisibleItems() as $item){ $qty[$item->getId()] = $item->getQtyToShip; } $shipment = $order->prepareShipment($qty); if ($shipment) { $shipment->register(); $shipment->addComment($comment, $email && $addComment); $shipment->getOrder()->setIsInProcess(true); try { $transactionSave = Mage::getModel('core/resource_transaction') ->addObject($shipment) ->addObject($shipment->getOrder()) ->save(); $shipment->sendEmail($email, ($addComment ? $comment : '')); } catch (Mage_Core_Exception $e) { Mage::log("Errror While Creating Shipment...".$e->getMessage()); } } } |
In the above example we have first loaded the order for which we want to create shipments. After Loading the order we have checked whether the order can be shipped or not. Then we have filtered the items quantities which can be shipped along with the item id which is used to prepare the shipment. After preparing the shipment comment and then we have added the shipment & order object to the core transaction. Once it is saved email is sent.
More Examples
Let’s have look over more example on the shipments.
Magento Create Multi Shipment for a Single Order programmatically
As we know we can create multiple shipments for a single order. We sometimes need to create multiple shipments for one order. Here is are two methods to create multiple shipments in magento.
Method 1
Magento Create Multi Shipment for a Single Order programmatically:
$order = Mage::getModel('sales/order')->load($orderId); $shipmentItems = array(); // array items of order to make shipments. //collect items which for which you want to make shipment. foreach ($shipmentItems as $item) { $shipmentItems [$item->getId()] = $item->getQuantity(); } if ($order->getId() && !empty($shipmentItems) && $order->canShip()) { $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($shipmentItems); $shipment->save(); } |
The above example will create the multiple shipments from the order on the basis of the items you pass.
Method 2
Sometimes method 1 does not works properly and gives data_invalid error. You can also create multiple shipments as below also –
Magento Create Multi Shipment for a Single Order programmatically:
$orderId = 100; $order = Mage::getModel('sales/order')->load($orderId); $convertor = Mage::getModel('sales/convert_order'); $shipment = $convertor->toShipment($order); foreach ($order->getAllVisibleItems() as $item) { // here colect order items you want to ship if ($item->getQtyToShip() && !$item->getIsVirtual()) { $shipItem = $convertor->itemToShipmentItem($item); $shipItem->setQty($item->getQtyToShip()); $shipment->addItem($shipItem); } } $shipment->register(); $order->setIsInProcess(true); Mage::getModel('core/resource_transaction') ->addObject($shipment) ->addObject($order)) ->save(); |
Magento get order id from increment id
Magento get order id from increment id : You can use Mage::getModel(‘sales/order’) to get the order id from the increment id of order. First load the order by increment id then get the order id from the order object. Here is this example we are going to explain how to get the order id from increment id.
Magento get order id from increment id
Here is syntax to get order id from increment id in magento –
Magento get order id from increment id:
$orderIncrementId = "1000002"; $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId); $orderId = $order->getId(); |
In the above example we have loaded order object by increment id of order. From order object $order you can get order id as $order->getId(); which will give you order id of an order.
Magento – get price rules from order
Magento – get price rules from order: Mage::getModel(‘catalogrule/rule’) is used to get the price rules applied on items. If you want to get price rules applied on order items first load order items and then load rules ids applied on the items. Here in this tutorial we are going to explain this with simple example.
Magento – get price rules from order
First order items using the model – Mage::getModel(‘sales/order_item’) and then load the rules applied on the items as below-
Magento – get price rules from order:
$orderId = 100; $items = Mage::getModel('sales/order_item') ->getCollection() ->addFilter('order_id',array('eq'=>$orderId)); foreach($items as $item){ if($item->getAppliedRuleIds() == ''){ /*if multiple rules applied**/ foreach(explode(",",$item->getAppliedRuleIds()) as $ruleId){ //Load object $rule = Mage::getModel('catalogrule/rule')->load($ruleId); echo "".$item->getName()." - Rule ".$rule->getName()." |
The above example will give you the rules applied on the order items.
Magento Get Child Categories
Magento Get Child Categories: Catalog catgory model Mage::getModel(‘catalog/category’) is used to the child categories of parent category in magento. Using this model you can load the categories in this model. Here in this tutorial we are going to explain how to get child categories of a categories with example.
Magento Get Child Categories
Let us understand how to get child categories with simple example –
Magento get child categories ids
First let us get child categories ids. Here is syntax to get child ids as below-
Magento Get Child Categories
/* First Load category by id*/ $category = Mage::getModel('catalog/category')->load($id); /*This will Return comma separated ids*/ $subcategories = $category->getChildren(); $subcategoriesArray = explode(',',$subcategories); |
The above example will give you comma separated string of ids which you can convert to arrays as above.
More Example
Let us have more example of child categories.
Magento Sub-Categories / Child Categories of parent Category-
You can get sub-categories of specific category as below-
Magento Sub-Categories / Child Categories of parent Category-
$childrenCat = Mage::getModel('catalog/category')->getCategories($id); foreach ($childrenCat as $category) { echo $category->getId()." |
The above example will you all child categories of the selected categories.
Magento get list of Products in Comparison List
Magento get list of Products in Comparison List : If you want to get the list of products which are in comparison list Mage_Catalog_Model_Product_Compare_List and it’s method getItemCollection() is used. You can get collection of items which are in comparison list. Here in this tutorial we are going to explain how to get comparison list products.
Magento get list of Products in Comparison List
Mage::getModel(‘catalog/product_compare_list’)->getItemCollection(); will give the collection of the products which are in comparison list.
Magento get list of Products in Comparison List:
$collection = Mage::getModel('catalog/product_compare_list')->getItemCollection(); foreach($collection as $product){ echo $product->getId(); } |
The above example will give you the product collection which are added on comparison list.
Magento Get Product Availability by Product Id
Magento Get Product Availability by Product Id : Mage::getModel(‘cataloginventory/stock_item’) is used to get the product availability in magento. You can load catalog inventory stock item by product object. Here in this tutorial we are going to explain how to get Product is in stock or not.
Magento Get Product Availability by Product Id
First load the product by product id then use product object to check whether the item is in stock or not. Here is syntax to check the stock availability of the product.
Magento Check Product is in stock or out of stock
Magento Check Product is in stock by Product Id
$product = Mage::getModel('catalog/product')->load($productId); $stocklevel = Mage::getModel('cataloginventory/stock_item') ->loadByProduct($product)->getQty(); |
Where $productId is product’s id which you want to check whether it is available or not. $stocklevel will give you the level of availability.
Magento get out of stock products
Magento get out of stock products : Mage::getModel(‘cataloginventory/stock_item’) is used to get the out of stock product in magento. You can add the filter to get the out of stock items. Here we are going to explain the method to get the out of stock items.
Magento get out of stock products
Here is syntax to get the out of stock items in magento –
Magento get out of stock products
$outOfStockProducts = Mage::getModel('cataloginventory/stock_item') ->getCollection() ->addFieldToFilter('is_in_stock', 0); |
The above collection will give you the out of stock items.