如何在电子邮件中显示url可下载文件产品


How to show the url downloadable files product in an email

我做了一些尝试,但在电子邮件中,我把变量$downloads出现Array。其余部分显示在右边,标题和页面链接。但可下载产品的url没有。

这是我在functions.php中的代码:
add_action('draft_to_publish', 'my_product_add');
function my_product_add($post)
{
    if ($post->post_type == "product") {
        $productId = $post->ID;
        $post_title = get_the_title($post_id);
        $tld_prod_url = esc_url(get_permalink($post_id));
        $subject = "Product Added Notification";
        $product = new WC_Product($post_id);
        $downloads.= $product->get_files();
        $to = "me@test.com";
        $body.= "The following product was added: 'n'n" . $post_title . "'n" . $tld_prod_url . "'n" . $downloads . "'n'nThanks,'n";
        wp_mail($to, $subject, $body);
    }
}

很可能$downloads.= $product->get_files()试图将array分配给字符串变量。你必须转换数组:

对于普通的电子邮件消息,将是:

$downloads.= implode("'r'n", $product->get_files());

如果你坚持恼人的html电子邮件信息,那就是:

$downloads.= implode("<br>'r'n", $product->get_files());

$downloads是一个数组,因此你不能直接输出它。必须使用foreach循环遍历数组并创建一个字符串。例如:

$downloadsString = '';
foreach($downloads as $download){
    $downloadsString = "<a href='".$download['file']."'>".$download['name']."</a>'r'n";
}
$body.= "The following product was added: 'n'n" . $post_title . "'n" . $tld_prod_url . "'n" . $downloadsString . "'n'nThanks,'n";

我不知道数组的索引,所以这只是一个例子,但这是这样做的