'Display Brand Name Above Product Title in Woocommerce Cart, Checkout Page, Orders and Email Notification

Using the functions below I've applied a custom field to products added in Woocommerce cart and checkout page also to orders and notification email.

My question is how to show the brand above product title in cart, checkout etc. Any help will be appreciate.

// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
    $product = $cart_item['data']; // Get the WC_Product Object


    if ( $value = $product->get_meta('my_custom_field') ) {
        $product_name .= '<span class="custom_field_class">'.$value.'</span>';
    }

    return $product_name;


}



// Display in orders and email notifications
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
    $product = $item->get_product(); // Get the WC_Product Object

    if ( $value = $product->get_meta('my_custom_field') ) {
        $product_name .= '<span class="custom_field_class">'.$value.'</span>';
    }
    return $product_name;
}


Solution 1:[1]

I have tried this solution to your query

I have used ACF for Entering brand name on product page in back-end, and used that field in functions to get the value of the brand name and display in front-end.

  • Use this code for displaying custom field value in single product page
 //Displaying custom field value in single product page
add_action( 'woocommerce_single_product_summary', '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( 'brand_name', $product_id );
    echo "</div>";

    return true;
}
  • Use this code for Storing this custom field into cart and session:
// Storing this custom field into cart and session:
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( 'brand_name', $product_id, true );

    if( !empty( $custom_field_value ) ) 
    {
        $cart_item_data['brand_name'] = $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;
}
  • Use this code for Render meta on cart and checkout:
//Render meta on cart and checkout
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['brand_name'] ) ) {
        $custom_items[] = array( "name" => "Brand Name", "value" => $cart_item['brand_name'] );
    }
    return $custom_items;
}
  • Use this code for Displaying Filed Value on Mail tamplate
//Display Filed Value on Mail tamplate
function add_order_item_meta_acf( $item_id, $values ) {

    wc_add_order_item_meta( $item_id, 'Brand Name', $values [ 'brand_name' ] );


    }
    add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta_acf' , 10, 2);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AddWeb Solution Pvt Ltd