Add custom product field – Warehouse location
-
HI
I need to add a custom product field “Warehouse location” to the packing slip so I can find my way around all the shelfs. I have added a custom field to my products namned Warehouse location using this tutorial:
https://www.remicorson.com/mastering-woocommerce-products-custom-fields/
Now I am struggling with how to get warehouse location on the packing slip. I have found this bit of code but can’t get my head around how to modify it to my needs.
https://gist.github.com/ChromeOrange/4272784
Someone having easy a solution for this?
https://www.ads-software.com/plugins/woocommerce-pdf-invoices-packing-slips/
-
Hi Wackes,
The key to your answer is in this part of that tutorial (under “Saving Fields Values”):update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
Whatever meta_key you chose there (‘_text_field’ in this example), you can show anywhere in the packing slip with the following code (replace ‘_text_field’ with your own meta_key):
<?php echo 'Warehouse location: ' . get_post_meta($wpo_wcpdf->export->order->id,''_text_field',true); ?>
HI Ewout
Thanks for your reply!
I have added your code, but cant get the values from my custom field to show on the packingslip.
This is what I have in funitions.php
//Warhouse location // Display Fields add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); // Save Fields add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); function woo_add_custom_general_fields() { global $woocommerce, $post; echo '<div class="options_group">'; // Custom fields will be created here... // Text Field woocommerce_wp_text_input( array( 'id' => '_warehouse_location', 'label' => __( 'Lagerplats', 'woocommerce' ), 'placeholder' => 'Lagerplats', 'desc_tip' => 'true', 'description' => __( 'Fyll i lagerplats', 'woocommerce' ) ) ); echo '</div>'; } function woo_add_custom_general_fields_save( $post_id ){ // Text Field $woocommerce_warehouse_location = $_POST['_warehouse_location']; if( !empty( $woocommerce_warehouse_location ) ) update_post_meta( $post_id, '_warehouse_location', esc_attr( $woocommerce_warehouse_location ) ); } //Warehouse location ends here
And this is what I added to packing-slip.php:
<table class="order-details"> <thead> <tr> <th class="product-label"><?php _e('Product', 'wpo_wcpdf'); ?></th> <th class="quantity-label"><?php _e('Quantity', 'wpo_wcpdf'); ?></th> <th class="quantity-label"><?php _e('Warehouse location', 'wpo_wcpdf'); ?></th> </tr> </thead> <tbody> <?php $items = $wpo_wcpdf->get_order_items(); if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?><tr> <td class="description"> <span class="item-name"><?php echo $item['name']; ?></span><span class="item-meta"><?php echo $item['meta']; ?></span> <dl class="meta"> <?php if( !empty( $item['sku'] ) ) : ?><dt><?php _e( 'SKU:', 'wpo_wcpdf' ); ?></dt><dd><?php echo $item['sku']; ?></dd><?php endif; ?> <?php if( !empty( $item['weight'] ) ) : ?><dt><?php _e( 'Weight:', 'wpo_wcpdf' ); ?></dt><dd><?php echo $item['weight']; ?><?php echo get_option('woocommerce_weight_unit'); ?></dd><?php endif; ?> </dl> </td> <td class="quantity"><?php echo $item['quantity']; ?></td> <td> <?php echo 'Warehouse location: ' . get_post_meta($wpo_wcpdf->export->order->id,'_warehouse_location',true); ?></td> </tr><?php endforeach; endif; ?> </tbody> </table><!-- order-details -->
Hi Wackes,
This should work if the Warehouse Location is properly saved to the database, which I cannot guarantee. The best thing to do, if you have the tools to access the database is to look for the values yourself. Note that it looks like you are trying to do this on a per-product basis, but the value that you save is saved to the order (which means that an order can only have one warehouse location saved!)Ewout
Hi Ewout
Yes, I want is to show my custom product field per product on the packing slip: My custom field
And this is the field I want to add to the packing slip per product:
my custom product fieldCan you advice me on the correct code for this? Thanks in advance =)
Hi Wackes,
If you send an email to [email protected], I’ll see what I can do for you!Have a great day!
EwoutCan you tell me what the solution was for this? I am experiencing the same issue. Thank you ??
Hi There,
You can use the following code:<?php $product_id = $item['product_id']; $variation_id = $item['variation_id']; if (!empty($variation_id)) { // switch product_id for variation_id if set $product_id = $variation_id; } // output _warehouse_location echo get_post_meta( $product_id, '_warehouse_location', true ); ?>
Have a great day!
Hmm… still not showing up for me. I added this code to plugins > editor > Woocommerce PDF invoices & packing slips > and pasted it within woocommerce-pdf-invoices-packing-slips/woocommerce-pdf-invoices-packingslips.php
Thank you!
Hi Dhendric,
This is supposed to go into your template, not the plugin! The FAQ contains instructions on how to create a custom template, which is the first step. Then you put this code snippet where you want the custom field to show up.Have a great day!
EwoutThank you!
I am trying to display it right under the SKU and Weight. Nothing is showing up:<?php if( !empty( $item[‘sku’] ) ) : ?><dt><?php _e( ‘SKU:’, ‘wpo_wcpdf’ ); ?></dt><dd><?php echo $item[‘sku’]; ?></dd><?php endif; ?>
<?php if( !empty( $item[‘weight’] ) ) : ?><dt><?php _e( ‘Weight:’, ‘wpo_wcpdf’ ); ?></dt><dd><?php echo $item[‘weight’]; ?><?php echo get_option(‘woocommerce_weight_unit’); ?></dd><?php endif; ?>
<?php $product_id = $item[‘product_id’];
$variation_id = $item[‘variation_id’];if (!empty($variation_id)) {
// switch product_id for variation_id if set
$product_id = $variation_id;
}// output _warehouse_location
echo get_post_meta( $product_id, ‘_warehouse_location’, true );
?>I also just tried duplicating the “weight” code and then editing it with the “_warehouse_location”. Could you help me determine what would need to be edited/change in the last lines of code below. I am obviously new to PHP so am trying to learn where I am supposed to pull everything from ?? Thanks again!!!
<?php if( !empty( $item[‘sku’] ) ) : ?><dt><?php _e( ‘SKU:’, ‘wpo_wcpdf’ ); ?></dt><dd><?php echo $item[‘sku’]; ?></dd><?php endif; ?>
<?php if( !empty( $item[‘weight’] ) ) : ?><dt><?php _e( ‘Weight:’, ‘wpo_wcpdf’ ); ?></dt><dd><?php echo $item[‘weight’]; ?><?php echo get_option(‘woocommerce_weight_unit’); ?></dd><?php endif; ?>
<?php if( !empty( $item[‘weight’] ) ) : ?><dt><?php _e( ‘Warehouse Location:’, ‘wpo_wcpdf’ ); ?></dt><dd><?php echo $item[‘weight’]; ?><?php echo get_option(‘woocommerce_weight_unit’); ?></dd><?php endif; ?>
Hi Dhendric,
I’m afraid I cannot help you with this. It depends on so many things, mostly the way you have configured how the warehouse location is saved. The code I posted did work perfectly for the topic starter, so there must be something in your code that is different.Your last attempt won’t work because the warehouse location data (in the case of the topic starter) is saved with the product. Unless you would change the main plugin file, but I don’t recommend doing that.
Just to be sure that your custom template is properly recognized, do any of the changes you make to the template show up?
Ewout
Ah… I thought I had it… The “Warehouse Location” field is showing up now, however, it is not pulling the information – I copied above the exact code for the function PHP – here is the code I have in the template per your help:
<table class="order-details"> <thead> <tr> <th class="product-label"><?php _e('Product', 'wpo_wcpdf'); ?></th> <th class="quantity-label"><?php _e('Quantity', 'wpo_wcpdf'); ?></th> <th class="quantity-label"><?php _e('Warehouse location', 'wpo_wcpdf'); ?></th> </tr> </thead> <tbody> <?php $items = $wpo_wcpdf->get_order_items(); if( sizeof( $items ) > 0 ) : foreach( $items as $item ) : ?><tr> <td class="description"> <span class="item-name"><?php echo $item['name']; ?></span><span class="item-meta"><?php echo $item['meta']; ?></span> <dl class="meta"> <?php if( !empty( $item['sku'] ) ) : ?><dt><?php _e( 'SKU:', 'wpo_wcpdf' ); ?></dt><dd><?php echo $item['sku']; ?></dd><?php endif; ?> <?php if( !empty( $item['weight'] ) ) : ?><dt><?php _e( 'Weight:', 'wpo_wcpdf' ); ?></dt><dd><?php echo $item['weight']; ?><?php echo get_option('woocommerce_weight_unit'); ?></dd><?php endif; ?> </dl> </td> <td class="quantity"><?php echo $item['quantity']; ?></td> <td> <?php echo get_post_meta( $product_id, '_warehouse_location', true ); ?></td> </tr><?php endforeach; endif; ?> </tbody> </table><!-- order-details -->
Hi Dhendric,
You are missing a few lines from the code I posted. Instead of:<td> <?php echo get_post_meta( $product_id, '_warehouse_location', true );?></td>
try:
<td> <?php $product_id = $item['product_id']; $variation_id = $item['variation_id']; if (!empty($variation_id)) { // switch product_id for variation_id if set $product_id = $variation_id; } // output _warehouse_location echo get_post_meta( $product_id, '_warehouse_location', true ); ?> </td>
It’s working!! Thank you so much for all your help ??
- The topic ‘Add custom product field – Warehouse location’ is closed to new replies.