在“订单"页面中显示产品图片 - Woocommerce
我第一次与 WC 合作,我正在用这个追我的尾巴.
I am working with WC for the first time and I am chasing my tail with this one.
在订单"页面中,客户可以看到他们所做的所有购买,该数组显示了有关订单的一些基本信息.我还需要展示客户购买的产品的图片.
In "Orders" page where customer's can see all the purchases they've made the array shows some basic info about the order. I need to also show the image of the product the customer bought.
订单似乎是自定义帖子,但我如何获得图片产品?
Orders seems to be custom posts, but how do I get the image product?
我意识到这个问题太含糊了.任何指针都会有很大帮助.
I realize the question is too vague. Any pointers would be a big great help.
订单是 shop_order
类型的自定义帖子类型.订单本身没有缩略图,但订单有购买的产品列表,每个产品都有缩略图的可能性.
Orders are custom post types of shop_order
type. Orders do not have thumbnails themselves, but orders have a list of products that were purchased and each product has the possibility of a thumbnail.
您可以在order-details.php
模板如何获取与任何订单对象关联的所有项目/产品... $order->get_items()
这将返回存储在单独数据库表中的数据数组.使用 $item
变量,您可以获得原始产品,并且您可以在链接模板中看到正在发送到 order-details 的
定义为 $product
变量-item.phporder->get_product_from_item( $item )
.
This returns an array of data that is stored in separate database tables. With the $item
variable you can get the original product and you can see in the linked template that the $product
variable that is being sent to the order-details-item.php
is defined as order->get_product_from_item( $item )
.
无论如何,一旦您拥有 $product
对象,您就可以使用 $product->get_image()
来检索产品的图像.
Anyway, once you have the $product
object you can use $product->get_image()
to retrieve the product's image.
作为一个简化示例,这将显示订单 999 中购买的所有产品的缩略图.
As a simplified example, this would show the thumbnails for all the products purchased in order 999.
$order_id = 999;
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item_id => $item ) {
$product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
echo $product->get_image();
}
将其嵌套在循环中:
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order();
$order->populate( $customer_order );
foreach( $order->get_items() as $item_id => $item ) {
$product' = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
echo $product->get_image();
}
}
虽然通常情况下,order-details.php
模板应该包含指向每个订单概览的链接.
Though normally, the order-details.php
template should have links to an overview of each individual order.