php - 在 Woocommerce 中访问和显示订单商品元数据
问题描述
在 Woocommerce 中,我试图显示订单项目对象的结果并访问它:
$product_meta = $item->get_meta_data();
print_r ($product_meta);
这就是我想要拉的:
编辑:这是我使用的输出$item->get_formatted_meta_data( '', true )
:
解决方案
要获取所有订单商品元数据,您将使用带有特定参数的WC_Order_Item
get_formatted_meta_data()
方法,这样:
// Accessible non protected Order item meta data
$item_meta_data = $item->get_formatted_meta_data( '', true );
// Formatted raw Output
echo '<pre>'; print_r($item_meta_data); echo '</pre>';
要访问某些订单项目属性,您可以使用任何WC_Order_Item_Product
方法,例如:
$item->get_product(); // Get the WC_Product object
$item->get_product_id(); // Get the Product ID
$item->get_variation_id(); // Get the Variation ID
$item->get_name(); // Get the Product name
$item->get_quantity(); // Get the item quantity
// and so on …
然后,如果您需要访问特定的“自定义”订单项目数据值,您将使用WC_Data
get_meta()
方法:
$custom_value = $item->get_meta("_custom_key");
请参阅:在 Woocommerce 3 中获取订单商品和 WC_Order_Item_Product
更新 (显示您所需的自定义订单商品元数据)
您可以通过以下方式访问和显示您需要的数据:
if( $lessons = $item->get_meta('lessons') ) {
echo '<p>Lessons: '.$lessons.'</p>';
}
if( $tour_guide = $item->get_meta('tour guide') ) {
echo '<p>Tour Guide: '.$tour_guide.'</p>';
}
我希望这现在有效。
推荐阅读
- regex - Varnish 4.0 多重匹配
- python - python客户端-服务器中的多个客户端文件传输,无需线程
- mysql - 从 Mysql 迁移到 Cassandra Spring boot
- html - HTML如何在线对齐列?
- cadence - cadence 是否支持 Azure Cosmos DB Cassandra API 而不是 cassandra
- python - Pandas Dataframe 不包含来自 PostgreSQL 查询的列
- django - 嵌入字段:必须是模型的实例:
- spring-boot - 如何处理请求 url-SpringBoot 中的无效/额外特殊字符 & =?
- angular - 将 FormGroup 作为 @Input 传递的 Angular 单元测试
- android - 在 SDK 管理器中找不到 Android 11