product custom fields not showing for variable products
-
https://wordpress.org/support/topic/product-custom-fields-not-showing-for-variable-products/
Looking at the above and trying to show custom fields on the invoice for both simple and variable products.
Using the $item[‘product’] it shows for simple products and $item[‘product’]->parent for variable, but I cannot get it to show for both
Many thanks in advance
-
Hi @akent64,
Can you share your code with us so we can see what might be going wrong?
The easiest way to add custom product fields is with our Premium Templates extension. This will give you access to the customizer that will let you add a ‘Custom field (product)’ block. Simply add the meta key / field name of your custom field and you’re good to go.
-
This reply was modified 7 years, 4 months ago by
kluver.
function wc_add_custom_fields_save( $post_id ){
// Text Field
$woocommerce_product_information = $_POST[‘_product_information’];
if( isset( $woocommerce_product_information ) )
update_post_meta( $post_id, ‘_product_information’, esc_attr( $woocommerce_product_information ) );
// Text Field
$woocommerce_print_information = $_POST[‘_print_information’];
if( isset( $woocommerce_print_information ) )
update_post_meta( $post_id, ‘_print_information’, esc_attr( $woocommerce_print_information ) );
// Text Field
$woocommerce_custom_text_font = $_POST[‘_custom_text_font’];
if( isset( $woocommerce_custom_text_font ) )
update_post_meta( $post_id, ‘_custom_text_font’, esc_attr( $woocommerce_custom_text_font ) );
// Text Field
$woocommerce_custom_text_colour = $_POST[‘_custom_text_colour’];
if( isset( $woocommerce_custom_text_colour ) )
update_post_meta( $post_id, ‘_custom_text_colour’, esc_attr( $woocommerce_custom_text_colour ) );
// Text Field
$woocommerce_custom_text_position = $_POST[‘_custom_text_position’];
if( isset( $woocommerce_custom_text_position ) )
update_post_meta( $post_id, ‘_custom_text_position’, esc_attr( $woocommerce_custom_text_position ) );
// Text Field
$woocommerce_custom_text_height = $_POST[‘_custom_text_height’];
if( isset( $woocommerce_custom_text_height ) )
update_post_meta( $post_id, ‘_custom_text_height’, esc_attr( $woocommerce_custom_text_height ) );}
add_action( ‘wpo_wcpdf_after_item_meta’, ‘wpo_wcpdf_product_custom_field’, 10, 3 );
function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) {
// check if product exists first
if (empty($item[‘product’])) return;// replace ‘Location’ with your custom field name!
$field_name = ‘_product_information’;
$_product_information = method_exists($item[‘product’], ‘get_meta’) ? $item[‘product’]->get_meta($field_name,true,’edit’) : get_post_meta( $item[‘product’]->id, $field_name, true );
$field_name = ‘_print_information’;
$_print_information = method_exists($item[‘product’], ‘get_meta’) ? $item[‘product’]->get_meta($field_name,true,’edit’) : get_post_meta( $item[‘product’]->id, $field_name, true );
$field_name = ‘_custom_text_font’;
$_custom_text_font = method_exists($item[‘product’], ‘get_meta’) ? $item[‘product’]->get_meta($field_name,true,’edit’) : get_post_meta( $item[‘product’]->id, $field_name, true );
$field_name = ‘_custom_text_colour’;
$_custom_text_colour = method_exists($item[‘product’], ‘get_meta’) ? $item[‘product’]->get_meta($field_name,true,’edit’) : get_post_meta( $item[‘product’]->id, $field_name, true );
$field_name = ‘_custom_text_position’;
$_custom_text_position = method_exists($item[‘product’], ‘get_meta’) ? $item[‘product’]->get_meta($field_name,true,’edit’) : get_post_meta( $item[‘product’]->id, $field_name, true );
$field_name = ‘_custom_text_height’;
$_custom_text_height = 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($_product_information)) {
echo ‘<div class=”invoice-information”><b>Product Information: </b>’.$_product_information.'</div>’;
}
if (!empty($_print_information)) {
echo ‘<div class=”invoice-information”><b>Print Information: </b>’.$_print_information.'</div>’;
}
if (!empty($_custom_text_font OR $_custom_text_colour OR $_custom_text_position OR $_custom_text_height)) {
echo ‘<div class=”invoice-information”><b>Custom Text Info: </b>’.$_custom_text_font.’, ‘.$_custom_text_colour.’, ‘.$_custom_text_position.’, ‘.$_custom_text_height.'</div>’;
}
}add_filter( ‘wc_product_enable_dimensions_display’, ‘__return_false’ );
add_action( ‘wpo_wcpdf_custom_styles’, ‘wpo_wcpdf_custom_styles’ );
function wpo_wcpdf_custom_styles () {
?>
.invoice-information {
color: grey;
font-size: 0.8em;
}.weight {
display: none;
}<?php
}Hi @akent64,
It looks like you were using
‘where you should be using'. This might be causing some problems. Also the last if statement in the wpo_wcpdf_after_item_meta hook was not correct. I rewrote the code so it should work but I would take a look at that last if statement because now you will get stuck with a lot of extra comma’s if only one value is returned.<?php function wc_add_custom_fields_save( $post_id ){ // Text Field $woocommerce_product_information = $_POST['_product_information']; if( isset( $woocommerce_product_information ) ) { update_post_meta( $post_id, '_product_information', esc_attr( $woocommerce_product_information ) ); } // Text Field $woocommerce_print_information = $_POST['_print_information']; if( isset( $woocommerce_print_information ) ) { update_post_meta( $post_id, '_print_information', esc_attr( $woocommerce_print_information ) ); } // Text Field $woocommerce_custom_text_font = $_POST['_custom_text_font']; if( isset( $woocommerce_custom_text_font ) ) { update_post_meta( $post_id, '_custom_text_font', esc_attr( $woocommerce_custom_text_font ) ); } // Text Field $woocommerce_custom_text_colour = $_POST['_custom_text_colour']; if( isset( $woocommerce_custom_text_colour ) ) { update_post_meta( $post_id, '_custom_text_colour', esc_attr( $woocommerce_custom_text_colour ) ); } // Text Field $woocommerce_custom_text_position = $_POST['_custom_text_position']; if( isset( $woocommerce_custom_text_position ) ) { update_post_meta( $post_id, '_custom_text_position', esc_attr( $woocommerce_custom_text_position ) ); } // Text Field $woocommerce_custom_text_height = $_POST['_custom_text_height']; if( isset( $woocommerce_custom_text_height ) ) { update_post_meta( $post_id, '_custom_text_height', esc_attr( $woocommerce_custom_text_height ) ); } } add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_product_custom_field', 10, 3 ); function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) { // check if product exists first if (empty($item['product'])) return; // replace 'Location' with your custom field name! $field_name = '_product_information'; $_product_information = method_exists($item['product'], 'get_meta') ? $item['product']->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true ); $field_name = '_print_information'; $_print_information = method_exists($item['product'], 'get_meta') ? $item['product']->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true ); $field_name = '_custom_text_font'; $_custom_text_font = method_exists($item['product'], 'get_meta') ? $item['product']->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true ); $field_name = '_custom_text_colour'; $_custom_text_colour = method_exists($item['product'], 'get_meta') ? $item['product']->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true ); $field_name = '_custom_text_position'; $_custom_text_position = method_exists($item['product'], 'get_meta') ? $item['product']->get_meta($field_name,true,'edit') : get_post_meta( $item['product']->id, $field_name, true ); $field_name = '_custom_text_height'; $_custom_text_height = 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($_product_information)) { echo '<div class="invoice-information"><b>Product Information: </b>'.$_product_information.'</div>'; } if (!empty($_print_information)) { echo '<div class="invoice-information"><b>Print Information: </b>'.$_print_information.'</div>'; } if (!empty($_custom_text_font) || !empty($_custom_text_colour) || !empty($_custom_text_position) || !empty($_custom_text_height)) { echo '<div class="invoice-information"><b>Custom Text Info: </b>'.$_custom_text_font.', '.$_custom_text_colour.', '.$_custom_text_position.', '.$_custom_text_height.'</div>'; } } add_filter( 'wc_product_enable_dimensions_display', '__return_false' ); add_action( 'wpo_wcpdf_custom_styles', 'wpo_wcpdf_custom_styles' ); function wpo_wcpdf_custom_styles () { ?> .invoice-information { color: grey; font-size: 0.8em; } .weight { display: none; } <?php }Thanks so much, this is showing on the invoice for simple products but not variable products, when I add in the advised fix it shows for variable but not for simple products, so I can never view the extra fields if an order contains both types of product
I have simplified some of your code (moved a lot of legacy checks too, assuming you’re on WC3+) and added a check that only uses the parent if it’s a variation:
$product = $item['product']->is_type( 'variation' ) ? wc_get_product( $item['product']->get_parent_id() ) : $item['product'];Full snippet (I omitted the filters/actions that are not relevant for this context, you can keep them as is):
add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_product_custom_field', 10, 3 ); function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) { // check if product exists first if (empty($item['product'])) return; // use parent for variable products $product = $item['product']->is_type( 'variation' ) ? wc_get_product( $item['product']->get_parent_id() ) : $item['product']; if ( $_product_information = $product->get_meta( '_product_information' ) ) { echo '<div class="invoice-information"><b>Product Information: </b>'.$_product_information.'</div>'; } if ( $_print_information = $product->get_meta( '_print_information' ) ) { echo '<div class="invoice-information"><b>Print Information: </b>'.$_print_information.'</div>'; } $_custom_text = array(); $_custom_text['font'] = $product->get_meta( '_custom_text_font' ); $_custom_text['colour'] = $product->get_meta( '_custom_text_colour' ); $_custom_text['position'] = $product->get_meta( '_custom_text_position' ); $_custom_text['height'] = $product->get_meta( '_custom_text_height' ); $_custom_text = array_filter($_custom_text); // remove empty values if (!empty($_custom_text) ) { echo '<div class="invoice-information"><b>Custom Text Info: </b>'.implode(', ', $_custom_text).'</div>'; } }Hope that helps!
That doesn’t just help, that’s amazing!!!
Thanks you so much, I hope will help others looking for a solution aswel 🙂
Very glad to hear it solves your issue! If you can spare a moment, we’d appreciate a plugin review here on wordpress.org: https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/#new-post
Thanks in advance and happy selling!
-
This reply was modified 7 years, 4 months ago by
The topic ‘product custom fields not showing for variable products’ is closed to new replies.