paolo@bimodesign.com | +34 608 61 64 10

E-Commerce

        

Magento, how to create an email with only a PDF file with all the coupons for DD

Before starting to explain how I can modify the structure to generate only one email for every orders, with one PDF file with several pages/coupons, I'm going to explain how Magento manage and define the product types, using these two lines

$product =  Mage::getModel('catalog/product')->load($product_id);
$typeId  = $product->getTypeId();

you'll get, one of:
- virtual
- configurable
- simple
- and more...

Only for the first of them, that is for example a theater or a restaurant coupon, we'll produce a PDF file and its bidi code, to redeem it. For the others, that are for example a television, car, t-shirt, etc... (in case of configurable type, these will have own properties, like colors, size, etc..) we'll not produce the PDF coupon, cause the product will send to the customer home address.
Moreover, we'll describe on the email body, the summary of the order, including all product type, and its quantity.

Now we could start analyzing the Magento code and which type of modifications I made.
Note: I'm going to show only the PHP files that you had to modify to goal it, without explain all the structure of this issue, thus excluding xml file, data base structure, the adyen payment, the Daily Deals module, the pdf library (I use DOMPDF to generate coupon) etc...
These are the four files that I modified:

app/code/community/Madia/Adyen/Model/Process.php
app/code/local/AdolMedia/DailyDeals/Helper/Data.php
app/locale/es_ES/template/email/dailydeals/coupon_self.html
app/design/frontend/default/default/template/dailydeals/customer/coupon.phtml

Note: the email body template is on the "es_ES" standard directory, but we'll use own structure of course.

../Model/Process.php
I modified the _processPostSuccess function where is this switch case, that identified when a adyen payment will authorised

case Madia_Adyen_Model_Event::ADYEN_EVENT_AUTHORISED:
case Madia_Adyen_Model_Event::ADYEN_EVENT_PENDING:

The idea is to send, how input to the sendDealCouponEmail function, all the data of coupons and so create a only email (actually the Daily Deals module produces one email for every coupon). The collection data of the order will be on the $purchased_coupons variable.
This is the code, where the last lines are needed to set the correct state to the order and it doesn't affect this explication.

case Madia_Adyen_Model_Event::ADYEN_EVENT_AUTHORISED:
case Madia_Adyen_Model_Event::ADYEN_EVENT_PENDING:

    $this->_addStatusHistoryComment($order, $params);
    $status = true;
    try {
        $purchased_coupons = Mage::getModel('dailydeals/history')->getCollection()
                                                                 ->addOrderFilter($order->getId());

        Mage::helper('dailydeals')->sendDealCouponEmail($purchased_coupons);
        $order->setEmailSent(true); 

        // set state to Complete
        $state = 'Complete';
        $status = 'complete';
        $comment = 'complete';
        $isCustomerNotified = false;
        $order->setState($state, $status, $comment, $isCustomerNotified);
        $order->save();

        // Update DailyDeals Status 
        // Use mysql direct
        /** @var $coreResource Mage_Core_Model_Resource */
        $coreResource = Mage::getSingleton('core/resource');
                    
        /** @var $conn Varien_Db_Adapter_Pdo_Mysql */
        $conn = $coreResource->getConnection('core_read');
                    
        $conn->update(
            $coreResource->getTableName('dailydeals/history'),
                array('status' => 'C'),
                    array(
                        'order_id = ?' => $order->getId(),
                        )
                    );                   


    } catch (Exception $e) {
        Mage::logException($e);
    }
    break;


../Helper/Data.php
This is the core of this issue, where we'll define the data to add to the email body and how calling and creating the PDF file.
I modified completely the function sendDealCouponEmail, starting to the input data, that is the $purchased_coupons defined into the Process.php code.
Note: How the code is quite large, we could find it here.


../dailydeals/coupon_self.html
This is the code that produces the body of email (thank's Edu!). Note: To understand the parameter values, you have to check the code and the comments in this file


.../customer/coupon.phtml
This is the code that produces the PDF file. You could notice, that I used some php code to get these information:
URL Deal Offer
Note the use of $history


    $deal = Mage::getModel("dailydeals/deal")->load($history->getDealId());
    $urlPathVerOferta= Mage::getBaseUrl().$deal->getProduct()->getUrlPath();  

Insert a Bidi Code or an image
Note, that we difference between show a bidi code (using google API) and show only an image.
(thank's Edu Diaz and Javi!)

    if (trim($url_compra) == '' || $url_compra == null) {
        $url_redencion = Mage::getBaseUrl() . "validarcupones/?ipr=" . $order_id . "&cr=" . $codigo_cupon;
        $cosarara = '
'; }else{ $urlCompra = 'http://yyyyy.xxxx.com/after/pago/loguear?coupon=' . $codigo_cupon; $urlImg = Mage::getBaseUrl() . "skin/frontend/default/moviplans/images/xxx-small.gif"; $cosarara = '

Click here to finish the purchase into the web site provider'; }

Validate date from and to
Note that the "validate date from", is from $deal->getProduct() while the "Validate date to" (the user validate) is from $deal->getDateToUser()

    // Fecha Valida Desde y Valida Hasta

    $dataDealFromDateNoFormat = $deal->getProduct()->getDealFromDate();
    $dataDealFromDateClass = new DateTime($dataDealFromDateNoFormat);
    $dataCouponDesde = $dataDealFromDateClass->format('d.m.Y');

    $dataDealToDateNoFormat = $deal->getDateToUser();
    $dataDealToDateClass = new DateTime($dataDealToDateNoFormat);
    $dataCouponTo = $dataDealToDateClass->format('d.m.Y');    

Order number

    $order = Mage::getModel('sales/order')->load($order_id);
    echo "#" . $order->getRealOrderId();