{"id":6421,"date":"2021-10-26T10:14:00","date_gmt":"2021-10-26T10:14:00","guid":{"rendered":"https:\/\/musilda.cz\/?p=6421"},"modified":"2021-10-26T10:14:00","modified_gmt":"2021-10-26T10:14:00","slug":"odeslani-e-mailu-s-kuponem-zakaznikovi-woocommerce","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/odeslani-e-mailu-s-kuponem-zakaznikovi-woocommerce\/","title":{"rendered":"Odesl\u00e1n\u00ed e-mailu s kup\u00f3nem z\u00e1kazn\u00edkovi WooCommerce"},"content":{"rendered":"\n
WooCommerce obsahuje n\u011bkolik z\u00e1kladn\u00edch transak\u010dn\u00edch e-mail\u016f. My si vytvo\u0159\u00edm vlastn\u00ed e-mail, v kter\u00e9m po\u0161leme z\u00e1kazn\u00edkovi jeho kup\u00f3n. <\/p>\n\n\n\n
V minul\u00e9m n\u00e1vodu jsme si popsali, jak vygenerovat kup\u00f3n za prvn\u00ed n\u00e1kup. <\/p>\n\n\n\n
A dnes n\u00e1vod roz\u0161\u00ed\u0159\u00edme o e-mail, kter\u00e1 z\u00e1kazn\u00edkovi po objedn\u00e1vce po\u0161leme. <\/p>\n\n\n\n
Nejprve si vytvo\u0159\u00edme z\u00e1klad e-mailu. Nebudeme pro n\u00e1vod pou\u017e\u00edvat plugin, tak\u017ee si v \u0161ablon\u011b, ve slo\u017ece woocommerce ud\u011bl\u00e1me podslo\u017eku emails a v n\u00ed podslo\u017eku plain. <\/p>\n\n\n\n
Slo\u017eka emails bude obsahovat html e-mail a plain bude obsahovat e-mail v \u010dist\u00e9m textu.<\/p>\n\n\n\n
Do slo\u017eky emails p\u0159id\u00e1me soubor first-order-coupon-email.php<\/strong> a do slo\u017eky plain soubor first-order-coupon-email-plain.php<\/strong> <\/p>\n\n\n\n Soubor first-order-coupon-email.php<\/strong> <\/p>\n\n\n\n Soubor first-order-coupon-email-plain.php<\/strong> <\/p>\n\n\n\n Nyn\u00ed mus\u00edme vytvo\u0159it t\u0159\u00eddu e-mailu. Ta bude roz\u0161i\u0159ovat WC_Email a nazveme ji WC_First_Order_Coupon_Email<\/p>\n\n\n\n Jako prvn\u00ed do t\u0159\u00eddy p\u0159id\u00e1me construktor. Ten obsahuje id e-mailu, kter\u00e9 budeme pot\u0159ebovat p\u0159i jeho registraci, pak n\u00e1le\u017eitosti, jako title a dal\u0161\u00ed. D\u016fle\u017eit\u00e9 jsou template_html a template_plain, kter\u00e9 mus\u00ed odpov\u00eddat um\u00edst\u011bn\u00ed soubor\u016f ve slo\u017ece woocommerce.<\/p>\n\n\n\n Vytvo\u0159\u00edme trigger, co\u017e je nejd\u016fle\u017eit\u011bj\u0161\u00ed metoda, star\u00e1 se o sestaven\u00ed a odesl\u00e1n\u00ed e-mailu:<\/p>\n\n\n\n N\u00e1sleduj\u00ed dv\u011b metody pro na\u010dten\u00ed \u0161ablon e-mailu:<\/p>\n\n\n\n A na z\u00e1v\u011br si p\u0159id\u00e1me z\u00e1kladn\u00ed nastaven\u00ed pol\u00ed v administraci. Je to jen basic, ale j\u00e1 to pou\u017e\u00edv\u00e1m v\u017edy, proto\u017ee to pak mohu jednodu\u0161e roz\u0161\u00ed\u0159it:<\/p>\n\n\n\n Cel\u00fd k\u00f3d pak vypad\u00e1 takto:<\/p>\n\n\n\n M\u00e1me p\u0159ipravenou t\u0159\u00eddu e-mailu a soubory \u0161ablony. Te\u010f mus\u00edme e-mail zaregistrovat. <\/p>\n\n\n\n Pak ji\u017e m\u00e1me n\u00e1\u0161 e-mail viditeln\u00fd v nastaven\u00ed WooCommerce:<\/p>\n\n\n\n$coupon_code = get_post_meta( $order->get_id(), 'generated_coupon', true );\n?>\n\n<?php do_action('woocommerce_email_header', $email_heading ); ?>\n\n<h2><?php _e( 'D\u011bkujeme za prvn\u00ed n\u00e1kup!', 'musilda' ); ?><\/h2>\n<h3><?php _e( 'Obdr\u017eeli jste slevov\u00fd kup=on na dal\u0161\u00ed n\u00e1kup', 'musilda' ); ?><\/h3>\n<p><?php _e( 'Pro z\u00edsk\u00e1n\u00ed slevy, p\u0159i dal\u0161\u00edm n\u00e1kupu zadete v pokladn\u011b n\u00e1lseduj\u00edc\u00ed k\u00f3d:', 'musilda' ); ?> <?php echo $coupon_code; ?><\/p>\n\n<?php do_action( 'woocommerce_email_footer' ); <\/code><\/pre>\n\n\n\n
$coupon_code = get_post_meta( $order->get_id(), 'generated_coupon', true );\n\necho \"= \" . $email_heading . \" =\\n\\n\";\n\necho __( 'D\u011bkujeme za prvn\u00ed n\u00e1kup!', 'musilda' ) . '\\n\\n';\necho __( 'Obdr\u017eeli jste slevov\u00fd kup=on na dal\u0161\u00ed n\u00e1kup', 'musilda' ) . 'n\\n';\n\necho __( 'Pro z\u00edsk\u00e1n\u00ed slevy, p\u0159i dal\u0161\u00edm n\u00e1kupu zadete v pokladn\u011b n\u00e1lseduj\u00edc\u00ed k\u00f3d:', 'musilda' ) . ' ' , $coupon_code . '\\n\\n';\n\n\necho apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' ) );<\/code><\/pre>\n\n\n\n
Vytvo\u0159en\u00ed t\u0159\u00eddy vlastn\u00edho e-mailu<\/h2>\n\n\n\n
1. krok<\/h3>\n\n\n\n
public function __construct() {\n\n\t \t$this->id = 'wc_first_order_coupon_email';\n\t \t$this->customer_email = true;\n\t \t$this->title = __( 'First order coupon', $this->language_slug );\n\t \t$this->description = __( 'First order coupon', $this->language_slug );\n\t \t$this->heading = __( 'First order coupon', $this->language_slug );\n\t \t$this->subject = __( 'First order coupon from {site_title}', $this->language_slug );\n\n\t \t$this->template_html = 'emails\/stock-alert-customer-email.php';\n\t \t$this->template_plain = 'emails\/plainsstock-alert-customer-email-plain.php';\n\n\t \tparent::__construct();\n\n\t}<\/code><\/pre>\n\n\n\n
2. krok<\/h3>\n\n\n\n
public function trigger( $order_id, $order = false ) {\n\n\t\t$this->setup_locale();\n\n\t\tif ( $order_id && ! is_a( $order, 'WC_Order' ) ) {\n\t\t\t$order = wc_get_order( $order_id );\n\t\t}\n\n\t\tif ( is_a( $order, 'WC_Order' ) ) {\n\t\t\t$this->object = $order;\n\t\t\t$this->recipient = $this->object->get_billing_email();\n\t\t\t$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );\n\t\t\t$this->placeholders['{order_number}'] = $this->object->get_order_number();\n\t\t}\n\n\t\tif ( $this->is_enabled() && $this->get_recipient() ) {\n\t\t\t$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );\n\t\t}\n\n\t\t$this->restore_locale();\n\n \t}<\/code><\/pre>\n\n\n\n
3. krok<\/h3>\n\n\n\n
\/**\n \t * get_content_html function.\n \t *\n \t *\/\n \tpublic function get_content_html() {\n\t \treturn wc_get_template_html( $this->template_html, array(\n\t\t\t'order' => $this->object,\n\t\t\t'email_heading' => $this->get_heading(),\n\t\t\t'sent_to_admin' => false,\n\t\t\t'plain_text' => false,\n\t\t\t'email' => $this,\n\t \t) );\n\n \t}\n\n\t\/**\n \t * get_content_plain function.\n \t *\n \t *\/\n \tpublic function get_content_plain() {\n\t \treturn wc_get_template_html( $this->template_plain, array(\n\t\t\t'order' => $this->object,\n\t\t\t'email_heading' => $this->get_heading(),\n\t\t\t'sent_to_admin' => false,\n\t\t\t'plain_text' => true,\n\t\t\t'email' => $this,\n\t \t) );\n \t} <\/code><\/pre>\n\n\n\n
4. krok<\/h3>\n\n\n\n
\/**\n \t * Initialise settings form fields.\n \t *\/\n \tpublic function init_form_fields() {\n\t \t$this->form_fields = array(\n\t\t \t'enabled' => array(\n\t\t\t \t'title' => __( 'Enable\/Disable', $this->language_slug ),\n\t\t\t \t'type' => 'checkbox',\n\t\t\t \t'label' => __( 'Enable this email notification', $this->language_slug ),\n\t\t\t \t'default' => 'yes',\n\t\t \t),\n\t\t \t'subject' => array(\n\t\t\t \t'title' => __( 'Subject', 'woocommerce' ),\n\t\t\t \t'type' => 'text',\n\t\t\t \t'desc_tip' => true,\n\t\t\t \t'description' => sprintf( __( 'Available placeholders: %s', $this->language_slug ), '<code>{site_title}, {order_date}, {order_number}<\/code>' ),\n\t\t\t \t'placeholder' => $this->get_default_subject(),\n\t\t\t \t'default' => '',\n\t\t \t),\n\t\t \t'heading' => array(\n\t\t\t \t'title' => __( 'Email heading', $this->language_slug ),\n\t\t\t \t'type' => 'text',\n\t\t\t \t'desc_tip' => true,\n\t\t\t \t'description' => sprintf( __( 'Available placeholders: %s', $this->language_slug ), '<code>{site_title}, {order_date}, {order_number}<\/code>' ),\n\t\t\t \t'placeholder' => $this->get_default_heading(),\n\t\t\t \t'default' => '',\n\t\t \t),\n\t\t \t'email_type' => array(\n\t\t\t \t'title' => __( 'Email type', $this->language_slug ),\n\t\t\t \t'type' => 'select',\n\t\t\t \t'description' => __( 'Choose which format of email to send.', $this->language_slug ),\n\t\t\t \t'default' => 'html',\n\t\t\t \t'class' => 'email_type wc-enhanced-select',\n\t\t\t \t'options' => $this->get_email_type_options(),\n\t\t\t \t'desc_tip' => true,\n\t\t \t),\n\t \t);\n \t} <\/code><\/pre>\n\n\n\n
Cel\u00e1 t\u0159\u00edda e-mailu<\/h3>\n\n\n\n
class WC_First_Order_Coupon_Email extends WC_Email {\n\n\t\/**\n \t * Unique identifier\n \t *\n \t *\/\n \tprotected $language_slug = 'musilda';\n\n\n\t\/**\n \t * Set email defaults\n \t *\n \t *\/\n \tpublic function __construct() {\n\n\t \t$this->id = 'wc_first_order_coupon_email';\n\t \t$this->customer_email = true;\n\t \t$this->title = __( 'First order coupon', $this->language_slug );\n\t \t$this->description = __( 'First order coupon', $this->language_slug );\n\t \t$this->heading = __( 'First order coupon', $this->language_slug );\n\t \t$this->subject = __( 'First order coupon from {site_title}', $this->language_slug );\n\n\t \t$this->template_html = 'emails\/stock-alert-customer-email.php';\n\t \t$this->template_plain = 'emails\/plainsstock-alert-customer-email-plain.php';\n\n\t \tparent::__construct();\n\n\t}\n\n\t\/**\n \t * Determine if the email should actually be sent and setup email merge variables\n \t *\n \t *\/\n \tpublic function trigger( $order_id, $order = false ) {\n\n\t\t$this->setup_locale();\n\n\t\tif ( $order_id && ! is_a( $order, 'WC_Order' ) ) {\n\t\t\t$order = wc_get_order( $order_id );\n\t\t}\n\n\t\tif ( is_a( $order, 'WC_Order' ) ) {\n\t\t\t$this->object = $order;\n\t\t\t$this->recipient = $this->object->get_billing_email();\n\t\t\t$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );\n\t\t\t$this->placeholders['{order_number}'] = $this->object->get_order_number();\n\t\t}\n\n\t\tif ( $this->is_enabled() && $this->get_recipient() ) {\n\t\t\t$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );\n\t\t}\n\n\t\t$this->restore_locale();\n\n \t}\n\n\t\/**\n \t * get_content_html function.\n \t *\n \t *\/\n \tpublic function get_content_html() {\n\t \treturn wc_get_template_html( $this->template_html, array(\n\t\t\t'order' => $this->object,\n\t\t\t'email_heading' => $this->get_heading(),\n\t\t\t'sent_to_admin' => false,\n\t\t\t'plain_text' => false,\n\t\t\t'email' => $this,\n\t \t) );\n\n \t}\n\n\t\/**\n \t * get_content_plain function.\n \t *\n \t *\/\n \tpublic function get_content_plain() {\n\t \treturn wc_get_template_html( $this->template_plain, array(\n\t\t\t'order' => $this->object,\n\t\t\t'email_heading' => $this->get_heading(),\n\t\t\t'sent_to_admin' => false,\n\t\t\t'plain_text' => true,\n\t\t\t'email' => $this,\n\t \t) );\n \t}\n\n \t\/**\n \t * Initialise settings form fields.\n \t *\/\n \tpublic function init_form_fields() {\n\t \t$this->form_fields = array(\n\t\t \t'enabled' => array(\n\t\t\t \t'title' => __( 'Enable\/Disable', $this->language_slug ),\n\t\t\t \t'type' => 'checkbox',\n\t\t\t \t'label' => __( 'Enable this email notification', $this->language_slug ),\n\t\t\t \t'default' => 'yes',\n\t\t \t),\n\t\t \t'subject' => array(\n\t\t\t \t'title' => __( 'Subject', 'woocommerce' ),\n\t\t\t \t'type' => 'text',\n\t\t\t \t'desc_tip' => true,\n\t\t\t \t'description' => sprintf( __( 'Available placeholders: %s', $this->language_slug ), '<code>{site_title}, {order_date}, {order_number}<\/code>' ),\n\t\t\t \t'placeholder' => $this->get_default_subject(),\n\t\t\t \t'default' => '',\n\t\t \t),\n\t\t \t'heading' => array(\n\t\t\t \t'title' => __( 'Email heading', $this->language_slug ),\n\t\t\t \t'type' => 'text',\n\t\t\t \t'desc_tip' => true,\n\t\t\t \t'description' => sprintf( __( 'Available placeholders: %s', $this->language_slug ), '<code>{site_title}, {order_date}, {order_number}<\/code>' ),\n\t\t\t \t'placeholder' => $this->get_default_heading(),\n\t\t\t \t'default' => '',\n\t\t \t),\n\t\t \t'email_type' => array(\n\t\t\t \t'title' => __( 'Email type', $this->language_slug ),\n\t\t\t \t'type' => 'select',\n\t\t\t \t'description' => __( 'Choose which format of email to send.', $this->language_slug ),\n\t\t\t \t'default' => 'html',\n\t\t\t \t'class' => 'email_type wc-enhanced-select',\n\t\t\t \t'options' => $this->get_email_type_options(),\n\t\t\t \t'desc_tip' => true,\n\t\t \t),\n\t \t);\n \t}\n\n} \/\/ end class<\/code><\/pre>\n\n\n\n
Registrace e-mailu a jeho odesl\u00e1n\u00ed<\/h2>\n\n\n\n
add_filter( 'woocommerce_email_classes', 'add_first_order_coupon_email' );\nfunction add_first_order_coupon_email( $email_classes ) {\n\n $email_classes['WC_First_Order_Coupon_Email'] = new WC_First_Order_Coupon_Email();\n\n return $email_classes;\n\n}<\/code><\/pre>\n\n\n\n