My Courses > CustomizeWoo > Module 5 > Lesson 03: Custom Fields

Custom Fields

MARK LESSON AS COMPLETE

Custom fields (post meta, metadata) allow you to store additional information with each WordPress post (WooCommerce products included). They can then be displayed/used anywhere.

Video

Please note: English captions are enabled by default (click on “CC” in the video player to disable). Also, you can click on the “gear” icon to manage quality and playback speed. Enjoy!

Useful Links

WooCommerce: Add Custom Product Fields (e.g. RRP) Without a Plugin: https://www.businessbloomer.com/woocommerce-display-rrp-msrp-manufacturer-price/

WooCommerce: Add Custom Field to Product Variations: https://www.businessbloomer.com/woocommerce-add-custom-field-product-variation/

WordPress Custom Fields: https://wordpress.org/support/article/custom-fields/

12 thoughts on “Custom Fields

  1. HI Rodolfo,
    I want to show an specific attribute in the single_product_summary after the price and on the left site. I know you told this in one of the courses but I can’t find the specific course anymore. Can you guide me in the right direction?

    Marco

    1. Thanks for your question Marco πŸ™‚

      So, to display a custom field you can use https://businessbloomer.com/woocommerce-show-number-products-sold-product-page/ (in this case it gets the total sales, but you can get the custom field value with get_post_meta() – here’s an example: https://businessbloomer.com/woocommerce-show-product-custom-field-in-the-category-pages/)

      Let me know

      1. Ciao Rodolfo,
        I am not getting there. The attribute is pa_merk and I want it after woocommerce_single_product_summary. I think it is easy but I am not seeing it. Could you …..:-)

        add_action( 'woocommerce_single_product_summary', 'merk_toevoegen', 10);
         function merk_toevoegen() {
           echo 'pa_merk';
        }
        
        1. Ah – sorry – so it’s a product attribute. Try with:

          global $product;
          echo $product->get_attribute( 'pa_merk' );
          
          1. YEP, that’s it. Thanks

            1. And if I want that attribute to link to the archive page?

              1. I would search through the WooCommerce plugin files for the wc_display_product_attributes() function. At some stage within this function you find this for a single attribute:

                $value_name = esc_html( $attribute_value->name );
                if ( $attribute_taxonomy->attribute_public ) {
                	$values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
                } else {
                	$values[] = $value_name;
                }
                

                So, you first need to get the $attribute_value.

                Another option: https://docs.woocommerce.com/document/display-product-attribute-archive-links/

                1. Thanks for all that!

  2. Thank you – but I need to just add a number (Item Code) to be displayed – what should be the ‘data type’?

    1. You can just remove that line, as it’s not a price πŸ™‚

  3. Hi Rodolfo,
    I have watched the lesson you have recommended and read the article, but something is still not clear;
    I have used this code – from your website – to add a custom field on variation products:

    // 1. Add custom field input @ Product Data &gt; Variations &gt; Single Variation
      
    add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_custom_field_to_variations', 10, 3 ); 
     
    function bbloomer_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input( array( 
    'id' =&gt; 'custom_field[' . $loop . ']', 
    'class' =&gt; 'short', 
    'label' =&gt; __( 'Item Code', 'woocommerce' ),
    'value' =&gt; get_post_meta( $variation-&gt;ID, 'custom_field', true )
    ) 
    );      
    }
      
    // -----------------------------------------
    // 2. Save custom field on product variation save
     
    add_action( 'woocommerce_save_product_variation', 'bbloomer_save_custom_field_variations', 10, 2 );
      
    function bbloomer_save_custom_field_variations( $variation_id, $i ) {
        $custom_field = $_POST['custom_field'][$i];
        if ( ! empty( $custom_field ) ) {
            update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
        } else delete_post_meta( $variation_id, 'custom_field' );
    }
      
    // -----------------------------------------
    // 3. Store custom field value into variation data
      
    add_filter( 'woocommerce_available_variation', 'bbloomer_add_custom_field_variation_data' );
     
    function bbloomer_add_custom_field_variation_data( $variations ) {
        $variations['custom_field'] = 'Item Code: ' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '';
        return $variations;
    }
    

    Can you let me know what changes I need to add to this code in order for the custom field to be displayed also on simple products pages?
    If it’s to much trouble – can you recommend a professional that can to it for money? I know it’s fairly easy for an experienced developer – I just need it solved fast and I am just in the
    beginning of my WooCommerce programming journey..

    Thank you!

    1. Hello Yonatan!

      For simple products you can use this version: https://businessbloomer.com/woocommerce-display-rrp-msrp-manufacturer-price/

      Let me know πŸ™‚

Questions? Feedback? Support? Leave your comment now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]

Your email address will not be published. Required fields are marked *