• Resolved studiokb

    (@studiokb)


    Hi,
    I have a number of custom fields and I have them outputting to my packing slips using the hook add_action( ‘wpo_wcpdf_after_order_data’, ‘wpo_wcpdf_who_for’, 10, 2 ); , which is great. But I have another plugin, Local Pickup Plus from WooCommerce themselves (Skyverge), which allows the user select the Shop from which to pickup an order. However I cannot get this to work as it turns out it is not stored in the same place as normal custom checkout fields. Looking at the SQL table and searching for a custom field e.g “_order_wcj_checkout_field_3” (created by booster plugin) you find that the custom fields (meta_key and meta_value) for the order are stored in the table “wp_postmeta” along with post ID.

    The developers of that plugin told me: “The data for Local Pickup Plus is stored as Order Item meta. This is stored in a separate table to normal post meta in the database, on a default install this would be located at wp_woocommerce_order_itemmeta. Within this table you can then extract the data using the _pickup_location_name key if you know the order_item_id that matches your order.”.

    So my question is, could your plugin hook (as previously mentioned) be used to query this table and get the value of the _pickup_location_name key?
    Thanks

    • This topic was modified 5 years, 6 months ago by studiokb. Reason: formatting
    • This topic was modified 5 years, 6 months ago by studiokb. Reason: formatting

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor kluver

    (@kluver)

    Hi @studiokb,

    I’m not familiar with the Local Pickup Plus plugin, but based on what their support has sent you the following code snippet should display the pickup location underneath each item in the order:

    add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_pickup_location_name', 10, 3 );
    function wpo_wcpdf_show_pickup_location_name ( $template_type, $item, $order ) {
    	if ( $template_type == 'packing-slip' ) {
    		$pickup_location = wc_get_order_item_meta( $item['item_id'], '_pickup_location_name', true );
    		if ( !empty( $pickup_location ) ) {
    			printf( '<span class="pickup-location">Pickup location: %s</span>', $pickup_location );
    		}
    	}
    }

    This snippet should be placed in the functions.php of your child theme. If you haven’t worked with code snippets or functions.php before please read this: How to use filters

    • This reply was modified 5 years, 6 months ago by kluver.
    Thread Starter studiokb

    (@studiokb)

    Hi,
    Thanks for the response. Your code did not work but I know it’s difficult when you can’t see the database.
    I thought I would have to do some custom SQL Queries, but as it turns out I figured a way to do it using WooCommerce’s get_shipping_methods() function. The location has obviously got a relationship to the shipping method. I would have thought the WooCommerce/Local Pickup Plus devs would have known that. Anyway the code that is working for me is as follows:

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_who_for', 10, 2 );
    function wpo_wcpdf_who_for ($template_type, $order) {
        if ( ($template_type == 'packing-slip') || ($template_type == 'invoice') ) {
            $document = wcpdf_get_document( $template_type, $order );
            ?>
            <tr class="who-for">
                <th>Pickup date selected:</th>
                <td><?php $document->custom_field('_order_wcj_checkout_field_3'); ?></td>
            </tr>
            <tr class="id">
                <th>Order ID:</th>
                <td><?php 
                    $kb_order_id = $order->get_id();
                    echo $kb_order_id;
                    ?>
               </td>
            </tr>
            <tr class="location">
                <th>Pickup Location:</th>
                <td><?php
                    $shipping_method = @array_shift($order->get_shipping_methods());
                    $shipping_method_id = $shipping_method['_pickup_location_name']; 
                    echo $shipping_method_id; 
                    //print_r ($shipping_method_id);
                    ?>
               </td>
            </tr>
            <?php
        }
    }

    I actually don’t need the Order ID, but it was useful when trying to figure this out.
    Thanks

    Plugin Contributor Ewout

    (@pomegranate)

    Glad to hear you found a solution! It’s strange that the Local Pickup Plus devs refer to order item meta (which you can call with wc_get_order_item_meta( $item_id, $meta_key, true ) as in kluver’s example), but apparently that’s not at all required. But perhaps the $item_id that they were referring to in this case is not of the order item (product) but the shipping item (which is also a line item).
    Just for completeness, if you would want to query item meta from shipping items you could use:

    
    $shipping_lines = $order->get_items('shipping');
    foreach ($shipping_lines as $item_id => $shipping_item) {
        $meta_key = '_pickup_location_name';
        $name = wc_get_order_item_meta( $item_id, $meta_key, true );
    }
    

    Hope that’s helpful for anyone ??

    Happy selling!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Custom field from wp_woocommerce_order_itemmeta table’ is closed to new replies.