1
votes

I have customized the backorder text on my woocommerce site and would also like to add the same text to the order confirmation emails.

For example, I have my cart and checkout that display "In Stock" or "Made To Order" under the (variation) meta for each product.

To get the result, I added the following code to the cart.php template file and I added similar code the the review-order.php template file to get it to show on the checkout page:

// Backorder notification.
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="cart_stock_notification made-to-order">' . esc_html__( 'Made-To-Order', 'woocommerce' ) . '</p>', $product_id ) );
}
else if ( !$_product->backorders_require_notification() && !$_product->is_on_backorder( $cart_item['quantity'] ) ) {
    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="cart_stock_notification made-to-order">' . esc_html__( 'Made-To-Order', 'woocommerce' ) . '</p>', $product_id ) );
}
else {
    echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="cart_stock_notification in-stock">' . esc_html__( 'In Stock', 'woocommerce' ) . '</p>', $product_id ) );
}

Now I want the same thing to show in my order confirmation emails. I especially want it on the customer emails, but admin emails are fine too.

Right now any back ordered product is coming across in the product meta as "Backordered: #" to denote the number of back ordered products.

I would like to remove that backordered meta and replace it with the same logic used on the cart and checkout pages, so that it will display the same stock status on the emails.

I know that the backordered meta is being applied from the set_backorder_meta function in the class-wc-order-item-product.php, but I dont want to edit the class file as it will be overwritten with updates.

I also don't want to just change the text, I need the logic changed. If I can disable set_backorder_meta from applying to the emails, I think I can hook into woocommerce_order_item_meta_end or just edit the email-order-items.php template file (either is fine with me).

My problem is that I cannot seem to check if the item "backorders_require_notification" and/or "is_on_backorder".

Is there a way to disable the default backorder meta on the email confirmation and add custom meta that checks if a product is on backorder (and requires backorder notification), then display a custom text?

1

1 Answers

1
votes

In email-order-items.php template on line 60 - 65. The wc_display_item_meta() function is used.

If we look further, we will see that this function is in wc-template-functions.php

https://github.com/woocommerce/woocommerce/blob/master/includes/wc-template-functions.php#L3202-L3244

So we can overwrite the output via woocommerce_display_item_meta hook and through the parameters we can obtain necessary info about the product.

function filter_woocommerce_display_item_meta ( $html, $item, $args ) { 
    // Get product
    $product = $item->get_product();

    // Html
    if ( $product->backorders_require_notification() && $product->is_on_backorder( $item['quantity'] ) ) {
        $html = '<ul class="wc-item-meta"><li><strong class="wc-item-meta-label" style="float: left; margin-right: .25em; clear: both">Made-To-Order</strong></li></ul>';       
    } else if ( !$product->backorders_require_notification() && !$product->is_on_backorder( $item['quantity'] ) ) {
        $html = '<ul class="wc-item-meta"><li><strong class="wc-item-meta-label" style="float: left; margin-right: .25em; clear: both">Made-To-Order</strong></li></ul>';
    } else {
        $html = '<ul class="wc-item-meta"><li><strong class="wc-item-meta-label" style="float: left; margin-right: .25em; clear: both">In stock</strong></li></ul>';
    }

    return $html;
}
add_filter( 'woocommerce_display_item_meta', 'filter_woocommerce_display_item_meta', 10, 3 );

Hint: to keep the output more dynamic you can rewrite the output via the args (you can see how this is done in wc-template-functions.php)