I am trying to understand how to define product in this code after having made some changes to it based on the answer in my previous question Why does WooCommerce Order Shortcode Generate Plugin Notice on Custom Thank You Page?
The notice message have changed and now it asks me to define product. This is the notice:
Notice: Undefined variable: product in /wp-content/themes/storefront/functions.php on line 842
The notice refers to this line:
'purchase_note' => $product ? $product->get_purchase_note() : '',
This is the full code:
function order_cost_breakdown(){
if ( isset( $_GET['order_id']) && $_GET['order_id'] > 0 ) {
$order_id = (int) esc_attr( $_GET['order_id'] );
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
$show_purchase_note = $order->has_status( apply_filters( 'woocommerce_purchase_note_order_statuses', array( 'completed', 'processing' ) ) );
ob_start();
?>
<div class="woocommerce-account woocommerce-page"><div class="woocommerce">
<table class="shop_table order_details">
<thead>
<tr>
<th class="product-name"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th class="product-total"><?php _e( 'Total', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $order->get_items() as $item_id => $item ) {
wc_get_template( 'order/order-details-item.php', array (
'order' => $order,
'item_id' => $item_id,
'item' => $item,
'product' => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ),
'show_purchase_note' => $show_purchase_note,
'purchase_note' => $product ? $product->get_purchase_note() : '',
) );
}
?>
<?php do_action( 'woocommerce_order_items_table', $order ); ?>
</tbody>
<tfoot>
<?php
foreach ( $order->get_order_item_totals() as $key => $total ){
?>
<tr>
<th scope="row"><?php echo $total['label']; ?></th>
<td><?php echo $total['value']; ?></td>
</tr>
<?php
}
?>
</tfoot>
</table>
</div>
</div>
<?php
return ob_get_clean();
}
}
Any advice is helpful as I am clearly not an expert.
To fix the notice
Replace
<?php foreach ( $order->get_items() as $item_id => $item ) {
wc_get_template( 'order/order-details-item.php', array (
'order' => $order,
'item_id' => $item_id,
'item' => $item,
'product' => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ),
'show_purchase_note' => $show_purchase_note,
'purchase_note' => $product ? $product->get_purchase_note() : '',
) );
}
?>
With
<?php foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
wc_get_template( 'order/order-details-item.php', array (
'order' => $order,
'item_id' => $item_id,
'item' => $item,
'product' => apply_filters( 'woocommerce_order_item_product', $item->get_product( $item ), $item ),
'show_purchase_note' => $show_purchase_note,
'purchase_note' => $product ? $product->get_purchase_note() : '',
));
}
?>
That should suffice