• Resolved koupmi

    (@koupmi)


    Hello, i have dynamical fields based on number of virtual products in cart. I would like to display them also in Invoice after order. Tried this code:

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_my_custom_field', 10, 2 );
    function wpo_wcpdf_my_custom_field ($template_type, $order) {
    function order_has_virtual_product($order_id) {
        $order = wc_get_order($order_id);
        $items = $order->get_items();
        $virtual_product_count = 0;
    
        foreach ($items as $item) {
            $product = $item->get_product();
            if ($product->is_virtual()) {
                $virtual_product_count++;
            }
        }
    
        return $virtual_product_count;
    }	
        $order_id = $order->get_id(); // Get the order ID from the $order object
        $virtual_product_count = order_has_virtual_product( $order_id );
    
        for ( $i = 1; $i <= $virtual_product_count; $i++ ) {	
            if ($template_type == 'invoice') {
                ?>
                <tr class="order_jmeno_ucastnika">
                <th>ú?astníci kurzu:</th>
    			<?php for ( $i = 1; $i <= $virtual_product_count; $i++ ) { ?>
                <td><?php echo $order->get_meta('order_jmeno_ucastnika' . $i); ?></td> <?php } ?>
                </tr>
                <?php
        }
    }}

    When user order 2 virtual products or more there are generated fields with name order_jmeno_ucastnika1 order_jmeno_ucastnika2 .. etc

    But in invoice is only first field “order_jmeno_ucastnika1” and others are not displayed.

    • This topic was modified 1 year, 6 months ago by koupmi.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor alexmigf

    (@alexmigf)

    Hello @koupmi

    Are you sure that the meta keys order_jmeno_ucastnika1 are in the order meta and not in the order item meta?

    Also, you could place the function order_has_virtual_productoutside the hook function wpo_wcpdf_my_custom_field.

    Thread Starter koupmi

    (@koupmi)

    It should be order meta when its here:

    • This reply was modified 1 year, 6 months ago by koupmi.
    • This reply was modified 1 year, 6 months ago by koupmi.
    Plugin Contributor dwpriv

    (@dwpriv)

    @koupmi

    You can check if the meta key is saved as order meta or order item meta with a plugin like the?WooCommerce Store Toolkit?plugin. Give it a try and see.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to display loop of custom fields in invoice?’ is closed to new replies.