1

i use observer on order place . sales_model_service_quote_submit_before when order is place i get order items and each item has a seller id . i get email from seller ids. and now send mail to those seller which has items in this order.

my observer code is

    public function execute(\Magento\Framework\Event\Observer $observer)
        {
            $orderData=array();
            $order = $observer->getData('order');
            $quote = $observer->getData('quote');
            $quoteItems = $quote->getAllVisibleItems();
            $orderData['customerName']=$order->getCustomerFirstname().' '.$order->getCustomerLastname();
            foreach ($quoteItems as $quoteItem){
                if ($quoteItem->getCurrentSellerId() != ""){

                    $userdetail=$this->userFactory->create()->load($quoteItem->getCurrentSellerId());
                    $orderData['SellerEmail']=$userdetail->getEmail();
                    $orderData['productinfo'][]=$quoteItem->getName();
                    $orderData['productinfo'][]=$quoteItem->getSku();
                    $orderData['productinfo'][]=$quoteItem->getQty();
                    $orderData['productinfo'][]=$quoteItem->getPrice();

                }
            }
            //$this->logger->info(print_r($orderData, true));

            $this->Seller_mail_method($orderData);

            return $this;
        }


public function Seller_mail_method($getOrderParams){
        /* Receiver Detail  */
        $receiverInfo = [
            'name' => 'Reciver Name',
            'email' => $getOrderParams['SellerEmail']
        ];

        $store = $this->storeManager->getStore();
         $TemplateParameter=[
           'customerName'=>$getOrderParams['customerName'],
           'orderItems'=>$getOrderParams['productinfo']
         ];

        $transport = $this->transportBuilder->setTemplateIdentifier(
            'seller_order_email_template'
        )->setTemplateOptions(
            ['area' => 'frontend', 'store' => $store->getId()]
        )->addTo(
            $receiverInfo['email'], $receiverInfo['name']
        )->setTemplateVars(
            $TemplateParameter
        )->setFrom(
            'general'
        )->getTransport();

        try {
            // Send an email
            $transport->sendMessage();
        } catch (\Exception $e) {
            // Write a log message whenever get errors
            $this->logger->critical($e->getMessage());
        }
    }

my seller_email_template.html code is

{{template config_path="design/email/header_template"}} <!-- pathe of template header-->

<table>
    <tr class="email-intro">
        <td>
            <p class="greeting">{{trans " <b>%customerName</b>," customerName=$customerName|row}}</p>

        </td>
    </tr>
    <tr class="email-information">
        <td>

            <table class="email-items">
                <tbody>
                {{layout handle="sellers_email_order_items"  area="frontend"}}
                </tbody>
            </table>
        </td>
    </tr>
</table>

{{template config_path="design/email/footer_template"}}

and phtml file code is

<?php

echo "testing";



?>
<?php //$_order = $block->getOrder() ?>
<?php if ($items): ?>
    <?php //$_items = $_order->getAllItems(); ?>
    <table class="email-items">
        <thead>
        <tr>
            <th class="item-info">
                <?= /* @escapeNotVerified */  __('Items') ?>
            </th>
            <th class="item-qty">
                <?= /* @escapeNotVerified */  __('Qty') ?>
            </th>
            <th class="item-price">
                <?= /* @escapeNotVerified */  __('Price') ?>
            </th>
        </tr>
        </thead>
        <?php foreach ($items as $item): ?>
       // Here want to set items
            <!--            <tr>-->
<!--                <td>-->
<!--                    --><?//= $item["product"]; ?>
<!--                </td>-->
<!--                <td>-->
<!--                    --><?//= $item["qty"]; ?>
<!--                </td>-->
<!--                <td>-->
<!--                    --><?//= $item["price"]; ?>
<!--                </td>-->
<!--            </tr>-->
        <?php endforeach; ?>
        <tfoot class="order-totals">

        </tfoot>
    </table>
<?php endif; ?>

How can set $orderItems in phtm file ? my mail send correctly but now just set content. please guide me .

there is problem in my code how can manage to set orderItems of same sellers. when call mail method these problem will resolve later . But now 1 order 1 item of 1 seller. Thanks

  • Can you please provide some more information about the observer? Which event is used and the complete method. As far as I understand you want to pass data from that observer to the item renderer for transactional emails – HelgeB Apr 9 at 9:23
  • @HelgeB ! i edit my question please review question if you understand . please – HaFiz Umer Apr 9 at 9:44
  • i think may be session use here . to store orderitems in session and get session in phtml . is this suitable or other plz guide me – HaFiz Umer Apr 9 at 9:46
1

OK, if you send the mail from your observer, it is possible to pass the object to the item renderer template. As far as I can see the passing to seller_email_template.html already works since you use $customerName , so you just have to pass the $orderItems to the item renderer block.

I guess with the following changes you should get the order items in your email:

seller_email_template.html:

{{layout handle="sellers_email_order_items"  orderItems=$orderItems area="frontend"}}

items.phtml:

<?php $items = $block->getOrderItems();?>
<?php if ($items): ?>
...

In your observer you should populate the array with the keys you want to access later in the template. I guess you should change the foreach loop like this:

foreach ($quoteItems as $quoteItem){
    if ($quoteItem->getCurrentSellerId() != ""){
        $userdetail=$this->userFactory->create()->load($quoteItem->getCurrentSellerId());
        $orderData['SellerEmail']=$userdetail->getEmail();
        $orderData['productinfo'][] = [
            'name' => $quoteItem->getName(),
            'sku' => $quoteItem->getSku(),
            'qty' => $quoteItem->getQty(),
            'price' => $quoteItem->getPrice()
        ];
    }
}
  • let me try it wait . . . . – HaFiz Umer Apr 9 at 10:11
  • You have also an error where you populate the array, I have updated the answer regarding that error – HelgeB Apr 9 at 10:18
  • HelgeB bro ! error in mail Error filtering template: Codility\VendorOrder\Observer\OrderItemToOrder does not implement BlockInterface My OderItemToOrder.php is not implemented with block interface. <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Product List" design_abstraction="custom"> <block class="Codility\VendorOrder\Observer\OrderItemToOrder" name="seller_items_block" template="Codility_VendorOrder::email/SellerItems.phtml" /> </body> – HaFiz Umer Apr 9 at 10:28
  • my ` Codility\VendorOrder\Observer\OrderItemToOrder` in implemented with ObserverInterface. Thats why <?php $items = $block->getOrderItems();?> not work. how can do that – HaFiz Umer Apr 9 at 10:30
  • Then you need to create a block there you can't use an observer in the layout :-) Just create a simple block which extends \Magento\Framework\View\Element\Template and put that in your layout. – HelgeB Apr 9 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.