Magento 2 display out of stock products at the end of the list page.

You can display out of stock product at end of catalog page in Magento 2 using just simple way. All the Out of stock product will be displayed at end of listing page.

You need to create module for display out of stock product at end of the page.

Create registration.php and module.xml file for creating a module. I hope you have created both file for define module,

For Collection of product generate from Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection file. We need to create a plugin for load() function. we need to set our custom condition for out of stock product to beforeLoad() function.

Create a di.xml file,

Path, app/code/Rbj/OutOfStock/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <!--  out of stock product at end of list -->
    <type name="Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection">
        <plugin name="Rbj_OutOfStock::OutofstockEnd" type="Rbj\OutOfStock\Plugin\Product\ProductList\Toolbar"/>
    </type>
</config>

Need to override Stock.php file from Magento\CatalogInventory\Helper\Stock class.

Create Toolbar.php file, File Path, app/code/Rbj/OutOfStock/Plugin/Product/ProductList/Toolbar.php

<?php
/**
 * @author Rakesh Jesadiya
 * @package Rbj_OutOfStock
 */

namespace Rbj\OutOfStock\Plugin\Product\ProductList;

class Toolbar
{
    /**
     * @param \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $subject
     * @param bool $printQuery
     * @param bool $logQuery
     * @return array
     */
    public function beforeLoad(\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $subject, $printQuery = false, $logQuery = false)
    {
        $orderBy = $subject->getSelect()->getPart(\Zend_Db_Select::ORDER);
        $outOfStockOrderBy = array('is_salable DESC');
        /* reset default product collection filter */
        $subject->getSelect()->reset(\Zend_Db_Select::ORDER);
        $subject->getSelect()->order($outOfStockOrderBy);

        return [$printQuery, $logQuery];
    }
}

Clear Cache and Check in front. All the Out of stock product will display at end of listing page.

How to update order status using Rest API Magento 2?

We can update Order status using REST api in Magento 2 by just simple payload. For Create Order, Create order using rest api in Magento 2
We required the order_id, order status, and order_incrementid for update order status.
Request:{YOUR_URL}/rest/V1/orders

Type: POST

Body:

POST
{
  "entity":{
    "entity_id":10,
    "status":"processing",
    "increment_id":"000000010"
  }
}

Response:

{
    "base_grand_total": null,
    "base_total_due": 0,
    "billing_address_id": 204,
    "created_at": "2018-09-16 18:12:42",
    "customer_email": null,
    "entity_id": 10,
    "grand_total": null,
    "increment_id": "000000010",
    "protect_code": "352f3f47cd42aa50f0e7e328ffd53040",
    "status": "processing",
    "total_due": 0,
    "updated_at": "2018-09-16 18:44:57",
    "items": [
        {
            "amount_refunded": 0,
            "base_amount_refunded": 0,
            "base_cost": 0,
            "base_discount_amount": 4.5,
            "base_discount_invoiced": 0,
            "base_original_price": 45,
            "base_price": 45,
            "base_price_incl_tax": 45,
            "base_row_invoiced": 0,
            "base_row_total": 45,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "created_at": "2018-09-16 18:12:42",
            "discount_amount": 4.5,
            "discount_invoiced": 0,
            "discount_percent": 10,
            "free_shipping": 0,
            "is_virtual": 0,
            "item_id": 124,
            "name": "Push It Messenger Bag",
            "no_discount": 0,
            "order_id": 103,
            "original_price": 45,
            "price": 45,
            "price_incl_tax": 45,
            "product_id": 14,
            "product_type": "simple",
            "qty_backordered": 0,
            "qty_canceled": 0,
            "qty_invoiced": 0,
            "qty_ordered": 1,
            "qty_refunded": 0,
            "qty_returned": 0,
            "qty_shipped": 0,
            "row_invoiced": 0,
            "row_total": 45,
            "row_total_incl_tax": 45,
            "row_weight": 0,
            "sku": "24-WB04",
            "store_id": 1,
            "tax_amount": 0,
            "tax_invoiced": 0,
            "tax_percent": 0,
            "updated_at": "2018-09-16 18:12:42",
            "weight": 0
        }
    ],
    "billing_address": {
        "address_type": "billing",
        "city": "Ahmedabad",
        "company": "Rbj",
        "country_id": "US",
        "email": "rakesh@rakeshjesadiya.com",
        "entity_id": 204,
        "firstname": "rakesh",
        "lastname": "jesadiya",
        "parent_id": 103,
        "postcode": "30332",
        "region": "Georgia",
        "region_code": "GA",
        "region_id": 19,
        "street": [
            "Street 1",
            "Street 2"
        ],
        "telephone": "123456"
    },
    "payment": {
        "account_status": null,
        "additional_information": [
            ""
        ],
        "cc_last4": null,
        "entity_id": 103,
        "method": "cashondelivery",
        "parent_id": 103
    },
    "status_histories": []
}

 

 

How to create Invoice using Rest API Magento 2?

Magento 2 is robust,scalable and multi feature enable eCommerce platform. You can create Invoice of an Order Using an third party platform to Magento 2 by API request.

For Create order, Create order using Rest API Magento 2

For Create Shipment Using API, Create Shipment using Rest API Magento 2
Using below payload you can create Shipment of an order using any third party platform to Magento 2. you Just need to pass below payload in your request with POST data.

Request:
{YOUR_URL}/rest/V1/order/{order_id}/invoice

Type: POST

Body:

{
  "capture": true,
  "items": [
    {
      "extension_attributes": {},
      "order_item_id": 82,
      "qty": 1
    }
  ],
  "notify": true,
  "appendComment": true,
  "comment": {
    "extension_attributes": {},
    "comment": "Order Invoice generate",
    "is_visible_on_front": 0
  },
  "arguments": {
    "extension_attributes": {}
  }
}

Pass order_item_id is your order item’s id don’t pass order id in order_item_id field. Pass order_id in reqeuest Url. You can create multiple request using pass multi array in items array.

Response:
10 (new Invoice id)