Moin, ich arbeite gerade an einer kleinen Funktion für meinen Woocommerce-Shop. Ich möchte den Kunden eine E-Mail senden, wenn sich die Kundengruppe geändert hat.
Ich habe einige Beispiele gefunden, die aber nicht wirklich zufriedenstellend funktionieren.
// in my functions.php
add_action( 'set_user_role', 'new_role_A', 10, 3 );
function new_role_A( $user_id, $role, $old_roles ) {
if($user_id)
{
do_action( 'woocommerce_ats_customer_activated', $user_id );
}
}
//###############################################################################
// in the extra "plugin" in custom-wc-email.php
<?php
/**
* Plugin Name: Custom WooCommerce Email
*/
if ( ! defined( 'ABSPATH' ) )
{
return;
}
/**
* Class Custom_WC_Email
*/
class Custom_WC_Email
{
/**
* Custom_WC_Email constructor.
*/
public function __construct()
{
add_filter( 'woocommerce_email_classes', array( $this, 'register_email' ), 90, 1 );
define( 'CUSTOM_WC_EMAIL_PATH', plugin_dir_path( __FILE__ ) );
}
/**
* @param array $emails
*
* @return array
*/
public function register_email( $emails )
{
require_once 'emails/class-wc-customer-activated.php';
$emails['WC_Customer_Activated'] = new WC_Customer_Activated();
return $emails;
}
}
new Custom_WC_Email();
//###############################################################################
// and in the extra "plugin" file in class-wc-customer-activated.php
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WC_Email' ) ) {
return;
}
/**
* Class WC_Customer_Activated
*/
class WC_Customer_Activated extends WC_Email {
/**
* Create an instance of the class.
*
* @access public
* @return void
*/
function __construct() {
// Email slug we can use to filter other data.
error_log("__construct");
$this->id = 'wc-customer-activated';
$this->title = __( 'Kunde freigeschaltet', 'custom-wc-email' );
$this->description = __( 'Es wird eine E-Mail an den Kunden gesendet, sobald er freigeschaltet wurde.', 'custom-wc-email' );
// For admin area to let the user know we are sending this email to customers.
$this->customer_email = true;
$this->heading = __( 'Account freigeschaltet', 'custom-wc-email' );
$this->subject = sprintf( _x( '[%s] Account freigeschaltet', 'Standardbetreff für E-Mail die an den Kunden gesendet werden, sobald er freigeschaltet wurde.', 'custom-wc-email' ), '{blogname}' );
// Template paths.
$this->template_html = 'emails/wc-customer-activated.php';
$this->template_plain = 'emails/plain/wc-customer-activated.php';
$this->template_base = CUSTOM_WC_EMAIL_PATH . 'templates/';
// Action to which we hook onto to send the email.
add_action( 'woocommerce_ats_customer_activated', array( $this, 'ats_trigger' ), 10, 1 );
error_log("__construct AFTER woocommerce_ats_customer_activated");
parent::__construct();
}
/**
* ATS_Trigger Function that will send this email to the customer.
*
* @access public
* @return void
*/
public function ats_trigger( $user_id ) {
error_log("TRIGGER START");
$customer = new WC_Customer( $user_id );
$this->object = $customer;
$this->recipient = $customer->get_email();
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
return;
}
error_log("TRIGGER vor this->send");
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
error_log("TRIGGER NACH this->send");
}
/**
* Get content html.
*
* @access public
* @return string
*/
public function get_content_html() {
error_log("get_content_html this->template_html:".print_r($this->template_html,1));
error_log("get_content_html this->object:".print_r($this->object,1));
error_log("get_content_html this->get_heading():".print_r($this->get_heading(),1));
error_log("get_content_html this->template_base:".print_r($this->template_base,1));
return wc_get_template_html( $this->template_html, array(
'customer' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this
), '', $this->template_base );
}
/**
* Get content plain.
*
* @return string
*/
public function get_content_plain() {
error_log("get_content_plain");
return wc_get_template_html( $this->template_plain, array(
'customer' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => false,
'plain_text' => true,
'email' => $this
), '', $this->template_base );
}
}
Alles anzeigen
in meiner debug.log habe ich diese Ausgaben gefunden:
[25-Nov-2022 18:58:56 UTC] ats_neuer_benutzer_freigeschaltet user_id:1267
[25-Nov-2022 18:58:56 UTC] ats_neuer_benutzer_freigeschaltet VOR woocommerce_ats_customer_activated benutzerDaten->user_login:andi
[25-Nov-2022 18:58:56 UTC] ats_neuer_benutzer_freigeschaltet NACH woocommerce_ats_customer_activated benutzerDaten->user_login:andi
[25-Nov-2022 18:58:57 UTC] __construct
[25-Nov-2022 18:58:57 UTC] __construct AFTER woocommerce_ats_customer_activated
also erreicht das Programm niemals die trigger Funktion der Klasse, aber warum?.
In den Woocommerce Einstellungen sieht es gut aus, dort kann ich alles einstellen, es wird aber keine Email verschickt.
Warum erreicht das Programm niemals die function ats_trigger?
Kann mir da jemand von Euch weiterhelfen?