1
votes

I created an advanced custom field with the ACF plugin to add additional information on the Woocommerce product edit page. It's a subtitle that differentiates t-shirt templates. I was able to make the value appear on the product page front-end, but I need this information to appear in the order details and all emails, perhaps as order meta. Thanks in advance.

2

2 Answers

0
votes

please try Below code in fucntions.php file..

 add_action( 'woocommerce_before_add_to_cart_button', 'add_custom_field', 0 );

function add_custom_field() {
    global $product;             // Changed this

    // Added this too (compatibility with WC +3) 
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    echo "<div class='produto-informacoes-complementares'>";
    echo get_field( 'woo_title', $product_id );
    echo "</div>";

    return true;
}


add_filter( 'woocommerce_add_cart_item_data', 'save_my_custom_product_field', 10, 2 );

function save_my_custom_product_field( $cart_item_data, $product_id ) {

    $custom_field_value = get_field( 'woo_title', $product_id, true );

    if( !empty( $custom_field_value ) ) 
    {
        $cart_item_data['woo_title'] = $custom_field_value;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );

function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
    $custom_items = array();
    // Woo 2.4.2 updates
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['woo_title'] ) ) {
        $custom_items[] = array( "name" => "ACF value:", "value" => $cart_item['woo_title'] );
    }
    return $custom_items;
}

function add_order_item_meta_acf( $item_id, $values ) {

    wc_add_order_item_meta( $item_id, 'acf', $values [ 'woo_title' ] );
  }
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);

Dispaly Acf value in order detail page after order table

add_action ('woocommerce_order_details_after_order_table', 'order_detail_page', 20);

  function order_detail_page($order_id) {

    //  global $post;
    // $order = wc_get_order( $post->ID ); 

     $order = new WC_Order( $order_id );
     $items = $order->get_items();
     foreach ( $items as $item ) {
          $product_id = $item['product_id']; 
         }

  if (get_field('woo_title',  $product_id)) {
   ?>
    <p class="WooCommerce">ACF value: <?php the_field('woo_title',  $product_id);    ?><p>      
    <?php 
     }
   }```
0
votes

This is brilliant and the only place I've ever found this answer! I might have to spread it around! :)

I'm getting this to work on a site I'm developing as well - the code has no bugs.

Just a few additional pointers on the code:

  • where is says "woo_title", that is where you put the name of your field

  • In the code where it says "ACF value:" and "acf", change those to titles that make sense to the data you are trying to display to the customer (like Date or Style)

  • In addition to the last point, there is no need to add ":" after your field's title, Woocommerce will insert a colon

  • When you add your test product to the cart and do a test checkout, if you are testing and changing the field title as I've been talking about above, you can't see your changes by simply refreshing the cart screen, you'll have to empty your cart, change the function code, change what you entered into the custom field in the ACF field to make the product data in the database update, then hit update on the product/post, then go through the process of adding it to the cart and checking out to see your update - hopefully that makes sense!