ctovelox
ctovelox

Reputation: 119

How to show the url downloadable files product in an email

I made a few attempts but in the email where I put the variable $downloads appears Array. The rest appears right, title and link to the page. But the url of the downloadable product not.

This is my code in 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 = "[email protected]";
        $body.= "The following product was added: \n\n" . $post_title . "\n" . $tld_prod_url . "\n" . $downloads . "\n\nThanks,\n";
        wp_mail($to, $subject, $body);
    }
}

Upvotes: 0

Views: 421

Answers (2)

Jose Luis T.R.
Jose Luis T.R.

Reputation: 31

$downloads is an array and therefore you can't output it directly. You must iterate over the array using a foreach loop and create a string. For example:

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

I don't know the array indexes, so this is only an example, but that's the way to do it.

Upvotes: 0

arkascha
arkascha

Reputation: 42925

Most likely $downloads.= $product->get_files() tries to assign an array to a string variable. You will have to convert that array:

For normal email messages that would be:

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

In case you insist on annoying html email messages that is:

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

Upvotes: 2

Related Questions