0

I've seen a couple of things about this but I can't seem to get the value

I have

<field id="template" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Email Template</label>
      <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
      <config_path>cron/email/template</config_path>
</field>

I want to get the email selected in that field, so I created a Helper Data.php with

    /*
     * @return string
     */
    public function getTemplate($scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT)
    {
        return $this->scopeConfig->getValue(
            'cron/email/template',
            $scope
        );
    }

Then I have a cron that runs another helper that looks like

<?php
namespace Custom\OrderCancelled\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\Escaper;
use Magento\Framework\Mail\Template\TransportBuilder;
use Custom\OrderCancelled\Helper\Data;

class Email extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $inlineTranslation;
    protected $escaper;
    protected $transportBuilder;
    protected $logger;
    protected $helper;

    public function __construct(
        Context $context,
        StateInterface $inlineTranslation,
        Escaper $escaper,
        TransportBuilder $transportBuilder,
        Data $helper
    ) {
        parent::__construct($context);
        $this->inlineTranslation = $inlineTranslation;
        $this->escaper = $escaper;
        $this->transportBuilder = $transportBuilder;
        $this->helper = $helper;
        $this->logger = $context->getLogger();
    }

    public function sendEmail()
    {
      $templatetest = $this->helper->getTemplate();

      $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/OrderCancelled.log');
      $logger = new \Zend\Log\Logger();
      $logger->addWriter($writer);
      $logger->info('||' . $templatetest . '||');
    }
}

But the $templatetest returns empty. What's wrong here?

Thanks :D

2
  • can you check template field value save in database, which value store in core_config_data table and check path is cron/email/template ? Jun 16 '20 at 12:26
  • It is 9, I changed and got it working but I think ir ends in the same so idk why this didn't work and the other way did, I'll post in an answer the way it worked
    – Rui Silva
    Jun 16 '20 at 14:16
0

So I got it working just changing this

$templatetest = $this->helper->getTemplate();

To this

$templatetest = $this->helper->getConfig('cron/email/template');

And this

/*
 * @return string
 */
public function getTemplate($scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT)
{
    return $this->scopeConfig->getValue(
        'cron/email/template',
        $scope
    );
}

to this

public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
            $config_path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

But surely I can't understand why the way I did returned empty and the other returned the right value.

EDIT: This is the system.xml

<system>
    <tab id="cron" translate="label" sortOrder="1000">
        <label>Crons</label>
    </tab>
    <section id="cron" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Emails</label>
        <tab>cron</tab>
        <resource>Crons_SystemConfig::cron</resource>
        <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Order Cancelled</label>
            <field id="template" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Email Template</label>
                <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
                <config_path>cron/email/template</config_path>
            </field>
        </group>
    </section>
</system>

Thanks :D

6
  • Can you share your system.xml file where you defined cron/email/template value Jun 16 '20 at 15:12
  • Check the edit, thanks :D
    – Rui Silva
    Jun 16 '20 at 15:22
  • please add config.xml code as well Jun 16 '20 at 15:31
  • I do not have a config.xml, should I? I'm kinda new to this but since it is working I thought I didn't need it
    – Rui Silva
    Jun 16 '20 at 15:37
  • yes you need it.. Jun 16 '20 at 15:38
0

Can you check following blog ..

To add config path ?

https://inviqa.com/blog/how-use-system-configuration-and-helpers-magento-2

If any more help required let me know.

3
  • I used that as example but why do I need the config.xml? What will it do? Since I can get the selected admin option the way I showed in my answer, sorry if I missed something. Thanks :D
    – Rui Silva
    Jun 16 '20 at 15:42
  • check if it exist in core_config_data for cron/email/template ? Jun 16 '20 at 15:44
  • It does, with the ID of the template it's selected in this case it is 9. The config will only add the default right?
    – Rui Silva
    Jun 16 '20 at 15:48

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.