我已经制作了自定义的order-list.php模板,但是我想在这里使用产品图片。每个订单始终只有一个产品,因此始终只有一个缩略图。我如何获得这张照片的网址?
如果需要,请输入我的代码:
<?php foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
?>
<div class="product_list">
<div class="product_item">
<div class="product_home">
<div class="text-center">
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>" title="<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>">
//i want to place image here
</a>
</div>
<div class="content_product_home">
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>" title="<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>"><h4> Zamówienie <?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?></h4></a>
<p><?php echo $order->get_formatted_order_total() ?><span class="per_month">including tax</span></p>
</div>
<div class="text-center">
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>" class="btn btn-more" title="Details">Details</a>
</div>
</div>
</div>
</div>
<?php } ?>
谢谢! :)
您可以使用wp_get_attachment_image_src()获取图像,只需先从$ order-> get_items()获取产品ID,如下所示:
foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order($customer_order); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
// first get product id
foreach($order->get_items() as $order_key => $order_value) {
$product_id = $order_value->get_data()['product_id'];
}
// get image
if (isset($product_id)) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id($product_id));
}
?>
<div class="product_list">
<div class="product_item">
<div class="product_home">
<div class="text-center">
<a href="<?php echo esc_url($order->get_view_order_url()); ?>" title="<?php echo esc_html(_x('#', 'hash before order number', 'woocommerce') . $order->get_order_number()); ?>">
<?php if (isset($image)) { ?>
<img src="<?php echo $image[0] ?>" />
<?php } ?>
</a>
</div>
<div class="content_product_home">
<a href="<?php echo esc_url($order->get_view_order_url()); ?>" title="<?php echo esc_html(_x('#', 'hash before order number', 'woocommerce') . $order->get_order_number()); ?>">
<h4> Zamówienie
<?php echo esc_html(_x('#', 'hash before order number', 'woocommerce') . $order->get_order_number()); ?>
</h4>
</a>
<p>
<?php echo $order->get_formatted_order_total() ?><span class="per_month">including tax</span></p>
</div>
<div class="text-center">
<a href="<?php echo esc_url($order->get_view_order_url()); ?>" class="btn btn-more" title="Details">Details</a>
</div>
</div>
</div>
</div>
<?php } ?>