Which are the latest features of magento 2.3 version?

Magento 2.3 Version will be Generally available from 28 November. Before installing Latest setup You must aware about the new version of your software.

I have collected some of the latest new feature available in Magento for the first time.

Check the list of new feature for Magento 2.3. Continue reading “Which are the latest features of magento 2.3 version?”

A Complete List of all events in Magento 2

Magento 2 Complete guide to the list of available events that will be used in daily routine as a Magento developer to trigger event-specific tasks at a time of Development.

You can use any required events from the Core module to accomplish your requirements. Define events in the events.xml file, Create an observer.php file to do specific actions for an event.

You can also create your own custom event to accomplish your task by link, Create Custom Events. Continue reading “A Complete List of all events in Magento 2”

How to create Shipment programmatically in magento 2?

We just need to pass order id in generateShipment($orderId) function from template.
below code snippet is used for generating Shipment in Magento 2, After generating Shipment automatically send the Shipment mail to a customer who has placed the order.

<?php
namespace Rbj\Shipment\Block;

class Shipment extends \Magento\Framework\View\Element\Template
{
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory,
        \Magento\Framework\DB\TransactionFactory $transactionFactory,
        \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        array $data = []
    ) {
        $this->orderRepository = $orderRepository;
        $this->orderConverter = $convertOrderFactory->create();
        $this->transactionFactory = $transactionFactory;
        $this->messageManager = $messageManager;
        $this->shipmentSender = $shipmentSender;
        parent::__construct($context, $data);
    }
/**
 * Create Shipment Based on Order Object
 * @param \Magento\Sales\Model\Order $order
 * @return $this
 */
public function generateShipment($orderId)
{
    try {
        $order = $this->orderRepository->get($orderId);
        if (!$order->getId()) {
            throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.'));
        }

        /* check shipment exist for order or not */
        if ($order->canShip())
        {
            // Initialize the order shipment object
            $shipment = $this->orderConverter->toShipment($order);
            foreach ($order->getAllItems() AS $orderItem) {
                // Check if order item has qty to ship or is order is virtual
                if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
                    continue;
                }
                $qtyShipped = $orderItem->getQtyToShip();
                // Create shipment item with qty
                $shipmentItem = $this->orderConverter->itemToShipmentItem($orderItem)->setQty($qtyShipped);
                // Add shipment item to shipment
                $shipment->addItem($shipmentItem);
            }
            // Register shipment
            $shipment->register();

            $shipment->getOrder()->setIsInProcess(true);

            try {
                $transaction = $this->transactionFactory->create()->addObject($shipment)
                    ->addObject($shipment->getOrder())
                    ->save();
                echo $shipmentId = $shipment->getIncrementId();
            } catch (\Exception $e) {
                $this->messageManager->addError(__('We can\'t generate shipment.'));
            }

            if ($shipment) {
                try {
                    $this->shipmentSender->send($shipment);
                } catch (\Exception $e) {
                    $this->messageManager->addError(__('We can\'t send the shipment right now.'));
                }
            }

            return $shipmentId;
        }
    } catch (\Exception $e) {
        $this->messageManager->addError($e->getMessage());
    }

    return true;
}

Call function from the template,

$orderId = 1;
$shipment = $block->generateInvoice($orderId);

Now Check from Admin panel.

Sales -> Order, Order with Id 1 has generated Shipment.