• Resolved starfiredev

    (@starfiredev)


    So I have a client who’s using woocommerce to send them orders for glasses prescriptions. There’s only 1 product but it has custom meta each time. I have it working on everything else on the site but not on the PDF. When I try to use any of the methods outlined in your documentation they either show up blank or throw an error. Here’s my custom function:

    
    add_filter( 'wpo_wcpdf_before_item_meta', 'wpo_wcpdf_add_meta', 10, 3 );
    function wpo_wcpdf_add_meta ($template_type, $item, $order) {
        // check if product exists first
        if (empty($item['product'])) return;
        
        $document = wcpdf_get_document( $template_type, $order, $item );
        if ($template_type == 'invoice') {
            
            
            
            ?>      
    
            <h3>Frame Info</h3>
            <table class="order-info" style="width:100%;">
            
                <?php
                if (!empty($item['product'])) {
                    $field_name = '_frame_name';
                    $frameName = method_exists($item['product'], 'get_meta') ? $item['product']->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true );
                    if ( !empty($frameName) ) {
                        echo '<div class="product-location">Frame Name: '.$frameName.'</div>';
                    }
                }
                ?>
            
            </table>
                
            <?php
    
            }
        }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    The reason this throws an error is probably because your HTML formatting is off, you’re creating a table without cells but with a stray div in it:

    
    <table class="order-info" style="width:100%;">
        <div class="product-location">Frame Name: xxxxxxxxx</div>
    </table>
    

    This will probably work if you remove the table element from your code (or make it a div element, or put your .product-location in a <tr><td>...</td></tr>.

    Thread Starter starfiredev

    (@starfiredev)

    My apoligies, I was more concerned about getting the data to work first and then make it look nice.

    I’ve cleaned up the code a bit. It doesn’t throw any errors but it does list Manufacturer as blank on the PDF:

    
    add_action( 'wpo_wcpdf_before_item_meta', 'wpo_wcpdf_add_meta', 10, 3 );
    function wpo_wcpdf_add_meta ($template_type, $item, $order) {
        // check if product exists first
        if (empty($item['product'])) return;
        
        $document = wcpdf_get_document( $template_type, $order, $item );
        //print_r($order);
        if ($template_type == 'invoice') {        
            $order_id = $order->get_meta('id');
            print_r($order_id);
            ?>      
            <h3>Frame Info</h3>
            <ul>
                <?php
                    $frameManufacturer = $order->get_meta('_frame_manufacturer');
                    ?>
                    <li>Manufacturer: <?php echo($frameManufacturer)?></li>';
                ?>
            </ul>
            <?php
            }
        }
    

    I should also mention the data I’m trying to retrieve is in the item line’s meta data. When I printed the entire order array I did see my variables so there must be a way to display them.

    • This reply was modified 4 years, 7 months ago by starfiredev.
    Plugin Contributor Ewout

    (@pomegranate)

    You have changed the function – the previous snippet pulled product meta:

    
    $item['product']->get_meta($field_name);
    

    and the new one pulls order meta:

    
    $order->get_meta('_frame_manufacturer');
    

    but you mention you need line item meta… that would be

    
    wc_get_order_item_meta( $item['item_id'], '_frame_manufacturer );
    
    Thread Starter starfiredev

    (@starfiredev)

    That did it! Thank you so much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Cannot Display Custom Meta Data on Items’ is closed to new replies.