WooCommerce: Add/display Product or Variation custom field everywhere



PHP Snippet 1:

// Admin: Add custom field
add_action('woocommerce_product_options_sku', 'vp_add_commodity_code' );
function vp_add_commodity_code(){

    woocommerce_wp_text_input( array(
        'id'          => '_commodity_code',
        'label'       => __('Commodity Code', 'woocommerce' ),
        'placeholder' => __('Enter Commodity Code here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('This field is for the Commodity Code of the product.', 'woocommerce' ),
    ) );
}

// Admin: Save custom field value for simple product inventory options
add_action('woocommerce_admin_process_product_object', 'vp_product_save_commodity_code', 10, 1 );
function vp_product_save_commodity_code( $product ){
    if( isset($_POST['_commodity_code']) )
        $product->update_meta_data( '_commodity_code', sanitize_text_field($_POST['_commodity_code']) );
}

// Admin: Add custom field in product variations options pricing
add_action( 'woocommerce_variation_options_pricing', 'vp_add_variation_commodity_code', 10, 3 );
function vp_add_variation_commodity_code( $loop, $variation_data, $variation ){

   woocommerce_wp_text_input( array(
        'id'          => '_commodity_code['.$loop.']',
        'label'       => __('Commodity Code', 'woocommerce' ),
        'placeholder' => __('Enter Commodity Code here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('This field is for the Commodity Code of the product.', 'woocommerce' ),
        'value'       => get_post_meta( $variation->ID, '_commodity_code', true )
    ) );
}

// Admin: Save custom field value from product variations options pricing
add_action( 'woocommerce_save_product_variation', 'save_barcode_variations', 10, 2 );
function save_barcode_variations( $variation_id, $i ){
    if( isset($_POST['_commodity_code'][$i]) ){
        update_post_meta( $variation_id, '_commodity_code', sanitize_text_field($_POST['_commodity_code'][$i]) );
    }
}

// Frontend: Display Commodity Code on product
add_action( 'woocommerce_before_add_to_cart_button', 'vp_product_display_commodity_code' );
function vp_product_display_commodity_code() {
    global $product;

    if( $value = $product->get_meta( '_commodity_code' ) ) {
        echo '<div class="vp-ccode-wrapper"><strong>' . __("Commodity Code", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</div>';
    }
}

// Frontend: Display Commodity Code on product variations
add_filter( 'woocommerce_available_variation', 'vp_variation_display_commodity_code', 10, 3 );
function vp_variation_display_commodity_code( $data, $product, $variation ) {

    if( $value = $variation->get_meta( '_commodity_code' ) ) {
        $data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Commodity Code", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</small></p>';
    }

    return $data;
}

// Frontend: Display Commodity Code on cart
add_filter( 'woocommerce_cart_item_name', 'vp_cart_display_commodity_code', 10, 3 );
function vp_cart_display_commodity_code( $item_name, $cart_item, $cart_item_key ) {
    if( ! is_cart() )
        return $item_name;

    if( $value = $cart_item['data']->get_meta('_commodity_code') ) {
        $item_name .= '<br><small class="vp-ccode"><strong>' . __("Commodity Code", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }
    return $item_name;
}

// Frontend: Display Commodity Code on checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'vp_checkout_display_commodity_code', 10, 3 );
function vp_checkout_display_commodity_code( $item_qty, $cart_item, $cart_item_key ) {
    if( $value = $cart_item['data']->get_meta('_commodity_code') ) {
        $item_qty .= '<br><small class="vp-ccode"><strong>' . __("Commodity Code", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }
    return $item_qty;
}

// Save Commodity Code to order items (and display it on admin orders)
add_filter( 'woocommerce_checkout_create_order_line_item', 'vp_order_item_save_commodity_code', 10, 4 );
function vp_order_item_save_commodity_code( $item, $cart_item_key, $cart_item, $order ) {
    if( $value = $cart_item['data']->get_meta('_commodity_code') ) {
        $item->update_meta_data( '_commodity_code', esc_attr( $value ) );
    }
    return $item_qty;
}

// Frontend & emails: Display Commodity Code on orders
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_commodity_code', 10, 4 );
function vp_order_item_display_commodity_code( $item_id, $item, $order, $plain_text ) {
    // Not on admin
    //if( is_admin() ) return;

    if( $value = $item->get_meta('_commodity_code') ) {
        $value = '<strong>' . __("Commodity Code", "woocommerce") . ':</strong> ' . esc_attr( $value );

        // On orders
        if( is_wc_endpoint_url() )
            echo '<div class="vp-ccode"><small>' . $value . '</small></div>';
        // On Emails
        else
            echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
    }
}