The filter described below has been available since ShopMagic 2.34.0. The code example uses the argument types passed by ShopMagic 4.8.9.
Files selected in the PDF Attachments field are attached to each email sent by that action. Use the filter below to attach different PDF files depending on the customer or other data available to the action.
Adding custom PDF files#
The shopmagic/core/action/send_mail/attachments_paths filter modifies the list of attachment paths before the email is sent.
The callback receives three arguments:
$attachments(string[]) — absolute server paths of the attachments selected in the action.$action_fields(\ShopMagicVendor\Psr\Container\ContainerInterface) — saved action settings. Usehas()andget()to access values such assubject_value.$provided_data(\ShopMagicVendor\Psr\Container\ContainerInterface) — data available to the action. Usehas()andget()with a class or interface name, such as\WPDesk\ShopMagic\Customer\Customer::class. Available data depends on the event.
Return an array of absolute paths to files on the server, not file URLs. The files must exist and be readable when the action runs. This filter does not generate PDF files.
ShopMagic skips files that do not exist. It accepts existing files when their detected MIME type is application/pdf or their extension is exactly pdf. Skipping an attachment does not stop the email from being sent.
Append paths to $attachments to keep the attachments selected in the action. Returning a new array containing only your paths replaces the existing attachment list. Return an empty array to remove all attachments.
Code example#
Add the following code to a custom plugin or a PHP file in wp-content/mu-plugins/. If you create a new PHP file, start it with the <?php opening tag.
This example appends ticket-{customer_id}.pdf for a registered customer when the action’s Subject is exactly Grab your ticket. Replace /absolute/path/to/tickets/ with the server directory containing your PDF files.
add_filter(
'shopmagic/core/action/send_mail/attachments_paths',
'attach_dynamic_files',
10,
3
);
function attach_dynamic_files(
array $attachments,
\ShopMagicVendor\Psr\Container\ContainerInterface $action_fields,
\ShopMagicVendor\Psr\Container\ContainerInterface $provided_data
): array {
// Apply only to actions with this saved subject.
if (
! $action_fields->has( 'subject_value' )
|| $action_fields->get( 'subject_value' ) !== 'Grab your ticket'
) {
return $attachments;
}
$customer_class = \WPDesk\ShopMagic\Customer\Customer::class;
if ( ! $provided_data->has( $customer_class ) ) {
return $attachments;
}
$customer = $provided_data->get( $customer_class );
// Skip guest customers.
if ( $customer->is_guest() ) {
return $attachments;
}
$file_path = '/absolute/path/to/tickets/ticket-'
. $customer->get_id()
. '.pdf';
// Keep existing attachments and append the customer's PDF.
if ( is_file( $file_path ) && is_readable( $file_path ) ) {
$attachments[] = $file_path;
}
return $attachments;
}
The subject condition checks the saved field value before placeholders are processed. It applies to every email action with that subject, including actions in other automations.
The filename uses the customer’s account ID, not the order ID. The same customer therefore receives the same file on subsequent executions. If the customer is unavailable, is a guest, or the file is missing or unreadable, the example returns the original attachment list.
