Skip to content

Instantly share code, notes, and snippets.

@adeelq
Created May 4, 2017 13:55
Show Gist options
  • Save adeelq/273a88f4913698f2b1ce0e47d1cf3137 to your computer and use it in GitHub Desktop.
Save adeelq/273a88f4913698f2b1ce0e47d1cf3137 to your computer and use it in GitHub Desktop.
<?php
/**
* This script will create subscribers
* and guest orders using subscriber emails
*
* You will need Faker library https://github.com/fzaninotto/Faker
* Place it in Magento root rename folder to faker
*
* Change value of $NUMBER_TO_GENERATE to desired number
* Change calue of $productId to the one that exist in Magento
*
*/
require_once "app/Mage.php";
require_once 'faker/src/autoload.php';
Mage::app();
//set number to generate here
$NUMBER_TO_GENERATE = 100;
$faker = Faker\Factory::create();
$subscribers = array();
$subscriberEmails = array();
for($i = 0; $i < $NUMBER_TO_GENERATE ; $i++) {
$setEmail = getEmailsimDomain($faker->userName.$i);
$subscriberEmails[] = $setEmail;
$subscribers[] = [
'store_id' => Mage::app()->getStore()->getId(),
'subscriber_email' => $setEmail,
'subscriber_status' => 1
];
}
try {
$resource = Mage::getSingleton('core/resource');
$write = $resource->getConnection('core_write');
$num = $write->insertMultiple(
'newsletter_subscriber',
$subscribers
);
echo 'Successfully inserted subscribers : ' . $num . PHP_EOL;
} catch(Exception $exception)
{
// error message
print_r($exception->getMessage()) . PHP_EOL;
}
//change product id here
$productId = 392;
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
if ($stockItem->getId() > 0 and $stockItem->getManageStock()) {
$qty = $NUMBER_TO_GENERATE;
$stockItem->setQty($qty);
$stockItem->setIsInStock((int)($qty > 0));
$stockItem->save();
}
$product = Mage::getModel("catalog/product")->load($productId);
$buyInfo = array(
"qty" => 1,
);
$info = new Varien_Object($buyInfo);
foreach ($subscriberEmails as $email) {
$quote = Mage::getModel("sales/quote");
$quote->setStoreId(Mage::app()->getStore("default")->getId())
->setCustomerEmail($email)
->addProduct($product, $info);
$addressData = array(
"firstname" => "Test",
"lastname" => "Test",
"street" => "Test Street 1",
"city" => "Test City",
"postcode" => "123456",
"telephone" => "123456",
"country_id" => "US",
"region_id" => "12",
);
$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod("flatrate_flatrate")
->setPaymentMethod("checkmo");
$quote->getPayment()->importData(array("method" => "checkmo"));
$quote->collectTotals()->save();
$service = Mage::getModel("sales/service_quote", $quote);
$service->submitAll();
$order = $service->getOrder();
printf("Created order %s\n", $order->getIncrementId()) . PHP_EOL;
}
function getEmailsimDomain($email){
return $email . '@emailsim.io';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment