• Resolved sabineblume

    (@sabineblume)


    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;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • @sabineblume

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.
    
    I can also recommend <a href="https://developer.woocommerce.com/" rel="noopener" target="_blank">the WooCommerce Developer Resources Portal</a> for resources on developing for WooCommerce.
    
    You can also visit the <a href="https://www.facebook.com/groups/advanced.woocommerce/" rel="noopener" target="_blank">WooCommerce Facebook group</a> or the <code>#developers</code> channel of the <a href="https://woocommerce.com/community-slack/" rel="noopener" target="_blank">WooCommerce Community Slack</a>. We're lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Seems we’ve not had additional inputs on this thread.

    We encourage you to make use of the above resources.

    That said, I’ll go ahead and mark the thread as resolved but please feel free to create a new thread if you have further questions.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show custom product fields in order and noifications’ is closed to new replies.