I have this code, its function is to add a column in woocommerce order details email template, but when I send an invoice I get this error message saying:
Fatal error: Uncaught Error: Call to a member function get() on null in http:\mysite.com\functions.php on line 1245
when using this code:
add_action( 'woocommerce_order_item_meta_end', 'order_custom_field_in_item_meta_end', 10, 4 );
function order_custom_field_in_item_meta_end( $item_id, $item, $order, $cart_item) {
global $woocommerce;
do_action( 'woocommerce_review_order_before_cart_contents' );
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
echo '<td class="td">'.$_product->get_price_html().'</td>';
}
}
do_action( 'woocommerce_review_order_after_cart_contents' );
}
My problem here, is that when I send an invoice or any other email notification from the order it gets an error.
What I am doing wrong and how to solve this?
Thanks