Snippet

Włącz powiadomienia e-mail o niskim stanie magazynowym i braku towaru.

How to enable low stock email notifications in WooCommerceConfiguring out of stock email notifications in WooCommerceSet low stock notifications in WordPressEnable out of stock notifications in WooCommerceEmail notification settings for stock status in WooCommerceHow to receive notifications when a product is out of stock in WooCommerceConfiguring low stock notifications in WooCommerceWooCommerce email notifications for inventory managementSet stock status notifications in WooCommerceConfiguring low stock emails in WooCommerce

Objaśnienie

Aby śledzić stan swojego magazynu, ten kod pomoże Ci otrzymywać powiadomienia e-mail, gdy Twoje produkty są na wyczerpaniu lub są niedostępne w WooCommerce.

Oto jak to działa:

  • Niestandardowe klasy e-maili: Kod dodaje dwie niestandardowe klasy e-maili dla powiadomień o niskim stanie magazynowym i braku towaru. Te klasy definiują treść i format wiadomości e-mail.
  • Wyzwalanie e-maili: Gdy poziom zapasów produktu się zmienia, kod wyzwala odpowiednie powiadomienie e-mail. Dzieje się to za pomocą wbudowanych akcji WooCommerce dla zdarzeń niskiego stanu i braku towaru.
  • Treść e-maila: E-maile zawierają szczegóły, takie jak nazwa produktu, i są wysyłane na adres e-mail administratora. Treść można dostosować za pomocą plików szablonów.

Dzięki temu ustawieniu otrzymasz na czas powiadomienia, co pomoże Ci skutecznie zarządzać poziomami zapasów i uniknąć rozczarowania klientów niedostępnymi produktami.

Kod

1add_action( 'init', 'wp_dudecom_enable_stock_notifications' ); 2 3function wp_dudecom_enable_stock_notifications() { 4 add_filter( 'woocommerce_email_classes', 'wp_dudecom_add_custom_email_classes' ); 5 add_action( 'woocommerce_low_stock_notification', 'wp_dudecom_trigger_low_stock_email' ); 6 add_action( 'woocommerce_no_stock_notification', 'wp_dudecom_trigger_no_stock_email' ); 7} 8 9function wp_dudecom_add_custom_email_classes( $email_classes ) { 10 require_once 'class-wp-dudecom-low-stock-email.php'; 11 require_once 'class-wp-dudecom-no-stock-email.php'; 12 13 $email_classes['WP_Dudecom_Low_Stock_Email'] = new WP_Dudecom_Low_Stock_Email(); 14 $email_classes['WP_Dudecom_No_Stock_Email'] = new WP_Dudecom_No_Stock_Email(); 15 16 return $email_classes; 17} 18 19function wp_dudecom_trigger_low_stock_email( $product ) { 20 $mailer = WC()->mailer(); 21 $mails = $mailer->get_emails(); 22 if ( ! empty( $mails['WP_Dudecom_Low_Stock_Email'] ) ) { 23 $mails['WP_Dudecom_Low_Stock_Email']->trigger( $product ); 24 } 25} 26 27function wp_dudecom_trigger_no_stock_email( $product ) { 28 $mailer = WC()->mailer(); 29 $mails = $mailer->get_emails(); 30 if ( ! empty( $mails['WP_Dudecom_No_Stock_Email'] ) ) { 31 $mails['WP_Dudecom_No_Stock_Email']->trigger( $product ); 32 } 33} 34 35class WP_Dudecom_Low_Stock_Email extends WC_Email { 36 37 public function __construct() { 38 $this->id = 'wp_dudecom_low_stock_email'; 39 $this->title = 'Low Stock Notification'; 40 $this->description = 'This email is sent when a product is low in stock.'; 41 $this->heading = 'Product Low in Stock'; 42 $this->subject = 'Low Stock Alert: {product_name}'; 43 $this->template_html = 'emails/wp-dudecom-low-stock-email.php'; 44 $this->template_plain = 'emails/plain/wp-dudecom-low-stock-email.php'; 45 46 add_action( 'wp_dudecom_low_stock_notification', array( $this, 'trigger' ), 10, 1 ); 47 48 parent::__construct(); 49 } 50 51 public function trigger( $product ) { 52 if ( ! $this->is_enabled() ) { 53 return; 54 } 55 56 $this->object = $product; 57 $this->recipient = get_option( 'admin_email' ); 58 59 $this->placeholders = array( 60 '{product_name}' => $this->object->get_name(), 61 ); 62 63 if ( ! $this->get_recipient() ) { 64 return; 65 } 66 67 $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); 68 } 69 70 public function get_content_html() { 71 return wc_get_template_html( $this->template_html, array( 72 'email_heading' => $this->get_heading(), 73 'product' => $this->object, 74 'sent_to_admin' => true, 75 'plain_text' => false, 76 'email' => $this, 77 ) ); 78 } 79 80 public function get_content_plain() { 81 return wc_get_template_html( $this->template_plain, array( 82 'email_heading' => $this->get_heading(), 83 'product' => $this->object, 84 'sent_to_admin' => true, 85 'plain_text' => true, 86 'email' => $this, 87 ) ); 88 } 89} 90 91class WP_Dudecom_No_Stock_Email extends WC_Email { 92 93 public function __construct() { 94 $this->id = 'wp_dudecom_no_stock_email'; 95 $this->title = 'Out of Stock Notification'; 96 $this->description = 'This email is sent when a product is out of stock.'; 97 $this->heading = 'Product Out of Stock'; 98 $this->subject = 'Out of Stock Alert: {product_name}'; 99 $this->template_html = 'emails/wp-dudecom-no-stock-email.php'; 100 $this->template_plain = 'emails/plain/wp-dudecom-no-stock-email.php'; 101 102 add_action( 'wp_dudecom_no_stock_notification', array( $this, 'trigger' ), 10, 1 ); 103 104 parent::__construct(); 105 } 106 107 public function trigger( $product ) { 108 if ( ! $this->is_enabled() ) { 109 return; 110 } 111 112 $this->object = $product; 113 $this->recipient = get_option( 'admin_email' ); 114 115 $this->placeholders = array( 116 '{product_name}' => $this->object->get_name(), 117 ); 118 119 if ( ! $this->get_recipient() ) { 120 return; 121 } 122 123 $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); 124 } 125 126 public function get_content_html() { 127 return wc_get_template_html( $this->template_html, array( 128 'email_heading' => $this->get_heading(), 129 'product' => $this->object, 130 'sent_to_admin' => true, 131 'plain_text' => false, 132 'email' => $this, 133 ) ); 134 } 135 136 public function get_content_plain() { 137 return wc_get_template_html( $this->template_plain, array( 138 'email_heading' => $this->get_heading(), 139 'product' => $this->object, 140 'sent_to_admin' => true, 141 'plain_text' => true, 142 'email' => $this, 143 ) ); 144 } 145}

Instrukcja

Lokalizacja pliku: Dodaj kod do pliku functions.php swojego motywu lub stwórz plik własnej wtyczki.

Wymagania wstępne:

  • Upewnij się, że WooCommerce jest zainstalowane i aktywowane na Twojej stronie WordPress.

Kroki wdrożenia:

  1. Dostęp do plików WordPress: Użyj klienta FTP lub menedżera plików swojego dostawcy hostingu, aby uzyskać dostęp do plików instalacji WordPress.
  2. Znajdź plik: Przejdź do wp-content/themes/twoj-motyw/functions.php lub stwórz nowy plik wtyczki w wp-content/plugins/.
  3. Edytuj plik: Otwórz plik functions.php lub plik wtyczki w edytorze tekstu.
  4. Dodaj kod: Skopiuj i wklej podany kod do pliku.
  5. Zapisz zmiany: Zapisz plik i upewnij się, że nie ma błędów składniowych.
  6. Prześlij szablony e-mail: Stwórz niezbędne pliki szablonów e-mail:
    • W katalogu swojego motywu stwórz folder o nazwie emails, a wewnątrz niego kolejny o nazwie plain.
    • Stwórz pliki wp-dudecom-low-stock-email.php i wp-dudecom-no-stock-email.php w folderze emails.
    • Stwórz wersje tekstowe wp-dudecom-low-stock-email.php i wp-dudecom-no-stock-email.php w folderze plain.
  7. Przetestuj funkcjonalność: Dostosuj poziomy zapasów produktów w WooCommerce, aby wywołać powiadomienia o niskim i braku zapasów oraz zweryfikuj, że e-maile są wysyłane na adres e-mail administratora.

W celu uzyskania pomocy przy wdrożeniu lub w celu poznania bardziej zaawansowanej funkcjonalności, rozważ skontaktowanie się z wp-dude.com w celu uzyskania fachowej pomocy w zakresie WordPress.