0

I want to create customer programmatically and want to assign current login customer quote but unable to do that. Here is script, Please see and help me.

// create customer 

$websiteId = Mage::app()->getWebsite()->getId();            
$store = Mage::app()->getStore();
$firstname ="Firstname";
$lastname = "Lastname";
$email = "[email protected]";

$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId($websiteId)
         ->setStore($store)                         
         ->setFirstname($firstname)                                         
         ->setLastname($lastname)
         ->setEmail($email) 
         ->setPassword('test123');

$customer->save();  
$customer_id = $customer->getId();  

// quote assign to new customer 
$session = Mage::getSingleton('checkout/session');
$current_quote_id = $session->getQuoteId();
$currentQuote = Mage::getModel('sales/quote')->load($current_quote_id);
$currentQuoteItem = $currentQuote->getAllItems();

$newQuote = Mage::getModel('sales/quote');
$newQuote->assignCustomer($customer);
$newQuote->addItem($currentQuoteItem);
$newQuote->setQuote($newQuote);
$newQuote->collectTotals()->save();

Above script is creating the customer but not assinging the quote in customer's account.

2 Answers 2

1

EDIT:

This script works. Tested.

try {
    // create customer 
    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();
    $firstname = "Firstname";
    $lastname = "Lastname";
    $email = "[email protected]";
    $password = "test123";

    $customer = Mage::getModel("customer/customer");
    $customer->setWebsiteId($websiteId)
             ->setStore($store)
             ->setFirstname($firstname)
             ->setLastname($lastname)
             ->setEmail($email)
             ->setPassword($password);
    $customer->save();

    // set customer as logged in
    $session = Mage::getSingleton('customer/session');
    $session->login($email,$password);

    // quote assign to new customer
    $quoteObj = Mage::getModel('sales/quote');
    $quoteObj->assignCustomer($customer);
    $quoteObj->setStoreId($store->getId());
    $quoteObj->collectTotals();
    $quoteObj->setIsActive(true);
    $quoteObj->save();
} catch (Exception $ex) {
    var_dump($ex->getMessage());
}
8
  • Thanks you ! But i want to assign current login customer quote to newly added customer. Can you please help me ! Dec 2, 2016 at 10:06
  • I have updated the script, please check this one and let me to sort it out Dec 2, 2016 at 11:59
  • I test this script on my test site and it worked. Result: created client, logged in and added product to the quote. Dec 2, 2016 at 15:46
  • I think, you are going in different scenario. My scenario is as a customer i logged in, added product to cart then i just want to push my all the cart into newly add customer which has recently added. Dec 3, 2016 at 6:12
  • 1
    "setCustomerAsLoggedIn" call is not necessary. It's included in "login" method. So you don't have to load the customer again after customer save. $session = Mage::getSingleton('customer/session'); $session->login($email,$password);
    – Franc
    May 4, 2017 at 10:56
0

It create issue because you $store_id is null in your case, try with some realistic store id and check it works fine.

4
  • Thanks for your reply But it is still not working. Dec 1, 2016 at 3:46
  • i have updated the code. Dec 1, 2016 at 4:17
  • pass $storeid as 1 and try, it's working fine in my system.
    – Keyur Shah
    Dec 1, 2016 at 8:26
  • I have updated the script, please check this one and let me to sort it out Dec 2, 2016 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.