Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

wordpress - Show Availability Stock Status in Email Woocommerce

Is it possible to show the stock status as "In Stock" "Pre-Order" in new order mails?

These codes help me show the SKU in Mail, but I could not create the stock status by changing it.

/**
 * Adds SKUs and product images to WooCommerce order emails
 */
function sww_add_sku_to_wc_emails( $args ) {
  
    $args['show_sku'] = true;
    return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'sww_add_sku_to_wc_emails' );
question from:https://stackoverflow.com/questions/65851482/show-availability-stock-status-in-email-woocommerce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can show the stock status of the products via the woocommerce_display_item_meta hook, for more information: https://woocommerce.github.io/code-reference/files/woocommerce-includes-wc-template-functions.html#source-view.3290

Here is the code for your case:

add_filter( 'woocommerce_display_item_meta', 'show_stock_status_in_email', 10, 3 );
function show_stock_status_in_email( $html, $item, $args ) {
    // gets the product object
    $product = $item->get_product();
    // gets stock status product
    $stock_status = $product->get_stock_status();
    // show it after the product name
    $html .= '<p style="margin:0;"><strong>(' . $stock_status . ')</strong></p>';
    return $html;
}

The result will be the following:

enter image description here

The code has been tested and works. Add it in your theme's functions.php file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...