【问题标题】:Woocommerce mail funtion returning NULL even mail is sent即使发送了邮件,Woocommerce 邮件功能也会返回 NULL
【发布时间】:2015-05-18 07:47:39
【问题描述】:

我正在尝试使用

在 wordpress 中发送邮件
$subject = 'WC Send Mail Test';

// load the mailer
$mailer = WC()->mailer();
$send = $mailer->send( get_option( 'admin_email' ), $subject, $mailer->wrap_message( $subject, 'a test message' ), '', '' );
if($send){
 echo "Sent";
}else{
 echo "Not sent";
}

此功能工作正常,但我无法跟踪邮件是否已发送。 $send 始终为 NULL

以上代码取自http://develop.woothemes.com/woocommerce/2014/10/2-3-emails/

【问题讨论】:

  • 你无法追踪是什么意思?你想在哪里追踪这个?
  • @mevius 请检查我的更新代码
  • 就像该代码一样简单,我可以向您保证,如果邮件已发送,它应该返回 true...。没有办法返回 null,因为它只能返回 true 或 false。 .
  • @ShravanShrama 去掉最后两个参数。这现在应该返回 true 在您的情况下。
  • @Phorce 不工作..

标签: php wordpress email woocommerce


【解决方案1】:

你应该 var_dump $send,看看返回了什么。不然你为什么不尝试使用wordpress原生邮件功能呢?

$multiple_recipients = array(
    'recipient1@example.com',
    'recipient2@foo.example.com'
);
$subj = 'The email subject';
$body = 'This is the body of the email';
if(wp_mail( $multiple_recipients, $subj, $body )){
 // Mail Sent Successfully
}else{
 // Mail not sent
}

【讨论】:

  • 实际上我的客户希望每个电子邮件格式都应该相同。由于我们在我们的应用程序中使用了 woocommerce,所以我们决定电子邮件格式与 woocommerce 订单电子邮件格式相同,这就是我使用 woocommerce 邮件功能的原因。
  • 我猜你找到了答案,但标准化所有电子邮件的最佳方法是使用这个插件wordpress.org/plugins/wp-email-template。我非常喜欢这个插件,并且通常在整个网站上使用它,所以从 wordpress + woocommerce 发送的所有电子邮件都遵循相同的模板。
  • 感谢您的建议,但我的要求不适合此。
【解决方案2】:

我终于找到了解决方案。我正在尝试挖掘 woocommerce 代码并找到 woocommerce 使用此文件发送电子邮件

woocommerce/includes/class-wc-emails.php

围绕线号。 214 我找到了发送方法。

我改了代码

$email->send( $to, $subject, $message, $headers, $attachments );

$return = $email->send( $to, $subject, $message, $headers, $attachments );
return $return;

                           **OR**

由于更改 wordpress 核心功能是一个坏习惯,所以我得到了一种覆盖 wordpress 核心功能的正确方法。

我刚刚在子主题内创建了一个目录结构,例如

woocommerce/包含

在这里您必须创建一个名为 class-wc-emails.php 的文件并将您编辑的文件代码粘贴到此处。

请不要选择部分,这对我不起作用。

【讨论】:

  • 这可以看作是 Woocommerce 代码中的错误吗?如果是这样,请向他们报告,以便他们为每个人修复它。另请记住,如果您更新 Woocommerce,此更改将被撤消。
  • @Forza 我想是的。我应该向 woocommerce 开发人员询问这个。
  • @Reigel 你在说github.com/woothemes/woocommerce/blob/master/includes/emails/…。我同意你的看法,他们在这里处理退货。但是请看这里github.com/woothemes/woocommerce/blob/master/includes/… 第 214 行周围。我的函数从这里发送电子邮件并且没有返回在这里处理。
  • 更换核心是个坏习惯……你能试试这个最后的建议吗?在这里...尝试将您的$mailer = WC()->mailer(); 更改为$mailer = WC_Email();。如果这不起作用,那么是的,改变它的核心是......;)
  • @Reigel 我知道这是一个坏习惯,但我遇到了致命错误:在使用您的建议时调用未定义的函数 WC_Email()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
相关资源
最近更新 更多