0

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

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

我从这个问题的答案开始,并取得了良好的开端: 如何在 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/log/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

我已经用谷歌搜索了这个问题。

4

2 回答 2

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);
        }
于 2013-07-30T15:32:36.207 回答
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();

}

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

于 2013-08-12T19:06:14.993 回答