Magento:根据售后属性发送电子邮件

时间:2013-07-30 15:12:43

标签: php magento

我正在尝试根据管理后端设置的属性向自定义电子邮件地址发送电子邮件,我对Magento缺乏经验,这有点超出我的范围。我不知道在哪里可以获得订单的逻辑以获得我需要的东西(如果订购了几件物品,使用/新/海报,那么我需要发送3封电子邮件,所以将是一个foreach

我知道我需要挑出每个项目,获取它的属性并运行我的foreach ...只是失去了Magento的技能来完成它。

我从这个问题的答案开始,并有一个良好的开端: How to send category based order emails in magento?

(编辑1& 2:更改了Observer.php文件的内容)

(编辑3:添加了config.xml,Data.php,Company_Module.xml内容)

{MagentoDir} app / etc / modules / Company_Module.xml文件:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <active>true</active>
            <codePool>core</codePool>
        </Company_Module>
    </modules>
</config>

{MagentoDir} app / code / local / Company / Module / etc / config.xml文件:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <version>0.1.0</version>
        </Company_Module>
    </modules>
    <global>
        <models>
            <company_module>
                <class>Company_Module_Model</class>
            </company_module>
        </models>
        <helpers>
            <cmod>
                <class>Company_Module_Helper</class>
            </cmod>
        </helpers>
        <events>
            <sales_order_place_after>
                <observers>
                    <sales_order_place_after_observer>
                        <class>company_module/observer</class>
                        <method>handleOrder</method>
                    </sales_order_place_after_observer>
                </observers>
            </sales_order_place_after>
        </events>
    </global>
</config>

{MagentoDir} app / code / local / Company / Module / Helper / Data.php文件:

<?php

class Company_Module_Helper_Data extends Mage_Core_Helper_Abstract
{

}

?>

{MagentoDir} app / code / local / Company / Module / Model / Observer.php文件:

<?php

class Company_Module_Model_Observer
{
    public function handleOrder($observer)
    {
        $order = $observer->getEvent()->getOrder();   
        // logic to find what was ordered
        $items = $order->getAllVisibleItems();

        $attributeType = array();
        foreach($items as $itemId => $item)
        {
            $product = Mage::getModel('catalog/product')->load($item->getProductId());

            //Once we have the product, we search the attribute set
            $attributeSetId = $product->getAttributeSetId();

            // get attribute
            $type = $product->getDataSource();

            var_dump($type);
            Mage::log('My text' . $type);

            // get item info and create array for each attribute
            $attributeType[$type][] = array(
                'name'      => $item->getName(),
                'unitPrice' => $item->getPrice(),
                'sku'       => $item->getSku(),
                'ids'       => $item->getProductId(),
                'qty'       => $item->getQtyToInvoice()
            );
        }

        foreach($attributeType as $orderType => $orderGroup)
        {
            // send email based on what was ordered
            $emailTemplate = Mage::getModel('core/email_template')->loadDefault('my_template');                            
            $emailTemplateVariables = array();
            $emailTemplateVariables['order'] = json_encode($orderGroup);        
            $emailTemplate->setSenderName('Site');
            $emailTemplate->setSenderEmail('sales@site.com');

            switch ($orderType)
            {
                case 'used':
                    $emailTemplate->setTemplateSubject('Used order at Site');
                    $emailTemplate->send('me@site.com', 'Site', $emailTemplateVariables);
                    break;
                case 'poster':
                    $emailTemplate->setTemplateSubject('Poster order from Site');
                    $emailTemplate->send('me@site.com', 'Site', $emailTemplateVariables);
                    break;
                default:
                    $emailTemplate->setTemplateSubject('New order at Site');
                    $emailTemplate->send('me@site.com', 'Site', $emailTemplateVariables);
                    break;
            }
        }
    }
}

?>

这将是3个单独的电子邮件地址,只是去找我测试

(编辑4:系统日志文件内容)

{MagenetoDir} VAR /数/ SYSTEM.LOG

2013-08-02T16:55:07+00:00 DEBUG (7): Dhtechnologies_Ediconnectorbase_Model_Observer::processDocuments finished
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string(): Entity: line 1: parser error : attributes construct error  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string(): <xml version="1.0"?>  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string():                   ^  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string(): Entity: line 1: parser error : Couldn't find end of Start Tag xml line 1  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string(): <xml version="1.0"?>  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string():                   ^  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string(): Entity: line 1: parser error : Extra content at the end of the document  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string(): <xml version="1.0"?>  in /www/html/lib/Varien/Simplexml/Config.php on line 510
2013-08-02T16:56:01+00:00 ERR (3): Warning: simplexml_load_string():                   ^  in /www/html/lib/Varien/Simplexml/Config.php on line 510

我已经阅读了以下内容: http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method

我搜索了这个问题。

2 个答案:

答案 0 :(得分:0)

试试这个:

class Company_Module_Model_Observer  
{
    public function handleOrder($observer)
    {
        $order = $observer->getEvent()->getOrder();
        $items = $order->getAllVisibleItems();
        foreach($items as $item){
            $product = Mage::getModel('catalog/product')->load($item->getProductId());
            //Once we have the product, we search the attribute Data Source
            $dataSource = $product->getDataSource();
            var_dump($dataSource);
        }

答案 1 :(得分:0)

好的,决定将我的代码放在“success.phtml”页面上,而不是将其添加为“mod”

将success.phtml复制到我的模板目录

/app/design/frontend/{theme}/{theme}/template/checkout/success.phtml

在页面底部我添加了

if($orderNumber = $this->escapeHtml($this->getOrderId()))
{
    $item_arr   = array(); $x = 0;

    $body  = "Order Number: <strong>"   . $orderNumber  . "</strong><br />";

    // fetch the order based on the id
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderNumber);

    // get order total value
    $orderValue = number_format ($order->getGrandTotal(), 2, '.' , $thousands_sep = '');

//  $body .= "Order Value: $"           . $orderValue   . "<br />";
    $body .= '<br />';

    // Get shipping method  
    $shipping_method = $order->_data["shipping_description"];   

    // Get ship-to address information  
    $shipping_address_data = $order->getShippingAddress();  

    $body .= "Shipping Method: <strong>". $shipping_method  . "</strong><br />";

    // Get payment information  
    $payment_method = $order->getPayment()->getMethodInstance()->getTitle();  

    $body .= "Payment Method: <strong>" . $payment_method   . "</strong>*<br /><br />";

    // get order item collection
    $orderItems = $order->getAllItems();

    // Output the ship-to address information  
    $body .= $shipping_address_data['firstname']    . " ";  
    $body .= $shipping_address_data['lastname']     . "<br />";  
    $body .= $shipping_address_data['street']       . "<br />";  
    $body .= $shipping_address_data['city']         . ", ";  
    $body .= $shipping_address_data['region']       . " ";  
    $body .= $shipping_address_data['postcode']     . "<br />";  
    $body .= $shipping_address_data['country_id']   . "<br /><br />";

    $table_start .= '
<table>
    <tr>
        <th>ID</th>
        <th>Description</th>
        <th>Quantity</th>
        <th>Price</th>
    </tr>';

    $used_body = ''; $posters_body = '';
    foreach ($orderItems as $item)
    {
        $product_id     = $item->product_id;
        $product_sku    = $item->sku;
        $product_name   = $item->getName();
        $_product       = Mage::getModel('catalog/product')->load($product_id);
        $cats           = $_product->getCategoryIds();
//      $new_or_used    = $_product->getAttributeText('new_or_used');
        $quantity       = rtrim($item->getQtyOrdered(), '.0000');
        $price          = number_format($item->getPrice(), 2);
        $category_id    = $cats[0]; // just grab the first id
        $category       = Mage::getModel('catalog/category')->load($category_id);
        $category_name  = $category->getName();     // Movies, Gaming, Music, Posters

        if(stripos($product_sku, 'Used'))
        {
            // duplicating items?
            if(!in_array($product_sku, $item_arr))
            {
                $item_arr[$x]   = $product_sku; $x++;

                $used_body .= "
        <tr style=\"border-top: 1px solid #000;\">
            <td style=\"padding: .25em;\">$product_id</td>
            <td style=\"padding: .25em .5em;\">
                SKU: <strong>$product_sku</strong> 
                Category: <strong>$category_name</strong> <br />
                <strong>$product_name</strong>
            </td>
            <td style=\"padding: .25em; text-align: right;\">$quantity</td>
            <td>$ $price</td>
        </tr>";

            }
        }

        else if($category_name == 'Posters')
        {
            // duplicating items?
            if(!in_array($product_sku, $item_arr))
            {
                $item_arr[$x]   = $product_sku; $x++;

                $posters_body .= "
        <tr style=\"border-top: 1px solid #000;\">
            <td> $product_id </td>
            <td style=\"padding: .25em 1em;\">
                SKU: <strong>$product_sku</strong> 
                Category: <strong>$category_name</strong> <br />
                <strong>$product_name</strong>
            </td>
            <td> $quantity </td>
        </tr>";

            }
        }
    }
    $table_end .= '</table><br />* If "Payment Method" is <strong>Check / Money order</strong>, please ensure payment is received BEFORE shipping product.';

    $poster_mail= ($poseter_body != '') ? $body . $table_start . $posters_body . $table_end : NULL;
    $used_mail  = ($used_body != '')    ? $body . $table_start . $used_body . $table_end    : NULL;

    $mail = Mage::getModel('core/email');
    $mail->setFromEmail('orders@store.com');
    $mail->setFromName("Store order queue");
    $mail->setType('html');     // You can use html or text as mail format

    if($used_mail != '')
    {
        $mail->setToName('store');
        $mail->setToEmail('me@store.com');
        $mail->setBody($used_mail);
        $mail->setSubject('Used item ordered at store');
    }

    if($poster_mail != '')
    {
        $mail->setToName('store');
        $mail->setToEmail('me@store.com');
        $mail->setBody($poster_mail);
        $mail->setSubject('Poster ordered at store');
    }

    $mail->send();

}

我需要清理一些,但它可以满足我的需求