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

时间:2016-10-26 15:27:16

标签: php wordpress woocommerce

我做了几次尝试,但在我将变量$downloads显示为Array的电子邮件中。其余部分显示正确,标题和页面链接。 但可下载产品的网址不是。

这是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);
    }
}

2 个答案:

答案 0 :(得分:2)

很可能using UnityEngine; using System.Collections; using System.Collections.Generic; public class Game_Play : MonoBehaviour { public int Num_Players = 11; public int Players = 1; // Use this for initialization void Start () { //Create a list // Using Character_Setup //add 10 characters to list List <Character_Setup> Characters = new List<Character_Setup> (); Characters.Add (new Character_Setup ("Flamethrower")); Characters.Add (new Character_Setup ("Shotgun")); Characters.Add (new Character_Setup ("Rifle")); Characters.Add (new Character_Setup ("Pistol")); Characters.Add (new Character_Setup ("Machine Gun")); Characters.Add (new Character_Setup ("Grenade Launcher")); Characters.Add (new Character_Setup ("Knife")); Characters.Add (new Character_Setup ("Rocket Launcher")); Characters.Add (new Character_Setup ("Throwing Star")); Characters.Add (new Character_Setup ("Sling Shot")); /*foreach (Character_Setup character in Characters) { print (character.Weapon); }*/ // Create 2 teams Off 3 Players // Team 1 // Create Blue Team List<Character_Setup> Blue_Team = new List<Character_Setup> (); Blue_Team.AddRange (Characters); foreach (Character_Setup character in Blue_Team) { print (character.Weapon); } // Team 2 //Create Red Team //List<Character_Setup> Red_Team = new List<Character_Setup> (); //Red_Team.AddRange (Characters); } // Update is called once per frame void Update () { } } 尝试将$downloads.= $product->get_files()分配给字符串变量。您将不得不转换该数组:

对于正常的电子邮件:

array

如果您坚持使用恼人的html电子邮件:

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

答案 1 :(得分:0)

$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";

我不知道数组索引,所以这只是一个例子,但这是实现它的方法。