Category Archives: Magento 2.0 Tutorial
Magento 2 get Shipment id from Order Id
Magento 2 get Shipment id from Order Id– There can be multiple shipments against an order. Here in this tutorial, we are going to explain how to get shipment ids from order object.
Magento 2 get Shipment id from Order Id | Shipment Collection Example
You can get the shipment collection by order.
Magento 2 Get shipment Collection
Shipment Collection can be retrived by order here is an example –
Magento 2 get Shipment id from Order Id Example:
$orderId = 9877; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId); $shipmentCollection = $order->getShipmentsCollection(); foreach($shipmentCollection as $shipment){ $shipmentIncrementId = $shipment->getIncrementId(); } |
Thus you can get the shipments from order.
Magento 2 Delete Products programmatically
Magento 2 Delete Products programmatically | PHP Script– You can delete products programmatially in using product id. Here in this tutorial we are going to explain how you can delete the products using your own program.
Magento 2 Delete Products programmatically | Delete All Products Script Example
Now let us go to understand how to deal with product deletion in Magento 2-
Delete Product By Id
To delete single product by id first load product object by id then you can delete it as below-
Magento 2 Delete Products programmatically Example:
$productID = 999; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->create('Magento\Catalog\Model\Product'); $product->load($productID)->delete(); |
Delete All Products
Let us create simple script to delete products. If you want to delete all products then you need to load products collection and delete products one by one simply as below-
Delete All Products Example:
$objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true); $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory'); $collection = $productCollection->create()->addAttributeToSelect('*')->load(); $app_state = $objectManager->get('\Magento\Framework\App\State'); $app_state->setAreaCode('frontend'); foreach ($collection as $product){ try { echo 'Deleted '.$product->getName()." |
Magento 2 Get Current Quote Id
Magento 2 Get Current Quote Id From Session – You can use object manager to get the current quote/cart session object, using this object you can get the quote details. Here in this article we are going to explain how you can get quote id quickly.
Magento 2 Get Current Quote Id From Session Example
Here in a simple example to get the quote id from session –
Magento 2 Get Current Quote Id From Session Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $quote = $cart->getQuote(); // This will return the current quote $quoteId = $quote->getId(); |
The above example will give the current quote id.
Tags
magento 2 get quote id from checkout session
Magento 2 get Cart Items
Magento 2 get Cart Items from session– While working with cart we sometimes need to get quote all items. It is very simple to get all visible items from quote/cart object. Here in this tutorial, we are going to explain how you can get the cart items programmatically.
Magento 2 get Cart Items From Session | Quote Items Example
You can use object manager to get the quote session and items.
Get All Items
This will give all items.
Get All Items From Quote Session Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $allItems = $cart->getQuote()->getAllItems(); foreach ($allItems as $item) { echo $item->getItemId(); echo $item->getName(); echo $item->getSku(); } |
Get All Visible Items
This will give all visible items that can be directly displayed.
Get All Items From Quote Session Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $allVisibleItems = $cart->getQuote()->getAllVisibleItems(); foreach ($allVisibleItems as $item) { echo $item->getItemId(); echo $item->getName(); echo $item->getSku(); } |
Magento 2 Clear Cart programmatically
Magento 2 Clear Cart programmatically – Sometimes we need to clear cart(Quote) programmatically in Magento, It is very simple to remove items from cart. Here in this tutorial we are going to explain how you can clear cart(quote) items programmatically.
Magento 2 Clear Cart programmatically | Remove Quote Items | All Items Example
First you need to get the items from the cart session then remove items one by one simply as below-
Remove All Items
The below exmple will remove all items.
Magento 2 Clear Cart programmatically Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $allItems = $cart->getQuote()->getAllVisibleItems(); foreach ($allItems as $item) { $itemId = $item->getItemId(); $cart->removeItem($itemId)->save(); } |
So the above example can be used anywhere to remove items using code.
Remove Cart Item By Item Id
If you want to remove some specified item from cart you can remove it by Id. Here in a simple example to remove quote item by Id-
Magento 2 Clear Quote Item By Item Id Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); $itemId = 1011; $cart->removeItem($itemId)->save(); // item removed successfully |
Thus you can remove any item from cart/quote by item_id.
Tags
Magento 2 remove item from cart programmatically
Magento 2 Get Last Insert Id
Magento 2 Get Last Insert Id Sometimes we need to get the last inserted id in database table. It is very simple to get the last inserted id in Magento 2. It is a little bit different from Magento 1. Here in this tutorial we are going to explain how you can get the last inserted row id in Magento 2.
Magento 2 Get Last Insert Id | Last Record From Model Example
Here is a simple example to get the last inserted id –
Magento 2 Get Last Insert Id Example:
$model = $this->_objectManager->create('Ecom\HelloWorld\Model\Question'); $model->setTitle('Simple Question'); $model->setDescription('Question Description'); $model->save(); $lastInsrtedId = $model->getId(); |
The above example will give you the last Inserted id from the database.
Magento 2 Get simple Products From Configurable Product
Magento 2 Get simple Products From Configurable ProductIt is little bit different to get the simple products from configurable in Magento 2.x. You can get the child products of parrent configurable products using object manager or factory method in Magento 2. Here in this article we are going to explain how you can get simple products of any configurable ie. child products of configurable product.
Magento 2 Get simple Products From Configurable Product | Get Child Products | Collection | Query Example
You can get the child products of any configurable products simply as below-
Magento 2 Get simple Products From Configurable Product | Example:
$productId = 199; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId); $configMgr = $objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable'); $childProducts = $configMgr->getUsedProducts($_product,null); foreach($childProducts as $childProduct) { echo "Product ID: ".$childProduct->getId(); echo " |
The above example will give you all the child products of configurable products.
Magento 2 Collection Set Page Size Limit
Magento 2 Collection Set Page Size Limit It is very simple to merge to collections in magento 2. Here in this tutorial we are going to explain how to set page size in collection.
Magento 2 Collection Set Page Size Limit | Curpage Example
You can set the page size of collection in magento simply as below-
Set Page Size
->setPageSize($pageSize); is used to set the current collection size in magento 2. Here is an example-
Magento 2 Collection Set Page Size Limit Example:
public function getMyData(){ $pageSize= 10; $newCollection = $this->newcollectionFactory->create(); $newCollection->setPageSize($pageSize); return $newCollection; } |
Set Current Page
If you are using pagination then you can use ->setCurPage($page); is used to set the current page in magento 2. Here is an example-
Magento 2 Collection Set Cur Page Example:
public function getMyData(){ $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1; $newCollection = $this->newcollectionFactory->create(); $newCollection->setCurPage($page); return $newCollection; } |
Where $page is current page.
Magento 2 collection order by
Magento 2 collection order by Id– It is very simple to set order by clause in Magento 2. We can use setOrder ASC or DESC in Collection. Here in this article, we are going to explain how you can get collection data order by ASC or DESC.
Magento 2 collection order by | Id | ASC | DESC Example
Here is example of product collection order by price ASC, DESC-
Collection order by ASC
Here is an example of order by asc clause in collection-
Magento 2 collection order by | ASC | Example:
$colletion = $productionCollection->getCollection(); $colletion->addFieldToFilter('store_id', 1); $colletion->setOrder('price','ASC'); |
Collection order by DESC
Here is an example of order by desc clause in collection-
Magento 2 collection order by | ASC | Example:
$colletion = $productionCollection->getCollection(); $colletion->addFieldToFilter('store_id', 1); $colletion->setOrder('price','DESC'); |
Magento 2 collection Set order by Not Working?
If order by clause not working check collection & order by column.