I am working on custom extension and i need to send cart items with all information in email like selected custom options, how can I do that?
3 Answers
Starting from Anshu's solution, consider that from any $item
you have in the loop you can read item options via $item->getProductOptions()
;
it will return an array with a attributes_info
key for configurable items; the value associated to that key holds the applied product configuration;
hope it helps
I have quite a similar requirement few months back.
I used below code.
app/etc/modules/Custom_CartEmail.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_CartEmail>
<active>true</active>
<codePool>local</codePool>
</Custom_CartEmail>
</modules>
</config>
app/code/local/Custom/CartEmail/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Custom_CartEmail>
<version>1.0.0</version>
</Custom_CartEmail>
</modules>
<frontend>
<routers>
<cartEmail>
<use>standard</use>
<args>
<module>Custom_CartEmail</module>
<frontName>cartEmail</frontName>
</args>
</cartEmail>
</routers>
</frontend>
</config>
app/code/local/Custom/CartEmail/controllers/cartEmail.php
<?php
$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
$imageHelper = Mage::helper('catalog/image');
?>
<div style="margin:10px 0"> <!-- logo space --> </div>
<table style=" border-collapse:collapse">
<tr>
<th style="border: solid 1px #ccc; padding:5px 10px; background:#f4f4f4;"></td>
<th style="border: solid 1px #ccc; padding:5px 10px; background:#f4f4f4;">Product Name</th>
<th style="border: solid 1px #ccc; padding:5px 10px; background:#f4f4f4;">Unit Price</th>
<th style="border: solid 1px #ccc; padding:5px 10px; background:#f4f4f4;">Quantity</th>
<th style="border: solid 1px #ccc; padding:5px 10px; background:#f4f4f4;">Sub Total</th>
<?php
foreach ($cartItems as $item) {
?>
<tr>
<?php
$productId = $item->getProductId();
$img = $imageHelper->init($item->getProduct(), 'thumbnail');
$itemName = $item->getName();
?>
<td style="border: solid 1px #ccc; padding:5px 10px "><img src="<?php $img; ?>" alt="<?php echo $itemName; ?>" /></td>
<td style="border: solid 1px #ccc; padding:5px 10px "><?php echo $itemName; ?></td>
<td style="border: solid 1px #ccc; padding:5px 10px "><?php echo Mage::helper('core')->formatPrice($item->getPrice()); ?></td>
<td style="border: solid 1px #ccc; padding:5px 10px "><?php echo $item->getQty(); ?></td>
<td style="border: solid 1px #ccc; padding:5px 10px "><?php echo $item->getRowTotalInclTax(); ?></td>
</tr>
<?php
}
?>
</table>
<div style=" margin:10px 0; padding:10px 0; text-align:center; border-top:solid 1px #ccc "> © 2003 - 2014, All rights reserved.</div>
app/code/local/Custom/CartEmail/controllers/IndexController.php
<?php
class Custom_CartEmail_IndexController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$emailTo = $this->getRequest()->getParam('emailto');
ob_start();
include 'cartEmail.php';
$emailContent = ob_get_contents();
$fromEmail = Mage::getStoreConfig('trans_email/ident_general/email');
$fromName = Mage::getStoreConfig('trans_email/ident_general/name');
$toEmail = $emailTo;
$toName = "Hello! Friend";
$body = $emailContent;
$subject = "Cart Products";
try {
$mail = new Zend_Mail();
$mail->setFrom($fromEmail, $fromName);
$mail->addTo($toEmail, $toName);
$mail->setSubject($subject);
$mail->setBodyHtml($body); // here u also use setBodyText options.
// this is for to set the file format
$at = new Zend_Mime_Part($body);
//$at->type = 'application/csv'; // if u have PDF then it would like -> 'application/pdf'
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding = Zend_Mime::ENCODING_8BIT;
//$at->filename = $file;
//$mail->addAttachment($at);
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Email successfully sent.');
} catch (Exception $e) {
//echo $e->getMassage();
Mage::getSingleton('core/session')->addError('There is some error while sending email.');
}
$this->_redirect('checkout/cart');
}
}
Try this code and modify according to your requirement and have a better version of it.
-
Thank you for the answer Anshu, but what about the selected custom options? Commented Feb 22, 2016 at 6:08
-
@GauravJain This is the basic code not the full version, you need to modify it according to your requirement. I think you can get the custom options info from the quote object. Commented Feb 22, 2016 at 6:23
$quote = Mage::getSingleton('checkout/session')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item)
{
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
foreach($options['attributes_info'] as $curopt)
{
// Apply logic on your custom options as per the requirements.
}
}