如何在 WooCommerce 订单项中获取 ACF 产品字段值

How to get ACF product fields values in WooCommerce Order Items

我正在修改我的商店,以便在下订单后在 WooCommerce 订单页面上显示自定义字段。

我的代码插入默认/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">'.__('&nbsp;unit&nbsp;', 'mbb').'</span>';
    } elseif ($per_unit_or_square == 'm2') {
        $pricingtype_order_received = '<span class="pricingtype_order_received">'.__('&nbsp;m<sup>2</sup>&nbsp;', 'mbb').'</span>';
    } else {
        $pricingtype_order_received = '<span class="pricingtype_order_received">'.__('&nbsp;pcs&nbsp;', '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() );

相关: