0

I am attempting to work on a module that will send an email to several different dropshippers when an order is placed. I am not getting errors after a few tweeks. although. i'm not receiving any emails no matter what i order. Below are the guideline for the module.

Mind you that i'm the type of person that will open his inward swinging bedroom door, halfway open i forget im opening it and walk right into it. please if you can help dumb it down.

  1. When a product is ordered, send an email to multiple dropshippers individually.
  2. During the ordering process, each order has the potential to include multiple products from various dropshippers.
  3. Each dropshipper will exclusively receive an email containing only the products assigned to them.
  4. The products of the dropshippers will be identified through the Magento Attribute Set.
  5. The dropshippers' email addresses will be identified using the same Magento Attribute Set as mentioned in step 4.
  6. The email that is being sent must include the customer's name, shipping address, ordered products, quantity, and order ID.
  7. The email subject is the order ID

This is the code im working with.

1. Dropshipper/DropshipperNotify/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Dropshipper_DropshipperNotify',
    __DIR__
);

2. Dropshipper/DropshipperNotify/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Dropshipper_DropshipperNotify" setup_version="1.0.0"/>
</config>

3. Dropshipper/DropshipperNotify/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="dropshipper_notify" instance="Dropshipper\DropshipperNotify\Observer\SendEmailToDropshipper" />
    </event>
</config>

4. Dropshipper/DropshipperNotify/Observer/SendEmailToDropshipper.php

<?php
namespace Dropshipper\DropshipperNotify\Observer;

use Magento\Framework\Event\ObserverInterface;
use Dropshipper\DropshipperNotify\Model\Email\DropshipperEmail;

class SendEmailToDropshipper implements ObserverInterface
{
    protected $dropshipperEmail;

    public function __construct(DropshipperEmail $dropshipperEmail)
    {
        $this->dropshipperEmail = $dropshipperEmail;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderIds = $observer->getEvent()->getOrderIds();
        $this->dropshipperEmail->sendEmail($orderIds);
    }
}

5. Dropshipper/DropshipperNotify/Model/Email/DropshipperEmail.php

<?php
namespace Dropshipper\DropshipperNotify\Model\Email;

use Magento\Sales\Model\Order;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Sales\Model\Order\Email\Container\ShipmentIdentity;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ProductRepository;

class DropshipperEmail
{
    protected $transportBuilder;
    protected $shipmentIdentity;
    protected $orderRepository;
    protected $productRepository;

    public function __construct(
        TransportBuilder $transportBuilder,
        ShipmentIdentity $shipmentIdentity,
        OrderRepositoryInterface $orderRepository,
        ProductRepository $productRepository
    ) {
        $this->transportBuilder = $transportBuilder;
        $this->shipmentIdentity = $shipmentIdentity;
        $this->orderRepository = $orderRepository;
        $this->productRepository = $productRepository;
    }

    public function sendEmail($orderIds)
    {
        $order = $this->orderRepository->get($orderIds[0]);
        $shippingAddress = $order->getShippingAddress();

        // Here is where we map attribute set names to dropshipper emails
        $dropshipperEmails = [
            'Gear Bags' => '[email protected]',
            'Rescue Ess' => '[email protected]',
            'North American' => '[email protected]',
            'ZOLL' => '[email protected]',
            // Add more mappings as needed
        ];

        foreach ($order->getAllVisibleItems() as $item) {
            $productId = $item->getProductId();
            $product = $this->productRepository->getById($productId);
            
            $attributeSetName = $product->getAttributeSet();

            // Look up the dropshipper email based on the attribute set
            $dropshipperEmail = isset($dropshipperEmails[$attributeSetName]) 
                ? $dropshipperEmails[$attributeSetName] 
                : null;

            // If we don't have a dropshipper email for this product, skip sending
            if (!$dropshipperEmail) {
                continue;
            }

            // Prepare email data
            $emailData = [
                'customer_name' => $shippingAddress->getName(),
                'order_id' => $order->getId(),
                'product_name' => $item->getName(),
                'qty' => $item->getQtyOrdered(),
                'shipping_address' => $shippingAddress->getFormattedAddress(),
            ];

            // Send email
            $transport = $this->transportBuilder
                ->setTemplateIdentifier('dropshipper_email_template') 
                .setTemplateOptions([
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ])
                .setTemplateVars($emailData)
                .setFrom($this->shipmentIdentity->getEmailIdentity()) 
                .addTo($dropshipperEmail)
                .getTransport();

            $transport->sendMessage();
        }
    }
}

6. Dropshipper/DropshipperNotify/view/dropshipper_email_template.html

<!DOCTYPE html>
<html>
<head>
    <style>
        body {
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
        }
        .email-content {
            margin: 15px;
        }
    </style>
</head>
<body>
<div class="email-content">
    <p>Hello,</p>

    <p>A new order with ID: {{var order_id}} has been placed by {{var customer_name}}.</p>

    <p>Product details:</p>
    <p>Product Name: {{var product_name}}</p>
    <p>Quantity: {{var qty}}</p>

    <p>The shipping address is as follows:</p>
    <p>{{var shipping_address}}</p>

    <p>Thank you,</p>
    <p>Your Store Name</p>
</div>
</body>
</html>

7. Dropshipper/DropshipperNotify/etc/email_templates.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="dropshipper_email_template" label="Dropshipper Email Template" file="dropshipper_email_template.html" type="html" module="Dropshipper_DropshipperNotify" area="frontend"/>
</config>

1 Answer 1

0

Check email sending code.

Replace below code

// Send email
$transport = $this->transportBuilder
    ->setTemplateIdentifier('dropshipper_email_template') 
    ->setTemplateOptions([
        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
    ])
    ->setTemplateVars($emailData)
    ->setFrom($this->shipmentIdentity->getEmailIdentity()) 
    ->addTo($dropshipperEmail)
    ->getTransport();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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