One of our client came up with a requirement where he adjusted payment type as authorize along with new orders to be placed as processing status.
Our work was to do execute cronjob such a way that it generates an invoice and capture the payment under technical limitation capped by client.
To execute the job we created a cronjob & set it at every minute.
Have a look on step-by-step explaination.
Step 1: We create a crontab.xml in module at app/code/Magemonkeys/Generateinvoice/etc/crontab.xml and add the following code in the XML.
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd"> <group id="default"> <job name="magemonkeys_generateinvoice_programmatically" instance="Magemonkeys\Generateinvoice\Cron\Invoicegenerate" method="execute"> <schedule>* * * * *</schedule> </job> </group> </config> |
Step 2: Then, we create an Invoicegenerate.php at app/code/Magemonkeys/Generateinvoice/Cron/Invoicegenerate.php and add the following code in this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
<?php namespace Magemonkeys\Generateinvoice\Cron; use Magento\Framework\Exception\LocalizedException; class Invoicegenerate { /** * @var \Magento\Sales\Api\OrderRepositoryInterface */ private $_orderRepository; /** * @var \Magento\Sales\Model\Service\InvoiceService */ private $_invoiceService; /** * @var \Magento\Framework\DB\TransactionFactory */ private $_transactionFactory; /** * @var \Magento\Sales\Model\Order\Email\Sender\InvoiceSender */ private $_invoiceSender; /** * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory */ private $_orderCollectionFactory; public function __construct( \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Framework\DB\TransactionFactory $transactionFactory, \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory ) { $this->_orderRepository = $orderRepository; $this->_invoiceService = $invoiceService; $this->_transactionFactory = $transactionFactory; $this->_invoiceSender = $invoiceSender; $this->_orderCollectionFactory = $orderCollectionFactory; } public function execute() { $orderCollection = $this->_orderCollectionFactory->create() ->addAttributeToSelect('*') ->addFieldToFilter('status', 'processing'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $logger = $objectManager->get("Psr\Log\LoggerInterface"); foreach ($orderCollection as $order) { if($order->hasInvoices()) { $logger->info('Order #'.$order->getId().' has already been invoiced.'); continue; } $payment = $order->getPayment(); $method = $payment->getMethodInstance(); $methodTitle = $method->getTitle(); if(strtolower($methodTitle) == 'credit card'){ try { $order = $this->_orderRepository->get($order->getId()); if (!$order->getId()) { $logger->info('Order #'.$order->getId().' no longer exists.'); continue; } if(!$order->canInvoice()) { $logger->info('Order #'.$order->getId().' does not allow an invoice to be created.'); continue; } $invoice = $this->_invoiceService->prepareInvoice($order); if (!$invoice) { $logger->info("Order #".$order->getId()." can't save the invoice right now."); continue; } if (!$invoice->getTotalQty()) { $logger->info("Order #".$order->getId()." create an invoice without products."); continue; } $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE); $invoice->register(); $invoice->getOrder()->setCustomerNoteNotify(false); $invoice->getOrder()->setIsInProcess(true); $order->addStatusHistoryComment('Automatically INVOICED', false); $transactionSave = $this->_transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder()); $transactionSave->save(); // send invoice emails try { $this->_invoiceSender->send($invoice); $logger->info("Order #".$order->getId()." - Invoice has been created successfully."); } catch (\Exception $e) { $logger->info("Order #".$order->getId()." - We can't send the invoice email right now."); } } catch (\Exception $e) { $logger->info($e->getMessage()); } } } } } |
[crayon-63e09e8ec219f603904118/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.