<?php
class Bysoft_Mycheckout_Model_Addtocart{
/**
* When occur FATAL ERROR, it will redo the run function
* @param unknown $i
*/
public function shutdown($i) {
var_dump('already shutdown.');
$last_error = error_get_last();
var_dump($last_error);
register_shutdown_function(array(&$this,'shutdown',$i));
$this->run($i);
}
/**
* main function:
* 1.get the number $i `th user,
* 2.login it
* 3.get two rand product and add them to user's shopping cart, before that, clear his shopping cart
*/
public function run($i) {
umask(0);
Mage::app('admin');
$this->i=$i;
register_shutdown_function(array(&$this,'shutdown',$i));
Mage::app()->setCurrentStore(1);
$websiteId = 1;
$arr = $this->getProductData();
$email = 'test_' . $i . '@test.com';
$password = 'password';
if ($this->login_user($email,$password)) {
$this->cleanCart();
var_dump($this->getCartItemNum());
if($this->getCartItemNum() < 2) {
$product_ids = array();
for ($j = 1; $j <= 2; ++$j) {
$product_ids[] = $this->saveRandProduct($arr,$j);
}
$cart = Mage::getModel('checkout/cart');
$cart->init();
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(1);
$customer->loadByEmail($email);
$customer->load($customer->getId());
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->setCustomer($customer);
var_dump($quote->getCustomerId());
$cart->addProductsByIds($product_ids);
$cart->save();
}
Mage::getSingleton('customer/session')->logout()->renewSession();
}
}
/**
* get all useable product sku from a csv file.
* @return multitype:unknown
*/
public function getProductData() {
//get product data from csv file(which products have enough stock)
$file_path = BP . DS . 'media' . DS. 'product_inventory' . DS .'data.csv';
$fp = fopen($file_path, 'r');
$res = array();
$count = 0;
while ($data = fgetcsv($fp)) {
if (isset($data[0]) && trim($data[0])!= '' && $count != 0) {
$res[] = $data;
}
++$count;
}
return $res;
}
/**
* login a user
*/
public function login_user($email,$password) {
$session = Mage::getSingleton('customer/session');
try {
$session->login( $email, $password );
$session->setCustomerAsLoggedIn($session->getCustomer());
var_dump($email . ' login successed!');
return true;
} catch( Exception $e ) {
return false;
}
}
/**
* clean current logined user's shopping cart.
*/
public function cleanCart() {
if ($this->getCartItemNum() >0) {
Mage::getSingleton('checkout/cart')->truncate()->save();
var_dump('remove all item from cart');
}
}
/**
* count current user's shopping cart item number
*/
public function getCartItemNum() {
//get cart item (current login user)
$cart = Mage::getSingleton('checkout/session');
$cart->init();
$quote = $cart->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
//var_dump($item->getData());
}
return count($items);
}
/**
* get random product from a array.
* @param array $arr
* @param int $j
* @return int
*/
public function saveRandProduct($arr,$j) {
while (!$_product) {
$num = (rand(($j-1)*(floor(count($arr)/2))+1,$j*floor(count($arr)/2)));
$product_model = Mage::getModel('catalog/product');
$productid = $product_model->getIdBySku($arr[$num-1][0]);
$_product = $product_model->load($productid);
}
return $productid;
}
}