Based on certain aspects of an order I want to replace the email template being used. I'd preferably like to use a plugin to do this as I feel this is the proper Magento 2 way.
Unfortunately most of the methods i'd like to intercept are protected. Thanks Magento. Anyway, the soonest place I can find to do this is in Magento\Sales\Model\Order\Email\SenderBuilder and using a plugin before the send() method.
I'm getting the error :
Missing argument 2 for
Namespace\Moduename\Model\Plugin\ReplaceNewOrderEmailPlugin::beforeSend(),
called in /var/www/public/vendor/magento/framework/Interception/Interceptor.php on line 123
and defined in /var/www/public/vendor/namespace/modulename/Model/Plugin/ReplaceNewOrderEmailPlugin.php on line 9
Here is my code :
di.xml
<type name="Magento\Sales\Model\Order\Email\SenderBuilder">
<plugin name="Company_Namespace::ReplaceNewOrderEmail"
type="Company\Namespace\Model\Plugin\ReplaceNewOrderEmailPlugin"
sortOrder="10"
disabled="false"/>
</type>
Plugin file
use Magento\Sales\Model\Order\Email\SenderBuilder;
class ReplaceNewOrderEmailPlugin
{
public function beforeSend($subject, $result)
{
die('plugin working');
return $result;
}
}
and for good measure, here is the function i'm trying to plugin to
public/vendor/magento/module-sales/Model/Order/Email/SenderBuilder.php
public function send()
{
$this->configureEmailTemplate();
$this->transportBuilder->addTo(
$this->identityContainer->getCustomerEmail(),
$this->identityContainer->getCustomerName()
);
$copyTo = $this->identityContainer->getEmailCopyTo();
if (!empty($copyTo) && $this->identityContainer->getCopyMethod() == 'bcc') {
foreach ($copyTo as $email) {
$this->transportBuilder->addBcc($email);
}
}
$transport = $this->transportBuilder->getTransport();
$transport->sendMessage();
}
Does anyone have any ideas why i'm getting this ?
Best Answer
The before plug-in gives you access to the subject instance and the parameters being passed in. In this case the method you are targeting has no parameters and that's why the second argument doesn't exist. Also, I think you should use the around plug-in instead so that you can run your code or base code depending on the order. Using before, the base code will always run.