cancel
Showing results for 
Search instead for 
Did you mean: 

How to save billing address

How to save billing address

I want to save and send to email each entered billing address during checkout process. In magetno 1.7 I edit saveBilling function at /app/code/core/Mage/Checkout/Model/Type/Onepage.php with adding following code. This code not working in magento 1.9.3. What could be wrong?

 

$costumerData = Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress();
$firstname= Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getFirstname();
$lastname= Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getLastname();	
	$street= Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getStreet(1);
	$postcode= Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getPostcode();
	$city= Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getCity();
	$telephone= Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getTelephone();
	$mail= Mage::getSingleton('checkout/session')->getQuote()->getBillingAddress()->getEmail();

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach($items as $item) {		
	$name = $item->getName();
	$sku = $item->getSku();
	$qty = $item->getQty();
	$price = $item->getPrice();
	$cart = "----------------------------------\nName: $name \nSku: $sku \nQty: $qty \nPrice: $price\n"; 
}

$to      = 'our.email@email.com';
$subject = 'Billing address';
$message = "Customer:\n-------------\nName: $firstname \nLastname: $lastname \nStreet: $street \nPost: $postcode $city \nPhone: $telephone \nEmail: $mail\n\n";
$message .= "Items in cart:\n$cart";
$headers = 'From: our.email@email.com' . "\r\n" . "Reply-To: our.email@email.com" . "\r\n" .  'X-Mailer: PHP/' . 'Content-type: text/html; charset=utf-8' . phpversion();
try {
			if($firstname != "test"){
	   $status = mail($to, $subject, $message, $headers);
	   $succ = "Successfully sent to ".$to.". Customer mail is ".$mail.". Status is ".$status;
	   Mage::log($succ, null, 'billingAddress.log');
			}else
			   Mage::log("LOG: TEST", null, 'billingAddress.log');

} catch(Exception $e) {
	$err = "ERROR: ".$e->getMessage()." error sending to ".$to.". Customer mail is ".$mail;		
	Mage::log($err, null, 'billingAddress.log');
}
3 REPLIES

Re: How to save billing address

Everything looks alright, although code is not written by best practice.

If you've tried this on your local, it may be that you haven't configured SMTP (or you don't have it at all).

 

Also, you can try to rewrite it using Magento provided functionalities, like in this blog post:

https://www.pixafy.com/blog/2013/04/sending-emails-programmatically-in-magento/

 

Then you can configure external SMTP service, or install SMTP Pro extension which will make it easier to you.

If this response was helpful to you, consider giving kudos to this post.
If this response solved your problem, click accept as solution to help others solve this issue

Re: How to save billing address

I forgot run compiler. My code works fine and also thanks for your link to best practices.

Re: How to save billing address

You don't have to use the compiler in Magento1 at all. That was useful earlier when there was no good opcode cache. Depending on PHP version, you can utilize APC, Zend Opcache...

If this response was helpful to you, consider giving kudos to this post.
If this response solved your problem, click accept as solution to help others solve this issue