How to Edit or Modify Shippable Order Item Quantity in Magento 2?

Change Shippable Order Item Quantity in Magento 2

In this topic, we will show you How to Change Shippable Order Item Qty in Magento 2 through 2 steps

  • Step 1: Create new columns on sales_order_item table
  • Step 2: Define plugin in di.xml
  • Step 3: Rewrite getSimpleQtyToShip() function

Step 1: Create new columns on sales_order_item table

  • In this example, we will create qty_preapreship column in sales_order_item table
  • File Directory: app/code/Magestore/OrderSuccess/Setup/InstallSchema.php
namespace Magestore\OrderSuccess\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface

{

    /**

     * @param SchemaSetupInterface $setup

     * @param ModuleContextInterface $context

     * @return $this

     * @throws \Zend_Db_Exception

     */

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)

    {

        $installer = $setup;

        $installer->startSetup();

        $setup->getConnection()->dropTable($setup->getTable('os_ordersuccess_batch'));

        $installer->getConnection()->addColumn(

            $installer->getTable('sales_order_item'),

            'qty_prepareship',

            array(

                'type'      => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,

                'nullable'  => false,

                'length'    => '12,4',

                'default'    => 0,

                'comment'   => 'Qty Prepareship'

            )

        );

            $installer->endSetup();

            return $this;

        }

}
namespace Magestore\OrderSuccess\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface

{

    /**

     * @param SchemaSetupInterface $setup

     * @param ModuleContextInterface $context

     * @return $this

     * @throws \Zend_Db_Exception

     */

    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)

    {

        $installer = $setup;

        $installer->startSetup();

        $setup->getConnection()->dropTable($setup->getTable('os_ordersuccess_batch'));

        $installer->getConnection()->addColumn(

            $installer->getTable('sales_order_item'),

            'qty_prepareship',

            array(

                'type'      => \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL,

                'nullable'  => false,

                'length'    => '12,4',

                'default'    => 0,

                'comment'   => 'Qty Prepareship'

            )

        );

            $installer->endSetup();

            return $this;

        }

}

Step 2: Define plugin in di.xml

File directory: app/code/Magestore/OrderSuccess/etc/di.xml

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Sales\Model\Order\Item">

        <plugin name="AfterGetSimpleQtyToShip" type="Magestore\OrderSuccess\Plugin\Item\AfterGetSimpleQtyToShip" sortOrder="1" disabled="false"/>

    </type>

</config>

Step 3: Rewrite getSimpleQtyToShip() function

File directory: app/code/Magestore/OrderSuccess/Plugin/AfterGetSimpleQtyToShip.php

namespace Magestore\OrderSuccess\Plugin\Item;

class AfterGetSimpleQtyToShip{

    public function afterGetSimpleQtyToShip(\Magento\Sales\Model\Order\Item $item)

    {

        $qty = $item->getQtyOrdered() - $item->getQtyShipped() - $item->getQtyRefunded()

              - $item->getQtyCanceled() - $item->getQtyBackordered() - $item->getQtyPrepareship();

        return max($qty, 0);

    }

}

From now, item to ship will be calculated with quantity prepareship.

With this guide, you can manage the Shippable Order Item Qty in Magento 2 easily. Every store has a Shippable Order Item Qty in Magento 2 with many attributes.

For Magento Kb, visit Webnexs Magento Knowledge Base.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *