• I am trying to display a custom product field in the order and notification emails. I found the following code which I modified and it shows the custom field (Units_cc) on the cart and checkout page but it doesn’t show the field on order and email notifications. I’m not sure how to modify the last part of the code so it does show up. Any help appreciated!

    add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
    function save_days_field( $cart_item_data, $product_id ) {
        $special_item = get_post_meta( $product_id , 'Units_cc',true );
    
        if(!empty($special_item)) {
            $cart_item_data[ 'Units_cc' ] = $special_item;
    
            // below statement make sure every add to cart action as unique line item
            $cart_item_data['unique_key'] = md5( microtime().rand() );
            WC()->session->set( 'Units_cc', $special_item );
        }
        return $cart_item_data;
    }
    
    // Render meta on cart and checkout
    add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
    function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
        if( isset( $cart_item['Units_cc'] ) ) {
            $cart_item_data[] = array( "name" => __( "Product units", "woocommerce" ), "value" => $cart_item['Units_cc'] );
        }
        return $cart_item_data;
    }
    
    // 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('Units_cc') ) {
            //$product_name .= '<small>'.$value.'</small>';
        }
        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('Units_cc') ) {
            //$product_name .= '<small>'.$value.'</small>';
        }
        return $product_name;
    }
    • This topic was modified 2 years, 11 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Show custom product fields in order and noifications’ is closed to new replies.