questions

How can get ACF product fields values in WooCommerce Order Items

Rate this post

I am modifying my shop to show custom fields on WooCommerce order page after the order has been made.

My code is inserted into default /themes/mytheme/woocommerce/order/order-details-item.php. This is the part that reads the field:

$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>';
    }
}

As you can see above, I am trying to set a variable $pricingtype_order_received and put it here just after the quantity numebers to show if the customer bought units, m2 or 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;
}

My issue is that the code does not work and not retrieving $pricingtype_order_received value. The same logic worked on cart and other pages, but not working on orders, and in email after the order have been made.

Where to look for? Does ACF fields can be read on order and email?

 

✔️Solution:

Try to replace $product->get_id() with $item->get_product_id() like:

$per_unit_or_square = get_field( 'pricem2_or_priceunit_', $item->get_product_id() );

Leave a Reply

Your email address will not be published.

Back to top button