Currently using WordPress 5.1.1 and WooCommerce 3.5.7. My WooCommerce store has around 500 products, made up of simple and variable products.
Each product naturally has a SKU, but each product also has a unique ID code called 'Commodity Code'. I sell specific products to a specific industry.
I have added the code for the Custom Fields for Simple and Variable product in my functions.php file, and this works great at the moment.
My problem is, I have trying to get the 'Commodity Code' to appear under the product title in the Cart, Check Out, invoice and email.
I have read various tutorials on the internet to help me, but I am none the wiser as each tutorial does similar things in different ways. Most of the tutorials assume a user has entered the data via the front-end but my data is preset.
Tutorials I have used to help me are:
- https://businessbloomer.com/woocommerce-add-custom-field-product-variation/
- https://www.liquidweb.com/blog/custom-fields-woocommerce-products/
- https://www.tychesoftwares.com/how-to-add-custom-fields-to-woocommerce-products-subsequently-through-the-order-cycle/
<?php
// Add WooCommerce Custom Field for Commodity Code
function vp_add_commodity_code() {
$args = array(
'id' => 'vp_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' ),
);
woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'vp_add_commodity_code' );
// Save Commodity Code into WooCommerce Database
function vp_save_commodity_code( $post_id ) {
// grab the Commodity Code
$sku = isset( $_POST[ 'vp_commodity_code' ] ) ? sanitize_text_field( $_POST[ 'vp_commodity_code' ] ) : '';
// grab the product
$product = wc_get_product( $post_id );
// save the Commodity Code custom field
$product->update_meta_data( 'vp_commodity_code', $sku );
$product->save();
}
add_action( 'woocommerce_process_product_meta', 'vp_save_commodity_code' );
// Display Commodity Code on the Frontend
function vp_display_commodity_code() {
global $post;
// Check for the Commodity Code value
$product = wc_get_product( $post->ID );
$title = $product->get_meta( 'vp_commodity_code' );
if( $title ) {
// Only display the field if we've got a value for the field title
printf(
'<div class="vpcommoditycode-wrapper"><strong>Commodity Code: </strong>%s</div>',
esc_html( $title )
);
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'vp_display_commodity_code' );
// Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'comcode_add_custom_field_to_variations', 10, 3 );
function comcode_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']',
'class' => 'short',
'label' => __( 'Community Code', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true ),
'placeholder' => __( 'Enter Commodity Code here', 'woocommerce' ),
'desc_tip' => true,
'description' => __( 'This field is for the Commodity Code of the product.', 'woocommerce' ),
)
);
}
// Save custom field on product variation
add_action( 'woocommerce_save_product_variation', 'comcode_save_custom_field_variations', 10, 2 );
function comcode_save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}
// Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'comcode_add_custom_field_variation_data' );
function comcode_add_custom_field_variation_data( $variations ) {
$variations['custom_field'] = '<div class="woocommerce_custom_field"><strong>Commodity Code: </strong><span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
return $variations;
}
?>
Can some kind PHP/WooCommerce genius help me here please by either providing the code or point me to a tutorial that would help me or name a third party WordPress Plugin that will do this.