0

I have a custom email template and I want to add sales rep data for a customer dynamically in that template. For that, I need to check that to which customer email is sending to before the email is actually sent so I can get the customer's id.

How Do I achieve this?

Again, I want to get a customer id before the email is sent to the customer and based on that id I want to add dynamic data to the email template.

My workflow is as following:

A new company is registered from frontend and then this company is on pending approval status. Once the admin puts this company in approved status from admin side, the email is sent to the customer who created the company (I created a custom template for that) and in that email I need to include sales rep data that is assigned to that customer which is why I need customer Id I can get it after checking which customer the email is sent to.

  public function getCustomerId()
    { 
        // this is where I want to get customer Id to whom the email is sending before 
        //email is sent. 
        return $this->userContext->getUserId();
    }

I am calling this function in my .phtml file that is below:

<?php
/** @var $block \Magento\Framework\View\Element\Template */
/** @var $viewModel Magebit\AheadworksSalesRepresentative\ViewModel\SalesRep\Info */

$viewModel = $block->getViewModel();
$customer = $viewModel->getCustomerId();
$salesRep = $viewModel->getAssignedSalesRep($customer);
?>
<?= $customer; ?>
<?php if ($salesRep) :?>
    <section class="aw_sales-rep-manager-info">
        <?php if ($salesRep->getImage()) :?>
            <div class="sales-manager-image">
                <img class="info-img" src="<?= $block->escapeUrl($viewModel->getUrl($salesRep->getImage())) ?>">
            </div>
        <?php endif; ?>
        <div class="info-content">
            <h3 class="sales-manager-name info-text"><strong><?= $block->escapeHtml($salesRep->getDisplayName()) ?></strong></h3>
            <p class="sales-manager-details info-text"><?= $block->escapeHtml($salesRep->getAdditionalInformation()) ?></p>
            <div class="contact-data">
                <p class="sales-manager-email"><?= $block->escapeHtml($salesRep->getEmail()) ?></p>
                <div class="elipse"></div>
                <p class="sales-manager-phone-number"><?= $block->escapeHtml($salesRep->getPhoneNumber()) ?></p>
            </div>
        </div>
    </section>
    <style>
        .actions-toolbar.order-actions-toolbar {
            margin-top: 100px;
        }
        .page-title-wrapper {
            position: relative;
        }

        .aw_sales-rep-manager-info {
            padding: 20px;
            margin-top: 20px;
            border: 1px solid #F2F2F2;
            width: 100% !important;
            max-width: 100% !important;
        }

        .sales-manager-image {
            display: inline-block;
            float: left;
            width: 100px;
            height: 100px !important;
            border-radius: 50%;
            position: relative;
            background-color: #0A246A;
        }

        .info-img {
            width: 100%;
            height: 100%;
        }

        .info-text {
            color: #53545A;
            font-size: 16px;
            font-weight: 400 !important;
        }

        .info-content {
            display: inline-block;
            margin-left: 40px;
        }

        .contact-data {
            width: 100%;
        }

        .sales-manager-email {
            float: left;
            color: #003057;
            font-size: 16px;
            font-weight: 700 !important;
        }

        .elipse {
            width: 4px;
            height: 4px;
            background-color: #DBDBDB;
            display: inline-block;
            border-radius: 50%;
            float: left;
            margin-left: 40px;
            margin-top: 10px;
        }

        .sales-manager-phone-number {
            float: right;
            color: #003057;
            font-size: 16px;
            font-weight: 700 !important;
            margin-left: 50px;
        }

        @media (max-width: 1050px) {
            .actions-toolbar.order-actions-toolbar{
                margin-top: 0px;
            }
        }
        .aw_sales-rep-manager-info {
            max-width: 560px;
        }
    </style>
<?php endif; ?>

and email template

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<!--@subject {{trans "Footer"}} @-->
<!--@vars {
"var store.frontend_name":"Store Name"
} @-->
<p class="capital">Ar cieņu, Capital Latvija!</p>

{{layout area="frontend" handle="sales_rep_info_email"}}

<section class="company-info-section details">

    <div class="company-base-info-block details">

        <h2 class="heading">AS "Capital</h2>
        <p class="paragraph-text">PVN reģ. nr.: LV40003088497</p>
        <p class="paragraph-text">Reģ. nr.: 40003088497</p>

    </div>

    <div class="company-address-block details">

        <h2 class="heading">Juridiskā adrese:</h2>
        <p class="paragraph-text">Ganību dambis 23c, Rīga, LV-1005</p>

    </div>

    <div class="company-actual-address-block details">

        <h2 class="heading">Faktiskā adrese:</h2>
        <p class="paragraph-text">Ganību dambis 23c, Rīga, LV-1005</p>
        <p class="paragraph-text">Tālrunis: 67517822</p>
        <p class="paragraph-text">Fakss: 67517822</p>
        <p class="paragraph-text">E-pasts: <span class="company-email">[email protected]</span></p>

    </div>

    <div class="luminor-info-block details">

        <h2 class="heading">Luminor Bank AS Latvijas filiāle:</h2>
        <p class="paragraph-text">Konts EUR: LV75RIKO0000082836852</p>

    </div>

    <div class="citadele-info-block">
        <h1 class="heading">Citadele AS:</h1>
        <p class="paragraph-text">Konts EUR: LV90PARX0006778570018</p>
    </div>
</section>

                    <!-- End Content -->
                    </td>
                </tr>
                <tr>
                    <td class="footer">
                        <table class="footer-table">
                            <tr>
                                <td>
                                    <a class="logo" href="{{store url=""}}">
                                    <img
                                        {{if logo_width}}
                                        width="{{var logo_width}}"
                                        {{else}}
                                        width="180"
                                        {{/if}}

                                    {{if logo_height}}
                                    height="{{var logo_height}}"
                                    {{/if}}

                                    src="{{var logo_url}}"
                                    alt="{{var logo_alt}}"
                                    border="0"
                                    />
                                    </a>
                                </td>
                            </tr>
                            <tr class="email-footer-links-block">
                                <td class="email-footer-links">
                                    <a class="link" href="#">Biznesa nodaļa</a>
                                    <div class="footer-elipse"/>
                                    <a class="link" href="#">Valsts iestādēm</a>
                                    <div class="footer-elipse"/>
                                    <a class="link" href="#">Risinājumi</a>
                                    <div class="footer-elipse"/>
                                    <a class="link" href="#">E-Veikals</a>
                                </td>
                            </tr>
                            <tr class="email-footer-social-links">
                                <td class="email-social-links">
                                    <a class="linkedin icon" href="#">
                                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path fill-rule="evenodd" clip-rule="evenodd" d="M0 1.49223C0 0.668394 0.671875 0 1.5 0C2.32812 0 3 0.668394 3 1.49223C3 2.31606 2.32812 3 1.5 3C0.671875 3 0 2.31606 0 1.49223ZM0 16V5H3V16H0ZM16 15.9799V10.5C16 7 14.5 5 11.5 5C9.7766 5 8.46809 6.15539 8 7V5H5V16H8V10.5C8 9.09232 8.67021 8 10.5 8C12.5 8 13 9.2532 13 10.5V16L16 15.9799Z" fill="white"/>
                                        </svg>
                                    </a>
                                    <a class="facebook icon" href="https:#/">
                                        <svg width="8" height="16" viewBox="0 0 8 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M5 5V3.5C5 3.23478 5.10536 2.98043 5.29289 2.79289C5.48043 2.60536 5.73478 2.5 6 2.5H7V0H5C4.20435 0 3.44129 0.31607 2.87868 0.87868C2.31607 1.44129 2 2.20435 2 3V5H0V8H2V16H5V8H7L8 5H5Z" fill="white"/>
                                        </svg>
                                    </a>
                                    <a class="instagram icon" href="#">
                                        <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path fill-rule="evenodd" clip-rule="evenodd" d="M12 0H4C1.79086 0 0 1.79086 0 4V12C0 14.2091 1.79086 16 4 16H12C14.2091 16 16 14.2091 16 12V4C16 1.79086 14.2091 0 12 0ZM11.9567 7.41327C12.0801 8.24551 11.938 9.09548 11.5505 9.84228C11.163 10.5891 10.5499 11.1947 9.79836 11.5729C9.04684 11.9512 8.19518 12.0829 7.36452 11.9492C6.53386 11.8155 5.76649 11.4233 5.17158 10.8284C4.57665 10.2335 4.18447 9.46614 4.0508 8.63548C3.91714 7.80482 4.04881 6.95316 4.42706 6.20164C4.80533 5.45012 5.41092 4.83701 6.15772 4.44951C6.90452 4.06201 7.75449 3.91986 8.58673 4.04327C9.43565 4.16915 10.2216 4.56473 10.8284 5.17158C11.4353 5.77842 11.8308 6.56434 11.9567 7.41327ZM10 8C10 9.10457 9.10457 10 8 10C6.89543 10 6 9.10457 6 8C6 6.89543 6.89543 6 8 6C9.10457 6 10 6.89543 10 8ZM12 5C12.5523 5 13 4.55228 13 4C13 3.44772 12.5523 3 12 3C11.4477 3 11 3.44772 11 4C11 4.55228 11.4477 5 12 5Z" fill="white"/>
                                        </svg>
                                    </a>
                                    <a class="twitter icon" href="#">
                                        <svg width="16" height="13" viewBox="0 0 16 13" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M16 1.5378C15.3981 1.8003 14.7616 1.97476 14.11 2.0558C14.7967 1.64843 15.3101 1.00336 15.553 0.242799C14.91 0.624135 14.2065 0.892673 13.473 1.0368C13.0232 0.556934 12.4395 0.223395 11.7977 0.0794801C11.1559 -0.0644347 10.4856 -0.0120742 9.87399 0.229764C9.26233 0.471602 8.73751 0.891745 8.36767 1.43564C7.99782 1.97954 7.80004 2.62207 7.8 3.2798C7.79741 3.53118 7.8229 3.78207 7.876 4.0278C6.57106 3.96421 5.29424 3.62591 4.12902 3.03502C2.96379 2.44414 1.93639 1.61397 1.114 0.598798C0.69185 1.32042 0.561093 2.17597 0.74843 2.99073C0.935767 3.8055 1.42707 4.51803 2.122 4.9828C1.60309 4.97014 1.09481 4.83296 0.64 4.5828V4.6188C0.641802 5.37598 0.903939 6.10952 1.38243 6.69635C1.86093 7.28318 2.52668 7.68761 3.268 7.8418C2.98739 7.91575 2.69819 7.95207 2.408 7.9498C2.19959 7.95351 1.99139 7.93473 1.787 7.8938C1.99894 8.54482 2.40749 9.11414 2.95639 9.52335C3.5053 9.93257 4.16756 10.1616 4.852 10.1788C3.69103 11.0871 2.25908 11.5801 0.785 11.5788C0.522491 11.5783 0.260287 11.5609 0 11.5268C1.49988 12.4933 3.24768 13.0046 5.032 12.9988C6.26023 13.0073 7.4779 12.7717 8.61431 12.3057C9.75071 11.8396 10.7832 11.1524 11.6518 10.284C12.5204 9.4156 13.2078 8.38327 13.6741 7.24696C14.1404 6.11065 14.3762 4.89303 14.368 3.6648C14.368 3.5198 14.363 3.3798 14.356 3.2408C15.003 2.77779 15.5601 2.20068 16 1.5378Z" fill="white"/>
                                        </svg>
                                    </a>
                                    <a class="#">
                                        <svg width="16" height="11" viewBox="0 0 16 11" fill="none" xmlns="http://www.w3.org/2000/svg">
                                            <path d="M8.00391 11C6.05691 11 3.55597 10.97 2.54297 10.913C1.61497 10.849 1.12871 10.7321 0.678711 9.93506C0.228711 9.13806 0 7.73496 0 5.50696V5.49695C0 3.23695 0.208711 1.86904 0.678711 1.06104C1.11271 0.294035 1.58392 0.147041 2.54492 0.092041C3.56592 0.024041 6.12391 0 8.00391 0C9.88391 0 12.435 0.024041 13.458 0.092041C14.419 0.147041 14.8913 0.292035 15.3203 1.06104C15.7913 1.86104 16.001 3.22802 16.001 5.49902V5.50598C16.001 7.77798 15.7923 9.14501 15.3223 9.93701C14.8923 10.699 14.422 10.849 13.458 10.913C12.448 10.969 9.94991 11 8.00391 11ZM6.00391 2.5V8.5L11.0039 5.5L6.00391 2.5Z" fill="white"/>
                                        </svg>
                                    </a>
                                </td>
                            </tr>
                        </table>

                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<!-- End wrapper table -->
</body>

1 Answer 1

0

Be aware that this will only works if the template knows the customer, you might have to add cacheable = false to the layout to make it work depending on where you want to use it.

protected $customerSession;

/**
 * Construct
 *
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Customer\Model\Session $customerSession
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    array $data = []
) {
    parent::__construct($context, $data);

    $this->customerSession = $customerSession;
}

public function getCustomerId()
{

    return $this->customerSession->getCustomer()->getId();
  
}
3
  • What do u mean by if template knows the customer. In my template, I do have customer name added and template can access customer name. Below is the code ` <p class="paragraph-text"> {{trans 'Laipni lūgti Capital Latvija. Jūsu uzņēmums %company_name ir apstiprināts un Jums ir piešķirtas administrātora tiesības.' company_name=$company.getName() |raw}} </p> ` But, using customerSession actually makes the dynamic data template disappear. There is nothing shown.
    – LosMos
    Aug 24, 2022 at 13:21
  • Code is giving you the current customer. I supposed the piece of code was triggered directly by the customer action. If it is, then it should work. What i meant by if the template know the customer is that sometime in your template declaration you have to add cacheable false to allow get customer not to be null, here is an example from the dashboard . <block class="Magento\Customer\Block\Account\Dashboard" name="msg-pro" template="Vendor_Customer::msg-pro.phtml" before="-" cacheable="false" /> If your issue is that you can't pass the variable, it's different issue than what was asked.
    – Claims
    Aug 24, 2022 at 13:58
  • Sorry for the confusion. I have edited my question. I do not need current customer because it will be wrong data. My workflow is as following: A new company is registered from frontend and then this company is on pending approval status. Once the admin puts this company in approved status from admin side, the email is sent to the customer who created the company (I created a custom template for that) and in that email I need to include sales rep data that is assigned to that customer which is why I need customer Id I can get it after checking which customer the email is sent to.
    – LosMos
    Aug 25, 2022 at 7:33

Your Answer

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

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