방법 3단계:
- Extending
WC_Email
class to define the email header, subject, email template for content, etc. - Adding our custom email class to the default WooCommerce email classes using
woocommerce_email_classes
filter - Creating our own email template to be used to generate email content for our custom email.
예제 – 첫 구매 시 환영 메일을 보내는 방법
※ 테마에 직접 추가가 아닌 wrodpress upload 폴더에 코드를 추가하는 방법 활용
- Create directory
crwc-custom-emails
insidewp-content > uploads
. - Create file
crwc-email-functions.php
in the root ofcrwc-custom-emails
directory (crwc-custom-emails > crwc-email-functions.php
). - Create file
class-crwc-welcome-email.php
in the root ofcrwc-custom-emails
directory (crwc-custom-emails > class-crwc-welcome-email.php
). - Create a sub-directory
emails
insidecrwc-custom-emails
directory. - Create a new file
crwc-welcome-email.php
inside theemails
directory (crwc-custom-emails > emails > crwc-welcome-email.php
). - You can also choose to create a sub-directory
plain
insideemails
directory and then createcrwc-welcome-email.php
file insideplain
directory (crwc-custom-emails > emails > plain > crwc-welcome-email.php
). This will be used in case emails use plain content-type.
■ Building custom email class
Add the following code to class-crwc-welcome-email.php
file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly /** * Welcome Email class used to send out welcome emails to customers purchasing a course * * @extends \WC_Email */ class CRWC_Welcome_Email extends WC_Email { /** * Set email defaults */ public function __construct() { // Unique ID for custom email $this->id = 'crwc_welcome_email'; // Is a customer email $this->customer_email = true; // Title field in WooCommerce Email settings $this->title = __( 'Welcome Email', 'woocommerce' ); // Description field in WooCommerce email settings $this->description = __( 'Welcome email is sent when an online training program account is created for the customer after the purchase of the online course.', 'woocommerce' ); // Default heading and subject lines in WooCommerce email settings $this->subject = apply_filters( 'crwc_welcome_email_default_subject', __( 'XYZ Online Training Program', 'woocommerce' ) ); $this->heading = apply_filters( 'crwc_welcome_email_default_heading', __( 'Welcome to Online Training Program', 'woocommerce' ) ); // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar $upload_dir = wp_upload_dir(); $this->template_base = $upload_dir['basedir'] . '/crwc-custom-emails/'; // Fix the template base lookup for use on admin screen template path display $this->template_html = 'emails/crwc-welcome-email.php'; $this->template_plain = 'emails/plain/crwc-welcome-email.php'; // Trigger email when payment is complete add_action( 'woocommerce_payment_complete', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_failed_to_processing', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_failed_to_completed', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_pending_to_processing', array( $this, 'trigger' ) ); // Call parent constructor to load any other defaults not explicity defined here parent::__construct(); } /** * Prepares email content and triggers the email * * @param int $order_id */ public function trigger( $order_id ) { // Bail if no order ID is present if ( ! $order_id ) return; // Send welcome email only once and not on every order status change if ( ! get_post_meta( $order_id, '_crwc_welcome_email_sent', true ) ) { // setup order object $this->object = new WC_Order( $order_id ); // get order items as array $order_items = $this->object->get_items(); //* Maybe include an additional check to make sure that the online training program account was created /* Uncomment and add your own conditional check $online_training_account_created = get_post_meta( $this->object->id, '_crwc_user_account_created', 1 ); if ( ! empty( $online_training_account_created ) && false === $online_training_account_created ) { return; } */ /* Proceed with sending email */ $this->recipient = $this->object->billing_email; // replace variables in the subject/headings $this->find[] = '{order_date}'; $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) ); $this->find[] = '{order_number}'; $this->replace[] = $this->object->get_order_number(); if ( ! $this->is_enabled() || ! $this->get_recipient() ) { return; } // All well, send the email $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); // add order note about the same $this->object->add_order_note( sprintf( __( '%s email sent to the customer.', 'woocommerce' ), $this->title ) ); // Set order meta to indicate that the welcome email was sent update_post_meta( $this->object->id, '_crwc_welcome_email_sent', 1 ); } } /** * get_content_html function. * * @return string */ public function get_content_html() { return wc_get_template_html( $this->template_html, array( 'order' => $this->object, 'email_heading' => $this->email_heading( $this->course_info['program'] ), 'sent_to_admin' => false, 'plain_text' => false, 'email' => $this ) ); } /** * get_content_plain function. * * @return string */ public function get_content_plain() { return wc_get_template_html( $this->template_plain, array( 'order' => $this->object, 'email_heading' => $this->email_heading( $this->course_info['program'] ), 'sent_to_admin' => false, 'plain_text' => true, 'email' => $this ) ); } /** * Initialize settings form fields */ public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'woocommerce' ), 'type' => 'checkbox', 'label' => 'Enable this email notification', 'default' => 'yes' ), 'subject' => array( 'title' => __( 'Subject', 'woocommerce' ), 'type' => 'text', 'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ), 'placeholder' => '', 'default' => '' ), 'heading' => array( 'title' => __( 'Email Heading', 'woocommerce' ), 'type' => 'text', 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ), 'placeholder' => '', 'default' => '' ), 'email_type' => array( 'title' => __( 'Email type', 'woocommerce' ), 'type' => 'select', 'description' => __( 'Choose which format of email to send.', 'woocommerce' ), 'default' => 'html', 'class' => 'email_type wc-enhanced-select', 'options' => array( 'plain' => __( 'Plain text', 'woocommerce' ), 'html' => __( 'HTML', 'woocommerce' ), 'multipart' => __( 'Multipart', 'woocommerce' ), ) ) ); } } |
■ Viewing custom email settings
우커머스 > 설정 > 이메일에서 기본 이메일 리스트에 표시 및 설정을 할 수 있도록 추가된 클래스를 디폴트 클래스 묶음에 추가
on WooCommerce Emails screen under WooCommerce > Settings > Emails
, we’ll need to add this class to the default email classes in WooCommerce. In order to do that, we’ll make use of the woocommerce_email_classes
filter to include custom email class to the default classes.
Add the following code to crwc-email-functions.php
file (inside crwc-custom-emails
directory).
1 2 3 4 5 6 7 8 9 10 11 |
<?php add_filter( 'woocommerce_email_classes', 'crwc_custom_woocommerce_emails' ); function crwc_custom_woocommerce_emails( $email_classes ) { //* Custom welcome email to customer when purchasing online training program $upload_dir = wp_upload_dir(); require_once( $upload_dir['basedir'] . '/crwc-custom-emails/class-crwc-welcome-email.php' ); $email_classes['CRWC_Welcome_Email'] = new CRWC_Welcome_Email(); // add to the list of email classes that WooCommerce loads return $email_classes; } |
■ Creating custom email template
탬플릿 파일 만들고 구성하기.
Add the following code to crwc-welcome-email.php
file (inside emails
directory).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php /** * * Welcome email content template * * The file is prone to modifications after plugin upgrade or alike; customizations are advised via hooks/filters * */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * @hooked WC_Emails::email_header() Output the email header */ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> <p><?php _e( 'Thank you for your purchase of Online training course. Your account has been successfully created over the Online Training Program portal.', 'woocommerce' ); ?></p> <p><?php _e( 'Use the following credentials to login to the portal:', 'woocommerce' ); ?></p> <p> <strong><?php __( 'Login URL: ', 'woocommerce' ) ?></strong><?php _e( 'https://example.com' ); ?><br /> <strong><?php __( 'Username: ', 'woocommerce' ) ?></strong><?php echo make_clickable( esc_attr( $order->billing_email ) ); ?><br /> </p> <p><?php _e( 'Below are the order details for your reference.' ) ?></p> <?php /** * @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::order_meta() Shows order meta data. */ do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::customer_details() Shows customer details * @hooked WC_Emails::email_address() Shows email address */ do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::email_footer() Output the email footer */ do_action( 'woocommerce_email_footer', $email ); |
★ Start making Custom WooCommerce email work
In order to do so, you can just add the following code to your theme’s functions.php
:
1 2 |
$upload_dir = wp_upload_dir(); include_once( $upload_dir['basedir'] . '/crwc-custom-emails/crwc-email-functions.php' ); |
!! _crwc_welcome_email_sent 라는 유저메타를 활용하여 최초 1회만 환영 메일을 발송함.