Home > OS >  How to display ACF field after the product name on the cart and order reviews in WooCommerce?
How to display ACF field after the product name on the cart and order reviews in WooCommerce?

Time:01-05

I have Advanced Custom Fields setup for post type on WooCommerce Products. So each product has 1 unique custom field.

I'm trying to display the custom field after the product name on the cart and the checkout page and order table information.

However, running into problems as my code doesn't display any output.

Any advice on how to achieve this would be much appreciated. Thanks

// Display the ACF custom field 'location' after the product title on cart / checkout page.
function cart_item_custom_feild( $cart_item ) {
    $address = get_field( 'location', $cart_item['product_id'] );
    echo "<div>Address: $address.</div>";
}
add_action( 'woocommerce_after_cart_item_name', 'cart_item_custom_feild', 10, 1 );

I also tried the_field instead of get_field

CodePudding user response:

If you would need to run it both on the cart page AND order review table on the checkout page, you could use woocommerce_cart_item_name filter hook, like this:

add_filter('woocommerce_cart_item_name', 'order_review_custom_field', 999, 3);

function order_review_custom_field($product_name, $cart_item, $cart_item_key)
{
    $address = get_field('location', $cart_item['product_id']);

    return $product_name . '<div>Address: ' . $address . '</div>';
}

Here's the result on the cart page:

enter image description here

And on the order review table on the check out page:

enter image description here

  •  Tags:  
  • Related