Search code examples
phpwordpresswoocommercestockemail-notifications

How customize WooCommerce no stock notification email


Kindly am trying to customize the email which is sent automatically from WooCommerce to the admin once a product becomes out of stock. I checked the WooCommerce related setting and it's not in the email tap.

Is there a way to customize the title, body and from?


Solution

  • You can customize WooCommerce out of stock notification email as follows:

    1). Change or add recipient - woocommerce_email_recipient_no_stock filter hook:

    Code example: Change stock email notifications recipient in WooCommerce


    2). Change email subject - woocommerce_email_subject_no_stock filter hook:

    The original subject code is (located on WC_Emails Class no_stock() method):

    $subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product out of stock', 'woocommerce' ) );
    

    Code examples: Customizing email subject with dynamic data in Woocommerce

    You will need to replace $order by $product and to use WC_Product methods instead to avoid errors.


    3). Change email content - woocommerce_email_content_no_stock filter hook:

    The original content code is (located on WC_Emails Class no_stock() method):

    $message = sprintf( 
        __( '%s is out of stock.', 'woocommerce' ), 
        html_entity_decode( wp_strip_all_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) 
    );
    

    So you can change it using something like:

    add_filter( 'woocommerce_email_content_no_stock', 'custom_email_content_no_stock', 20, 2 );
    function custom_email_content_no_stock( $content, $product ){
        
        return sprintf( 
            __( 'The product "%s" is actually out of stock.', 'woocommerce' ), 
            html_entity_decode( wp_strip_all_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) 
        );
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.


    4). Customizing From:

    See: Change sender name and email address for specific WooCommerce email notifications

    It's not possible to target no stock email notification.


    All available filter hooks are located on WC_Emails Class no_stock() method

    Note: Never use $this variable, replace it by $emails adding in your code at the beginning:

    $emails = WC()->mailer; 
    

    or

    $emails = new WC_Emails();