Im trying to echo WooCommerce thumbail of producs onto an orderform.
Not sure how though though. This is what I've got so far:
<?php echo woocommerce_get_product_thumbnail();?>
This give me the WooCommerce thumbnail placeholder. How do I pull the right one for each item in my orderform? The orderform has fields for order id, creationdate, status and price, so it does pull the proper id´s somewhere.
This is examples of the meta keys and other fields that pull info for each order on the same form, if any of this makes any sense.
<a href="<?php echo $order->get_view_order_url(); ?>">
<?php echo $order->get_order_number(); ?>
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order();
$order->populate( $customer_order );
$item_count = $order->get_item_count();
$customer_orders = get_posts(
apply_filters( 'woocommerce_my_account_my_orders_query',
array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types('view-orders'),
'post_status' => array_keys(wc_get_order_statuses())
)
)
);
This is how the php is set up. I added the first table class for the immage.
<tr>
<th class="order-number"><span class="nobr"><?php _e( 'Image', 'woocommerce' ); ?></span></th>
<th class="order-number"><span class="nobr"><?php _e( 'Order', 'woocommerce' ); ?></span></th>
<th class="order-date"><span class="nobr"><?php _e( 'Date', 'woocommerce' ); ?></span></th>
</tr>
Then Im trying to add the image.
<td class="order-image”>
<?php echo woocommerce_get_product_thumbnail(); ?>
</td>
<td class="order-number">
<a href="<?php echo $order->get_view_order_url(); ?>">
<?php echo $order->get_order_number(); ?>
</a>
</td>
<td class="order-date”>
<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
</td>
Keep in mind that WooCommerce already has all the items/costs/thumbnails for previous orders in the My Accounts section and customers can "order again" directly from their My Account area.
But, assuming you have the order ID you can get to the product thumbnails with the following. This will output a small list of items with their thumbnails and product name:
$order = wc_get_order( $order_id );
$items = $order->get_items();
if( $items ) {
echo '<ul class="ordered-items">';
foreach( $items as $item ){
$id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
$product = wc_get_product( $id );
echo '<li>'. $product->get_image() . $product->get_title() . '</li>';
}
echo '</ul>';
}
Edit: To integrate into your code I would probably do the following
function so_28179558_get_order_thumbnail( $order ){
if( is_numeric( $order ) ){
$order = wc_get_order( $order_id );
}
if( is_wp_error( $order ) ){
return;
}
$order_thumb = '';
$items = $order->get_items();
if( $items ) {
foreach( $items as $item ){
$id = isset( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];
$product = wc_get_product( $id );
$order_thumb = $product->get_image();
continue;
}
}
return $order_thumb;
}
Then you can use it in your template like so:
<td class="order-image”>
<?php echo so_28179558_get_order_thumbnail( $order ); ?>
</td>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With