php - 如何在 WooCommerce 订单项中获取 ACF 产品字段值
问题描述
下订单后,我正在修改我的商店以在 WooCommerce 订单页面上显示自定义字段。
我的代码被插入到 default/themes/mytheme/woocommerce/order/order-details-item.php
中。这是读取字段的部分:
$per_unit_or_square = get_field('pricem2_or_priceunit_', $product->get_id());
if (!empty($per_unit_or_square)) {
if ($per_unit_or_square == 'unit') {
$pricingtype_order_received = '<span class="pricingtype_order_received">'.__(' unit ', 'mbb').'</span>';
} elseif ($per_unit_or_square == 'm2') {
$pricingtype_order_received = '<span class="pricingtype_order_received">'.__(' m<sup>2</sup> ', 'mbb').'</span>';
} else {
$pricingtype_order_received = '<span class="pricingtype_order_received">'.__(' pcs ', 'mbb').'</span>';
}
}
正如您在上面看到的,我正在尝试设置一个变量$pricingtype_order_received
并将其放在数量数字之后,以显示客户是否购买了单位 m2 或 pcs:
if ( $refunded_qty ) {
$qty_display = '<del>' . esc_html( $qty ) . '</del> <ins>' . esc_html( $qty - ( $refunded_qty * -1 ) ) . '</ins>'.$pricingtype_order_received;
} else {
$qty_display = esc_html( $qty ).$pricingtype_order_received;
}
我的问题是代码不起作用并且没有检索$pricingtype_order_received
值。相同的逻辑适用于购物车和其他页面,但不适用于订单,以及订单完成后的电子邮件。
在哪里寻找?ACF 字段是否可以在订单和电子邮件中读取?
如果有人感兴趣,这是整个代码:https ://pastebin.com/PCDs32LC
解决方案
尝试替换$product->get_id()
为$item->get_product_id()
:
$per_unit_or_square = get_field( 'pricem2_or_priceunit_', $item->get_product_id() );
推荐阅读
- node.js - NestJS 导出。连接,postgres 多租户
- javascript - 角型错误
- json - 用 jq 合并两个 JSON 对象
- visual-studio-2017 - 用户在 vs setup 项目中卸载应用程序时如何删除应用程序生成的配置文件
- django-rest-framework - DRF - 具有多个序列化器或重复序列化器的 API
- javascript - 基于出生日期的数据表搜索年龄
- javascript - 如何提高键盘导航性能
- machine-learning - Adaptive_avg_pool2d 的计算 FLOPs
- javascript - 如何在此 HTML 页面中存储数据并使用 JavaScript 在另一个页面中检索它
- javascript - 如何处理DMN业务规则任务的O/P - 带有多个对象数组的resultList