Last active
October 10, 2023 14:34
-
-
Save cdiacon/9cd2c99050426e13904bb99b6384a2e8 to your computer and use it in GitHub Desktop.
dummy orders M2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once '../vendor/fzaninotto/faker/src/autoload.php'; | |
use \Magento\Framework\App\Bootstrap; | |
include('../app/bootstrap.php'); | |
/** | |
* NUMBER OF Orders TO GENERATE. | |
*/ | |
$NUMBER_TO_GENERATE = 100; | |
$html_bootstrap = Bootstrap::create(BP, $_SERVER); | |
$set_objectManager = $html_bootstrap->getObjectManager(); | |
$faker = Faker\Factory::create(); | |
$manager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$get_state = $set_objectManager->get('\Magento\Framework\App\State'); | |
$get_state->setAreaCode('frontend'); | |
for($i=0; $i < $NUMBER_TO_GENERATE;$i++){ | |
$orderData=[ | |
'currency_id' => 'GBP', | |
'email' => $faker->safeEmail, //buyer email id | |
'shipping_address' =>[ | |
'firstname' => $faker->name, //address Details | |
'lastname' => $faker->lastName, | |
'street' => $faker->streetName, | |
'city' => $faker->city, | |
'country_id' => 'IN', | |
'region' => 'xxx', | |
'postcode' => $faker->postcode, | |
'telephone' => $faker->phoneNumber, | |
'fax' => '32423', | |
'save_in_address_book' => 1 | |
], | |
'items' =>[ [ | |
'product_id' => '1', | |
'qty' => '1' | |
]] | |
]; | |
$orderId = createOrder($orderData); | |
echo 'order id : ' . $orderId . PHP_EOL; | |
} | |
function createOrder($orderData) { | |
$manager = \Magento\Framework\App\ObjectManager::getInstance(); | |
$rate = $manager->create('Magento\Quote\Model\Quote\Address\Rate'); | |
$rate | |
->setCode('freeshipping_freeshipping') | |
->getPrice(1); | |
$context = $manager->get('\Magento\Framework\App\Helper\Context'); | |
$storeManager = $manager->get('\Magento\Store\Model\StoreManagerInterface'); | |
$productFactory = $manager->get('\Magento\Catalog\Model\ProductFactory'); | |
$quoteManagement = $manager->get('\Magento\Quote\Model\QuoteManagement'); | |
$customerFactory = $manager->get('\Magento\Customer\Model\CustomerFactory'); | |
$customerRepository = $manager->get('\Magento\Customer\Api\CustomerRepositoryInterface'); | |
$orderService = $manager->get('\Magento\Sales\Model\Service\OrderService'); | |
$cartRepositoryInterface = $manager->get('\Magento\Quote\Api\CartRepositoryInterface'); | |
$cartManagementInterface = $manager->get('\Magento\Quote\Api\CartManagementInterface'); | |
$shippingRate = $manager->get('\Magento\Quote\Model\Quote\Address\Rate'); | |
//init the store id and website id @todo pass from array | |
$store = $storeManager->getStore(); | |
$websiteId = $storeManager->getStore()->getWebsiteId(); | |
//init the customer | |
$customer=$customerFactory->create(); | |
$customer->setWebsiteId($websiteId); | |
$customer->loadByEmail($orderData['email']);// load customet by email address | |
//check the customer | |
if(! $customer->getEntityId()){ | |
//If not avilable then create this customer | |
$customer->setWebsiteId($websiteId) | |
->setStore($store) | |
->setFirstname($orderData['shipping_address']['firstname']) | |
->setLastname($orderData['shipping_address']['lastname']) | |
->setEmail($orderData['email']) | |
->setPassword($orderData['email']); | |
$customer->save(); | |
} | |
//init the quote | |
$cartId = $cartManagementInterface->createEmptyCart(); | |
$cart = $cartRepositoryInterface->get($cartId); | |
$cart->setStore($store); | |
// if you have already buyer id then you can load customer directly | |
$customer= $customerRepository->getById($customer->getEntityId()); | |
$cart->setCurrency(); | |
$cart->assignCustomer($customer); //Assign quote to customer | |
//add items in quote | |
foreach($orderData['items'] as $item){ | |
$product_id = '2';//$item['product_id']; | |
$product = $productFactory->create()->load($product_id); | |
$cart->addProduct( | |
$product, | |
intval($item['qty']) | |
); | |
} | |
//Set Address to quote @todo add section in order data for seperate billing and handle it | |
$cart->getBillingAddress()->addData($orderData['shipping_address']); | |
$cart->getShippingAddress()->addData($orderData['shipping_address']); | |
// Collect Rates and Set Shipping & Payment Method | |
$shippingRate | |
->setCode('freeshipping_freeshipping') | |
->getPrice(1); | |
$shippingAddress = $cart->getShippingAddress(); | |
//@todo set in order data | |
$shippingAddress->setCollectShippingRates(true) | |
->collectShippingRates() | |
->setShippingMethod('flatrate_flatrate'); //shipping method | |
$cart->getShippingAddress()->addShippingRate($rate); | |
$cart->setPaymentMethod('checkmo'); //payment method | |
//@todo insert a variable to affect the invetory | |
$cart->setInventoryProcessed(false); | |
// Set sales order payment | |
$cart->getPayment()->importData(['method' => 'checkmo']); | |
// Collect total and saeve | |
$cart->collectTotals(); | |
// Submit the quote and create the order | |
try { | |
$cart->save(); | |
$cart = $cartRepositoryInterface->get($cart->getId()); | |
$orderId = $cartManagementInterface->placeOrder($cart->getId()); | |
}catch (Exception $e){ | |
} | |
return $orderId; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment