phpwordpresswoocommerceemail-notificationsproduct-variations

Customize WooCommerce Low Stock Email Content Based on Product Variation custom field


I have customized my WooCommerce store to assign priority levels (1-4) to product variants, which helps us manage our inventory more effectively. These priority levels are saved as post meta for each product variant.

The challenge I'm facing now is with the automated low stock email notification. I would like to include the product variant's priority level in these emails to make the restocking process more efficient.

How can I retrieve the priority level meta for each variant and inject it into the WooCommerce low stock email content? Ideally, I'd like the email to contain a list of low stock variants along with their respective priority levels.

Here is my PHP code that assigns priority levels to product variants:

function add_priority_field_to_variants($loop, $variation_data, $variation) {
    ?>
    <div class="form-row form-row-full">
        <label for="prio_production_<?php echo $loop; ?>"><?php _e('Prio Produktion', 'woocommerce'); ?> </label>
        <select id="prio_production_<?php echo $loop; ?>" name="prio_production[<?php echo $loop; ?>]">
            <option value="1" <?php selected(get_post_meta($variation->ID, '_prio_production', true), '1'); ?>>1</option>
            <option value="2" <?php selected(get_post_meta($variation->ID, '_prio_production', true), '2'); ?>>2</option>
            <option value="3" <?php selected(get_post_meta($variation->ID, '_prio_production', true), '3'); ?>>3</option>
            <option value="4" <?php selected(get_post_meta($variation->ID, '_prio_production', true), '4'); ?>>4</option>
        </select>
    </div>
    <?php
}
add_action('woocommerce_product_after_variable_attributes', 'add_priority_field_to_variants', 10, 3);


function save_priority_field_variants($variation_id, $i) {
    $prio_production = $_POST['prio_production'][$i];
    if (isset($prio_production)) {
        update_post_meta($variation_id, '_prio_production', sanitize_text_field($prio_production));
    }
}
add_action('woocommerce_save_product_variation', 'save_priority_field_variants', 10, 2);




function display_priority_on_product_page() {
    global $product;
    if ($product->is_type('variable')) {
        foreach ($product->get_available_variations() as $variation) {
            $prio_value = get_post_meta($variation['variation_id'], '_prio_production', true);
            echo '<div class="product-priority">Prio Produktion för denna variant: ' . esc_html($prio_value) . '</div>';
        }
    }
}
add_action('woocommerce_single_product_summary', 'display_priority_on_product_page', 25);

I tried to add this code, but I had no effect on the low stock email.

// Funktion för att hämta och formatera prioritetsinformation för e-postmeddelanden
function get_priority_info_for_email($product) {
    $priority_info = '';

    if ($product->is_type('variable')) {
        $variations = $product->get_children();
        foreach ($variations as $variation_id) {
            $variation_obj = wc_get_product($variation_id);
            $priority = get_post_meta($variation_id, '_prio_production', true);
            $variation_description = wc_get_formatted_variation($variation_obj, true);
            $priority_info .= "{$variation_description} - Prioritet: {$priority}\n";
        }
    } else {
        $priority = get_post_meta($product->get_id(), '_prio_production', true);
        $priority_info = "Prioritet: {$priority}";
    }

    return $priority_info;
}

// Anpassar e-postmeddelandet för lågt lagersaldo för att inkludera prioritetsinformation
function customize_low_stock_email_notification($email_content, $email) {
    if ('low_stock' === $email->id) {
        $product = $email->object;
        $priority_info = get_priority_info_for_email($product);
        $email_content .= "\n\nProduktionsprioritet:\n" . $priority_info;
    }
    return $email_content;
}
add_filter('woocommerce_mail_content', 'customize_low_stock_email_notification', 20, 2);


Solution

  • You are not using the right hook, and you are making things much more complicated.

    Try to replace your 2 last functions simply with the following (assuming that stock management is enabled at the product variations level):

    add_filter( 'woocommerce_email_content_low_stock', 'filter_email_content_low_stock', 10, 2 );
    function filter_email_content_low_stock( $message, $product ) {
        if ( $priority = $product->get_meta('_prio_production') ) {
            $message .= sprintf('<br><div class="product-priority">%s: %s</div>', 
                __('Prio Produktion för denna variant', 'woocommerce'), 
                esc_html($priority) 
            );
        }
        return $message;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.