Last active
December 8, 2021 11:39
-
-
Save damiencarbery/6af7ab6f2700284c18f8febd6c87c40d to your computer and use it in GitHub Desktop.
Conditionally attach files to WooCommerce order email - Attach a file to the WooCommerce Order Completed email if a specific product or variation is in the order. https://www.damiencarbery.com/2020/02/conditionally-attach-files-to-woocommerce-order-email/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Conditionally attach files to WooCommerce order email | |
Plugin URI: https://www.damiencarbery.com/2020/02/conditionally-attach-files-to-woocommerce-order-email/ | |
Description: Attach a file to the WooCommerce Order Completed email if a specific product or variation is in the order. | |
Author: Damien Carbery | |
Author URI: https://www.damiencarbery.com | |
Version: 0.1 | |
*/ | |
add_filter( 'woocommerce_email_attachments', 'dcwd_conditionally_attach_files_to_order_email', 10, 4 ); | |
function dcwd_conditionally_attach_files_to_order_email( $attachments, $email_id, $object, $email_obj ) { | |
// Only attach files to Completed Order email, otherwise return early. | |
if ( 'customer_completed_order' != $email_id ) { | |
return $attachments; | |
} | |
$upload_dir = wp_get_upload_dir(); | |
// Retrieve items in the order and examine each one. | |
$items = $object->get_items(); | |
foreach ( $items as $item_id => $item ) { | |
$product = $item->get_product(); | |
switch ( $product->get_id() ) { | |
// Belt (ID 27) | |
case 27: $attachments[] = $upload_dir[ 'basedir' ] . '/2018/03/belt.jpg'; | |
break; | |
// Long Sleeve Tee, Medium, Blue (variation ID 63) | |
case 63: $attachments[] = $upload_dir[ 'basedir' ] . '/2018/03/long-sleeve-tee.jpg'; | |
break; | |
// A third attachment option (ID 71) | |
case 71: $attachments[] = $upload_dir[ 'basedir' ] . '/2021/12/third-attachment.jpg'; | |
break; | |
// Two attachments: Front and back views of a t-shirt (ID 84) | |
case 84: $attachments[] = $upload_dir[ 'basedir' ] . '/2021/21/t-shirt-front.jpg'; | |
$attachments[] = $upload_dir[ 'basedir' ] . '/2021/21/t-shirt-back.jpg'; | |
break; | |
} | |
} | |
return $attachments; | |
} |
@Celinevanc : Duplicate the block of 3 lines (comment, case, break).
I have updated the code to add a 3rd attachment option and code to add 2 attachments for a particular product.
thank you Damien, it works!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
How do I add a third product to this code?
Tx!
Celine