2

So far I have created file MyModule/ProductAlert/view/frontend/email/stock.html -

<!--@subject {{trans "Products back in stock alert"}} @-->
<!--@vars {
"var alertGrid|raw":"Alert Data Grid",
"var email":"Customer Email",
"template config_path=\"design\/email\/footer_template\"":"Email Footer Template",
"template config_path=\"design\/email\/header_template\"":"Email Header Template"
} @-->
{{template config_path="design/email/header_template"}}

{{var alertGrid|raw}}

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

And I would like to pass my variable email to .phtml file in MyModule/ProductAlert/view/frontend/templates/email/stock.phtml and call it there -

    <?php
use MyModule\ProductAlert\Block\Email\Stock;

/** @var $block Stock */
?>

    <?php if ($_products = $block->getProducts()): ?>
        <?php foreach ($_products as $_product): ?>
            <p><?= __('Hello!') ?></p>
            <p>Your email: <?php $block->getData('email') ?></p> // Email should be here
        <?php endforeach; ?>
    <?php endif; ?>

Currently it returns me nothing. How can I achieve that? Thank you!

Ayaz Ahmed Khan
  • 157
  • 1
  • 11
Mee
  • 63
  • 1
  • 7
  • have you tried {{var email}} instead of $block-getData(email)? . Some code is missing – Rafael Shkembi May 19 '19 at 19:51
  • @RafaelShkembi if I call this in .html file, then it works yes, but I want to pass that email variable from .html file and use it in .phtml file. If I call {{var email}} in .phtml file then it returns "{{var email}}" – Mee May 19 '19 at 20:01
  • I'm not sure if you can achieve that but you can try using sessions. https://magento.stackexchange.com/questions/94265/how-to-set-retrieve-and-unset-session-variables-in-magento-2 – Rafael Shkembi May 19 '19 at 20:06
  • In stock.phtml file display dynamic email data? – Chirag Patel May 20 '19 at 04:38

2 Answers2

1

You can create a method in MyModule\ProductAlert\Block\Email\Stock
You need to inject \Magento\Customer\Model\Session $customerSession, class to get customer Email.

protected $customerSession;

public function __construct(
    ...
    \Magento\Customer\Model\Session $customerSession,
    ...
) {
    ...
    $this->customerSession = $customerSession;
    ...
}

public function getCustomerEmail()
{
    return $this->customerSession->getCustomer()->getEmail(); 
}

And in your .phtml, you call it $block->getCustomerEmail()

fmsthird
  • 4,592
  • 4
  • 17
  • 41
1

You have to create a layout handle for it.

MyModule/ProductAlert/view/frontend/email/stock.html

<tr class="email-information">
    <td>
        {{layout handle="product_alert_email_stock" items=$items  area="frontend"}}
    </td>
</tr>

In the above code, I have declared variable items=$items

create block file for a layout handle.

MyModule\ProductAlert\Block\Email\Stock

<?php
namespace MyModule\ProductAlert\Block\Email\Stock;

class Items extends \Magento\Framework\View\Element\Template
{

}

Now in phtml file write below code.

MyModule/ProductAlert/view/frontend/templates/email/stock.phtml

<?php if ($this->getItems()) :
    foreach ($this->getItems() as $_item) :
    print_r($_item);
    endforeach;
endif; ?>

You can get data using $this->getItems() because we have create variable items=$items

I hope it helps!

Chirag Patel
  • 6,126
  • 2
  • 23
  • 65