Support » Plugin: PDF Invoices & Packing Slips for WooCommerce » Comment field in Packing Slip
Comment field in Packing Slip
-
I would love to be able to add a variable text field to the Packing Slip so I can comment on the product(s) ordered or to personalize the thank you to the customer. Is there a straightforward way to do this? There is always plenty of white space under the Product / Quantity details.
I have Advanced Custom Fields, if that helps.
-
Hi @ozviewer,
Please read my replies below to learn more:
Yordan, thanks so much for your prompt and comprehensive reply. As soon as I have digested the information I will try it out. It looks very promising.
Sure! Take your time and let me know if you need anything else 😉
Yordan, I made some progress by adding the custom field using ACF and the field appears on the admin order page but entering text does not appear on the packing slip. I had already added the code snippet:
add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_product_location', 10, 3 ); function wpo_wcpdf_show_product_location ( $template_type, $item, $order ) { if (isset($item['product']) && !empty($location = $item['product']->get_meta('location'))) { echo '<div class="product-location">Location: '.$location.'</div>'; } }
I couldn’t find any reference to ‘location’ as a choice in ACF so I think the snippet I added had no effect. Sorry, I am out of my depth.
Hi @ozviewer,
Sorry for the late reply!
Yordan, I made some progress by adding the custom field using ACF and the field appears on the admin order page but entering text does not appear on the packing slip.
This is because your code snippet is trying to add a product metadata, not an order metadata. There are three common data sources in WooCommerce related with the orders:
- Order metadata: Here is stored the billing and shipping info, order number, order date, etc.
- Item metadata: This data includes the products, shipping and fees of the order.
- Product metadata: This is the product data stored in your inventory that is used by WooCommerce at the moment of the order placement, like price, weight, quantity, etc. You can set your own custom fields here that can be fetched manually later, like product location, HS Code, etc.
You said that you added a custom field in your order, but if you want to add info related to your products, you have to add a product custom field instead, if the data you want to show is per item, and not general data from the order. Then you can use a code snippet like the one above to display that field.
To switch the location of the data, go to Advanced Custom Fields, edit your field group, find the Location panel and switch rule to Post type » is equal to » Product
I couldn’t find any reference to ‘location’ as a choice in ACF so I think the snippet I added had no effect
You’ll find the custom field name within your field group settings, in the Name column.
Yordan, thanks for your reply. I actually want the custom field to apply to each order and for it to appear on the Packing Slip. My custom field group and post type can be seen here
I also tried this code snippet and it also didn’t work:
add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_product_location', 10, 3 ); function wpo_wcpdf_show_product_location ( $template_type, $item, $order ) { if (isset($item['product']) && !empty($location = $item['product']->get_meta('location'))) { echo '<div class="product-location">Location: '.$location.'</div>'; } }
I noticed on doing an FTP inspection of my files I have the standard PDF Invoices installed template and a custom template. How do I know which one is being used?
I really appreciate your help.
As I said above, the reason why you aren’t seeing the field is that you have set up an order custom field, but your code snippet is to display product custom fields. The order custom fields are intended to be used outside the item table, while the product custom fields are intended to be used in a per-product basis, often before/after the item meta.
That said, let’s say that you want to display the value from the
ps_comments
after the customer notes, then you can achieve it with this code snippet:/** * WooCommerce PDF Invoices & Packing Slips: * Display the PS Comments after the customer notes on the packing slip */ add_action( 'wpo_wcpdf_after_customer_notes', function( $document_type, $order ){ if( $document_type == 'packing-slip' && ( $ps_comments = $order->get_meta( 'ps_comments' ) ) ) { echo "<div><h3>PS Comments:</h3><p>{$ps_comments}</p></div>"; } }, 10, 2);
If you haven’t worked with code snippets (actions/filters) or
functions.php
before, read this guide: How to use filtersYordan, thank you so much for your patience and help. Comparing the two snippets I now understand (I think) but the main thing is your suggested snippet works!
Outstanding support.
- The topic ‘Comment field in Packing Slip’ is closed to new replies.