Woocommerce sendet PDF-Anhänge in der Bestell-E-Mail nur für 2 Produkte

Lesezeit: 1 Minute

In WooCommerce habe ich 2 Produkte mit Produktanleitungen im PDF-Format.

Wenn der Kunde eines dieser beiden Produkte kauft, möchte ich sein PDF zusammen mit der Bestellbestätigungs-E-Mail senden.

Im Moment verwende ich diesen Code, um eine PDF-Datei mit der Bestellbestätigungs-E-Mail zu senden –

add_filter( 'woocommerce_email_attachments', 'attach_terms_conditions_pdf_to_email', 10, 3); 

function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {

    $allowed_statuses = array( 'new_order', 'customer_invoice', 'customer_processing_order', 'customer_completed_order' );

    if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {
         $your_pdf_path = get_template_directory() . '/media/test1.pdf'; 
         $attachments[] = $your_pdf_path; 
    } 
return $attachments; 
}

Aber dies sendet PDF an alle Bestell-E-Mails. Ich möchte PDF nur senden, wenn der Kunde eines dieser 2 Produkte gekauft hat.

Ich denke, ich muss eine Bedingung mit Produkt-ID oder so etwas hinzufügen.

  • Sie haben nur die Art der E-Mail (Status) überprüft und nicht, ob das Produkt in der Bestellung enthalten ist.

    – Adrian

    11. März 2016 um 7:42 Uhr

Bestellpositionen können Sie mit abrufen $order->get_items()
Alles, was Sie tun müssen, ist, dieses Array zu durchlaufen und nach entsprechenden Produkt-IDs zu suchen:

function attach_terms_conditions_pdf_to_email ( $attachments, $status , $order ) {

$allowed_statuses = array( 'new_order', 'customer_invoice', 'customer_processing_order', 'customer_completed_order' );

if( isset( $status ) && in_array ( $status, $allowed_statuses ) ) {

    $attachment_products = array(101, 102) // ids of products that will trigger email
    $send_email = false;
    $order_items = $order->get_items();

    foreach ($order_items as $item) { // loop through order items
        if(in_array($item['product_id'], $attachment_products)) { // compare each product id with listed products ids
            $send_email = true;
            break; // one match is found ; exit loop
        }
    }

    if($send_email) {
        $your_pdf_path = get_template_directory() . '/media/test1.pdf'; 
        $attachments[] = $your_pdf_path;
    }
} 
return $attachments; 
}

1366470cookie-checkWoocommerce sendet PDF-Anhänge in der Bestell-E-Mail nur für 2 Produkte

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy