• Resolved molin449

    (@molin449)


    Im using ACF-plugin to show a custom text on the product page showing the warrenty of the product. I would like to show that on the packing slips and invoices as well.

    I have looked at this link: “https://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/displaying-product-custom-fields/” and that seems like the thing i am looking for, but when i change the name ‘locations’ from the example, to my custom field name, it doesn’t seem to work?

    The name of my ACF custom field is ‘garantier’

    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 = 'garantier';
        $location = 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($location)) {
            echo '<div class="product-location">Location: '.$location.'</div>';
        }
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    This should work if it’s indeed stored under that name. The only thing I can think of is that the name is different or if this is a variable product and the field is only stored in the main product.
    If you want to be sure about the field name, you could install the Store Toolkit plugin and navigate to the bottom of the corresponding product page in the backend to see if you find the field listed under “Product Post Meta”

    Thread Starter molin449

    (@molin449)

    Thank you so much for your answer Ewout! It was because it was a variable product 🙂

    But is it possible to show it on a variable product too? When im using the Store Toolkit plugin, the ‘garantier’ field name shows up, also on the variable product. Shouldn’t it then be able to get the field? 🙂

    Plugin Contributor Ewout

    (@pomegranate)

    Yes, it should be able to get the field, but WooCommerce doesn’t know whether you want it from the main product or the variation (and will try to get it from the variation by default).

    Here’s a version of your action that reads the value from the main product data for variable products:

    
    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'];
    
    	$field_name = 'garantier';
    	if ( $location = $product->get_meta( $field_name ) ) {
    		echo '<div class="product-location">Location: '.$location.'</div>';
    	}
    }
    
    Thread Starter molin449

    (@molin449)

    AWESOME that worked perfectly! Thank you so much!!

    Plugin Contributor Ewout

    (@pomegranate)

    You’re welcome, glad to hear that works for you!
    If you can spare a moment, we always appreciate a review here on wordpress.org:

    Thanks in advance and happy selling! https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/#new-post

    Thread Starter molin449

    (@molin449)

    Yes of course, i just dropped a 5 star review. Thank you so much for awesome support!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Need to display “product custom fields”’ is closed to new replies.