0

I want to get order collection which don't have invoice. I want to create invoice manually for specific product type.

How can I get?

My working code in which I have get recent 10 days orders:

<?php
ini_set("memory_limit", "-1");
ini_set('max_execution_time', 18000);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
$droot = '/var/www/html/';
include $droot.'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$baseUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

$_connection = $resource->getConnection();
$scopeConfig = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface');

$orderCollection = $objectManager->get('Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$prev_date = date('Y-m-d', strtotime('-10 days'));
$current_date = date('Y-m-d');
 
$collection = $orderCollection->create()
   ->addAttributeToSelect("*")
   ->addAttributeToFilter('created_at', ['gteq'=>$prev_date.' 00:00:00'])
   ->addAttributeToFilter('created_at', ['lteq'=>$current_date.' 23:59:59']);

foreach ($collection as $col) {
    $orderId = $col->getId();
    $order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);

    $productType = '';
    foreach ($order->getAllVisibleItems() as $_item) {
        $productType = $_item->getProductType();
     }

    if($productType == 'amgiftcard'){
        if ($order->canInvoice()) {
            $invoice = $objectManager->create('Magento\Sales\Model\Service\InvoiceService')->prepareInvoice($order);
            if (!$invoice->getTotalQty()) {
                throw new \Magento\Framework\Exception\LocalizedException(
                            __('You can\'t create an invoice without products.')
                        );
            }

            $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE);

            $invoice->register();
            $invoice->getOrder()->setIsInProcess(true);
            $transaction = $objectManager->create('Magento\Framework\DB\Transaction')
                            ->addObject($invoice)
                            ->addObject($invoice->getOrder());

            $transaction->save();
            $objectManager->create('Magento\Sales\Model\Order\Email\Sender\InvoiceSender')->send($invoice); // Send Invoice email

            $order->addStatusHistoryComment(__('Invoice generated #%1.', $invoice->getId()))->setIsCustomerNotified(true)->save();
        }
    }
    
}


?>
  • Are you facing issue with inovice generation or the order collection? – Anuraag Oct 19 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.