Before place order checkout action plugin Magento 2.

You can create a plugin for Before Place order action In Magento 2 using beforePlace() method.

When you required to check or set data before place order in Magento 2, You can use a plugin for OrderManagementInterface interface place() method.

place() method from the Magento\Sales\Api\OrderManagementInterface interface class used to Place order operation in Magento 2 from checkout page.

Continue reading “Before place order checkout action plugin Magento 2.”

Get shipping methods from current active quote Magento 2.

You can fetch a list of all the active shipping methods carrier code, the title, amount from the current quote id using Magento 2.

When you are working with checkout customization or you required to get all the active shipping methods for the current quote, You can get all the active shipping methods from the quote.

You can use Magento\Quote\Api\ShippingMethodManagementInterface interface for getting shipping methods. Continue reading “Get shipping methods from current active quote Magento 2.”

Get Auto Increment Field from a table using Magento 2.

You can get the Auto Increment Field from a database table using Magento 2 as Best coding practice way.

You need to use getAutoIncrementField() for getting the auto-increment field from the table.

Output: Returns field name if an auto-increment field is available otherwise return as false.

<?php
namespace Path\To\Class;

use Magento\Framework\App\ResourceConnection;

class AutoIncrementField {

    const ORDER_TABLE = 'sales_order';

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    public function __construct(
       ResourceConnection $resourceConnection
    ) {
       $this->resourceConnection = $resourceConnection;
    }

    /**
    * get AutoIncremntFieldName using Query
    *
    * @return string|bool
    */
    public function getAutoIncremntFieldName()
    {
      $connection  = $this->resourceConnection->getConnection();
      $tableName = $connection->getTableName(self::ORDER_TABLE);
      $getFieldName = $connection->getAutoIncrementField($tableName);

      return $getFieldName;
    }
}

You can call $this->getAutoIncremntFieldName() function to check sales_order table with auto_increment field name.

Output:
entity_id