我在使用以下代码时遇到
Call to a member function getFormattedAddress() on string in vendor/magento/module-email/Model/AbstractTemplate.php
错误。
if ($sendingDays === $this->scopeConfig->getValue(self::PROP_LAST_DAY_REMINDER, ScopeInterface::SCOPE_STORE)) {
$emailTemplateId = $this->scopeConfig->getValue(self::PROP_FINAL_RENEWAL_TEST_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE);
} else {
$emailTemplateId = $this->scopeConfig->getValue(self::PROP_RENEWAL_TEST_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE);
}
$sender = [
'name' => $this->scopeConfig->getValue(self::PROP_CONFIG_ADMIN_FROM_NAME, ScopeInterface::SCOPE_STORE),
'email' => $this->scopeConfig->getValue(self::PROP_CONFIG_ADMIN_FROM_EMAIL, ScopeInterface::SCOPE_STORE)
];
$data = [
'firstname' => $renewData['firstname'],
'lastname' => $renewData['lastname'],
'renewalurl' => $renewalUrl,
'sendingdays' => $sendingDays,
'store' => $renewData['store_name'],
'propUrl' => $this->storeManager->getStore($storeId)->getBaseUrl() . 'prop'
];
$renewalEmailAttempt += 1;
try {
$transport = $this->transportBuilder
->setTemplateIdentifier($emailTemplateId)
->setTemplateOptions([
'area' => Area::AREA_FRONTEND,
'store' => $storeId
])
->setTemplateVars($data)
->setFromByScope($sender, $this->storeManager->getStore()->getId())
->addTo($this->scopeConfig->getValue(self::PROP_RENEWAL_TEST_MODE, ScopeInterface::SCOPE_STORE)
? $this->scopeConfig->getValue(self::PROP_RENEWAL_TEST_EMAIL_RECEPIENT, ScopeInterface::SCOPE_STORE)
: $emailAddress)
->getTransport();
$transport->sendMessage();
} catch (\Exception $e) {
$this->_logger->error('Error sending renewal emails: ' . $renewData['id'] . ' ' . $e->getMessage());
}
我收到此错误是因为我将计划数组传递到
->setTemplateVars($data)
代码。当我转换为 Object 并传递给 setTemplateVars() 时,问题得到了解决。
if ($sendingDays === $this->scopeConfig->getValue(self::PROP_LAST_DAY_REMINDER, ScopeInterface::SCOPE_STORE)) {
$emailTemplateId = $this->scopeConfig->getValue(self::PROP_FINAL_RENEWAL_TEST_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE);
} else {
$emailTemplateId = $this->scopeConfig->getValue(self::PROP_RENEWAL_TEST_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE);
}
$sender = [
'name' => $this->scopeConfig->getValue(self::PROP_CONFIG_ADMIN_FROM_NAME, ScopeInterface::SCOPE_STORE),
'email' => $this->scopeConfig->getValue(self::PROP_CONFIG_ADMIN_FROM_EMAIL, ScopeInterface::SCOPE_STORE)
];
$data = [
'firstname' => $renewData['firstname'],
'lastname' => $renewData['lastname'],
'renewalurl' => $renewalUrl,
'sendingdays' => $sendingDays,
'store' => $renewData['store_name'],
'propUrl' => $this->storeManager->getStore($storeId)->getBaseUrl() . 'prop'
];
$postObject = new DataObject();
$postObject->setData($data);
$renewalEmailAttempt += 1;
try {
$transport = $this->transportBuilder
->setTemplateIdentifier($emailTemplateId)
->setTemplateOptions([
'area' => Area::AREA_FRONTEND,
'store' => $storeId
])
->setTemplateVars(['prop' => $postObject])
->setFromByScope($sender, $this->storeManager->getStore()->getId())
->addTo($this->scopeConfig->getValue(self::PROP_RENEWAL_TEST_MODE, ScopeInterface::SCOPE_STORE)
? $this->scopeConfig->getValue(self::PROP_RENEWAL_TEST_EMAIL_RECEPIENT, ScopeInterface::SCOPE_STORE)
: $emailAddress)
->getTransport();
$transport->sendMessage();
} catch (\Exception $e) {
}
$this->_logger->error('Error sending renewal emails: ' . $renewData['id'] . ' ' . $e->getMessage());
我必须使用类似的代码。
$postObject = new DataObject();
$postObject->setData($data);