2

I am currently developing a drop shipping module for our Magento 2.2.3 store.

For this I am creating a cron job to send a post request to my API that pulls in all the orders where a Magento 2 order hasn't been created. The cron job will then create magento 2 orders for these.

I have created a cron job that pulls orders to our database. this is working without a problem. The second cron job must then Create the orders.

<?php
namespace Vendor\DropShippingreceiver\Cron;
use \Psr\Log\LoggerInterface;

class CreateNewOrders {
protected $logger;
protected $_resource;
protected $curlClient;
protected $_storeManager;

public function __construct(
  \Magento\Framework\App\ResourceConnection $resource,
  LoggerInterface $logger,
  \Magento\Store\Model\StoreManagerInterface $storeManager,
  \Magento\Framework\HTTP\Client\Curl $curl,
  \Magento\Catalog\Model\Product $product,
  \Magento\Framework\Data\Form\FormKey $formkey,
  \Magento\Quote\Model\QuoteFactory $quote,
  \Magento\Quote\Model\QuoteManagement $quoteManagement,
  \Magento\Customer\Model\CustomerFactory $customerFactory,
  \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
  \Magento\Sales\Model\Service\OrderService $orderService
) {
    $this->curlClient = $curl;
    $this->logger = $logger;
    $this->_resource = $resource;
    $this->_storeManager = $storeManager;
    $this->_product = $product;
    $this->_formkey = $formkey;
    $this->quote = $quote;
    $this->quoteManagement = $quoteManagement;
    $this->customerFactory = $customerFactory;
    $this->customerRepository = $customerRepository;
    $this->orderService = $orderService;
}

 public function execute() {
  $this->logger->info("Creating new Orders-----------------------------");

  //Pull uncreated orders from DB. foreach( uncreated order) { create order }
//All other contents of this method have been commented out while testing.
//The only lines that i want to run are the two shown in this method: 
//Log "finished creating" & Log "Creating new"

  $this->logger->info("Finished Creating new Orders---------------------");
 }
}

If i only add the Inject the following dependencies into the constructor, the cron job runs fine with no problems.

\Magento\Framework\App\ResourceConnection $resource,
LoggerInterface $logger,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\HTTP\Client\Curl $curl

However, when i add the other dependencies, the Cron refuses to run at all.

Is there an issue with cron jobs regarding adding too many dependencies? I require all of these to create an order programatically, to my understanding.

Although i mention creating orders, i am not asking how to do this as i already have the code set up for it.

'Why will my Cron job not run when i add the extra dependencies?' all i am trying to make it do for now is output to the logger.

Thanks

EDIT:

When i run the cron, this gets logged to the system.log and debug.log

[2018-06-08 10:36:06] main.INFO: Too late for the schedule Schedule Id: 37125 Job Code: dropshipping_createorders [] []
[2018-06-08 10:36:06] main.INFO: Too late for the schedule Schedule Id: 37128 Job Code: dropshipping_createorders [] []
  • Please share code which is in this method : ` public function execute() { $this->logger->info("Creating new Orders-----------------------------"); //Pull uncreated orders from DB. foreach( uncreated order) { create order } $this->logger->info("Finished Creating new Orders---------------------"); }` – kunj Jun 8 '18 at 10:26
  • you have used $this->logger you must need LoggerInterface $logger, – kunj Jun 8 '18 at 10:27
  • That is all the code in that method for now, i only want it to output to log. I just want the Cron to run with these dependencies – vmp Jun 8 '18 at 10:28
  • the logger is not the issue, when i only add the first 4 dependencies, the Cron runs absolutely fine – vmp Jun 8 '18 at 10:29
  • that is in first four, if you will remove those you must run php bin/magento setup:di:compile. – kunj Jun 8 '18 at 10:31

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.