title: WooCommerce trigger custom email via AJAX
body: <p>hi and thanks for stopping by.
  i am currently writing a plugin for wordpress. i need a button inside a certain page, that triggers an email-notification. i figured it would be good to use the woocommerce email functionality, since it is a customer email and i'd like to use the woocommerce-email-templates, too. </p>
  
  <p>in my plugin i include my class extension via</p>
  
  <pre><code>function add_wc_custom_email( $email_classes ) {
      require( 'includes/class-wc-custom-email.php' );
      $email_classes['WC_Custom_Email'] = new WC_Custom_Email();
  
      return $email_classes;
  }
  add_filter( 'woocommerce_email_classes', 'add_wc_custom_email' );
  //if i write a print_r(something) inside this function, just to alert if this filters hits, i don't see a result, so i think the error is here.. the filter gets hit under woocommerce-&gt;settings-&gt;emails but not, if you simply load a page in the backend 
  </code></pre>
  
  <p>in the file class-wc-custom-email.php i have my class extension</p>
  
  <pre><code>class WC_Custom_Email extends WC_Email {
      public function __construct() {
          …
      }
      public function mail( $var1, $var2, $var3 ) {
          …
      }
  }
  </code></pre>
  
  <p>my ajax looks like this</p>
  
  <pre><code>( function( $ ) {
      $('#button1').on( "click", function() {
          $.post(
              ajaxurl,
              {
                  'action' : 'jnz_email_custom',
                  'page_id': '&lt;?php the_ID(); ?&gt;',
              },
              function ( response ) {
                  console.log( "triggered", response )
              }
          );
      });
  })(jQuery);
  </code></pre>
  
  <p>the ajax handler looks like this</p>
  
  <pre><code>add_action('wp_ajax_jnz_email_custom', 'jnz_email_custom');
  function jnz_email_custom() {
      $message = email_custom( $_POST['page_id'] );
      $response = array(
          'what'   =&gt; 'email_custom_triggered',
          'action' =&gt; 'email custom triggered',
          'id'     =&gt; $_POST['page_id'],
          'data'   =&gt; $message,
      );
      $xmlResponse = new WP_Ajax_Response( $response );
      $xmlResponse-&gt;send();
  }
  </code></pre>
  
  <p>the function that i trigger with ajax</p>
  
  <pre><code>function email_custom( $page_id = 2728 ) {
      $field = get_field( 'something', $page_id )[0];
      //get_field() is a function by plugin 'Advanced Custom Fields' and works fine
  
      function somethingSomething( $field ) {
          …
          return $results;
      }
  
      foreach ( $results as $result ) :
          $email = new WC_Custom_Email();
          $email-&gt;mail( $result['details']['name'], $result['details']['email'], $result['something'] );
      endforeach;
  
      return json_encode( $results, JSON_UNESCAPED_UNICODE );
      //this is the $xmlResponse i get from the ajax call and looks fine (but only works, if i uncomment the 'new WC_Custom_Email()' stuff. otherwise i see the "Fatal Error" thingy)
  }
  </code></pre>
  
  <p>the response is: <code>Fatal error: Class 'WC_Custom_Email' not found in …</code></p>
  
  <p>all the functions work fine, if i return the <code>$results</code> i see all i want and get no errors.</p>
  
  <p>same is, when i change it to <code>new WC_Email()</code>. so my guess is, that the woocommerce functionality isn't loaded into my admin->edit_page_xy screen. so the big question is: how would i load the woocommerce functionality (or only the email-functionality) into my plugin..??</p>
  
  <p>hope this is somewhat clear and makes any sense. i only know little php and am completely new to oop.</p>