
Viewing documentation for the Nutrition for WooCommerce Plugin
Viewing documentation for the Nutrition for WooCommerce Plugin
Whether you’re setting up products programmatically, creating custom workflows, or building simple integrations, this guide shows you how to work with nutrition data through code. The plugin stores data using WordPress post meta, making it accessible through standard WordPress functions and custom helper functions.
The plugin uses several meta keys per product/variation. You shouldn’t edit these directly. The meta keys are for reference.
// Enable/disable nutrition for product
'_nw_nutrition_enabled' => 'yes' // or 'no'
// For variations
'_nw_variation_nutrition_enabled' => '1' // or empty
// Input method
'_nw_nutrition_input_method' => 'per_100g' // or 'per_serving'
// Serving size
'_nw_serving_size_amount' => '30' // numeric string
'_nw_serving_size_unit' => 'g' // unit string
// All nutrition data consolidated
'_nw_nutrition_data' => array(
'energy' => '1046',
'protein' => '20.5',
'fat_total' => '8.2',
// ... other nutrition fields
)
Code language: PHP (php)
/**
* Get complete nutrition data for a product (recommended method)
*/
$nutrition_data = nw_get_product_nutrition_data( $product_id );
if ( $nutrition_data && $nutrition_data['enabled'] ) {
echo "Energy: " . $nutrition_data['nutrition_data']['energy'] . " kJ\n";
echo "Protein: " . $nutrition_data['nutrition_data']['protein'] . " g\n";
echo "Serving Size: " . $nutrition_data['serving_amount'] . $nutrition_data['serving_unit'] . "\n";
}
/**
* Get specific nutrition values
*/
$energy = nw_get_nutrition_value( $product_id, 'energy' );
$protein = nw_get_nutrition_value( $product_id, 'protein' );
$fat = nw_get_nutrition_value( $product_id, 'fat_total' );
/**
* Check if nutrition is enabled
*/
if ( nw_is_nutrition_enabled( $product_id ) ) {
// Product has nutrition data
$values = nw_get_nutrition_values( $product_id );
foreach ( $values as $field_id => $value ) {
echo "{$field_id}: {$value}\n";
}
}
Code language: PHP (php)
/**
* Get nutrition per 100g (regardless of input method)
*/
$per_100g = nw_get_nutrition_per_100g( $product_id );
if ( $per_100g ) {
echo "Protein per 100g: " . $per_100g['protein'] . " g\n";
echo "Energy per 100g: " . $per_100g['energy'] . " kJ\n";
}
/**
* Get serving size information
*/
$serving = nw_get_serving_size( $product_id );
echo "Serving: " . $serving['amount'] . " " . $serving['unit'] . "\n";
echo "Input method: " . $serving['input_method'] . "\n";
/**
* Convert energy units manually if needed
*/
$energy_kj = nw_get_nutrition_value( $product_id, 'energy' );
if ( $energy_kj ) {
$energy_kcal = $energy_kj * 0.239006; // kJ to kcal conversion
echo "Energy: {$energy_kj} kJ / " . round( $energy_kcal ) . " kcal\n";
}
Code language: PHP (php)
/**
* Add nutrition data to a product (recommended method)
*/
$nutrition_data = array(
'energy' => '1046', // kJ
'protein' => '20.5',
'fat_total' => '8.2',
'carbohydrates_total' => '45.1',
'sodium' => '320',
'ingredients' => 'Wheat flour, sugar, eggs, butter',
'allergens' => 'Contains: wheat, eggs, milk'
);
$settings = array(
'input_method' => 'per_100g',
'serving_amount' => '30',
'serving_unit' => 'g',
'servings_per_container' => '10'
);
// Set nutrition data using helper function
$success = nw_set_product_nutrition_data( 123, $nutrition_data, $settings );
if ( $success ) {
echo "Nutrition data saved successfully!";
}
/**
* Quick setup with minimal configuration
*/
$simple_nutrition = array(
'energy' => '850',
'protein' => '15.0',
'fat_total' => '5.5'
);
// Uses default settings (per_100g, g units)
nw_set_product_nutrition_data( 456, $simple_nutrition );
/**
* Remove nutrition data from a product
*/
nw_remove_product_nutrition_data( $product_id );
Code language: PHP (php)
/**
* Add nutrition data to a specific variation
*/
function add_nutrition_to_variation( $variation_id, $nutrition_data, $serving_amount = 30, $serving_unit = 'g' ) {
$settings = array(
'input_method' => 'per_100g',
'serving_amount' => $serving_amount,
'serving_unit' => $serving_unit
);
return nw_set_product_nutrition_data( $variation_id, $nutrition_data, $settings );
}
/**
* Copy parent nutrition to all variations (useful for setup)
*/
function copy_parent_nutrition_to_variations( $parent_product_id ) {
$product = wc_get_product( $parent_product_id );
if ( ! $product || ! $product->is_type( 'variable' ) ) {
return false;
}
// Get parent nutrition data
$parent_nutrition = nw_get_product_nutrition_data( $parent_product_id );
if ( ! $parent_nutrition || ! $parent_nutrition['enabled'] ) {
return false;
}
$variations = $product->get_children();
foreach ( $variations as $variation_id ) {
$settings = array(
'input_method' => $parent_nutrition['input_method'],
'serving_amount' => $parent_nutrition['serving_amount'],
'serving_unit' => $parent_nutrition['serving_unit'],
'servings_per_container' => $parent_nutrition['servings_per_container']
);
nw_set_product_nutrition_data(
$variation_id,
$parent_nutrition['nutrition_data'],
$settings
);
}
return count( $variations );
}
Code language: PHP (php)
/**
* Standard nutrition field IDs
*/
$nutrition_fields = array(
// Energy
'energy', // kJ
// Macronutrients
'protein', // g
'fat_total', // g
'fat_saturated', // g
'trans_fat', // g
'carbohydrates_total', // g
'sugars', // g
'added_sugars', // g
'dietary_fiber', // g
// Minerals
'sodium', // mg
'calcium', // mg
'iron', // mg
'potassium', // mg
'cholesterol', // mg
// Vitamins
'vitamin_d', // mcg
// Additional
'ingredients', // text
'allergens' // text
);
Code language: PHP (php)
Use the nw_nutrition_field_groups
filter to add custom fields:
/**
* Add custom nutrition fields to existing groups
*/
add_filter( 'nw_nutrition_field_groups', 'add_custom_nutrition_fields' );
function add_custom_nutrition_fields( $field_groups ) {
// Add Magnesium to minerals group
foreach ( $field_groups as &$group ) {
if ( $group['id'] === 'minerals_group' ) {
$group['fields'][] = array(
'id' => 'magnesium',
'label' => __( 'Magnesium', 'your-textdomain' ),
'field_type' => 'weight',
'unit' => 'mg'
);
}
// Add Omega-3 to fat group
if ( $group['id'] === 'fat_group' ) {
$group['fields'][] = array(
'id' => 'omega_3',
'label' => __( 'Omega-3 Fatty Acids', 'your-textdomain' ),
'field_type' => 'weight',
'unit' => 'g'
);
}
}
return $field_groups;
}
Code language: PHP (php)
Add entirely new nutrition field groups:
/**
* Add custom nutrition field groups
*/
add_filter( 'nw_nutrition_field_groups', 'add_custom_field_groups' );
function add_custom_field_groups( $field_groups ) {
// Add Amino Acids group
$field_groups[] = array(
'id' => 'amino_acids_group',
'label' => __( 'Amino Acids', 'your-textdomain' ),
'type' => 'group',
'fields' => array(
array(
'id' => 'leucine',
'label' => __( 'Leucine', 'your-textdomain' ),
'field_type' => 'weight',
'unit' => 'mg'
),
array(
'id' => 'lysine',
'label' => __( 'Lysine', 'your-textdomain' ),
'field_type' => 'weight',
'unit' => 'mg'
),
array(
'id' => 'methionine',
'label' => __( 'Methionine', 'your-textdomain' ),
'field_type' => 'weight',
'unit' => 'mg'
)
)
);
// Add Certifications group
$field_groups[] = array(
'id' => 'certifications_group',
'label' => __( 'Certifications', 'your-textdomain' ),
'type' => 'group',
'fields' => array(
array(
'id' => 'organic_certified',
'label' => __( 'Organic Certification', 'your-textdomain' ),
'field_type' => 'textarea'
),
array(
'id' => 'gluten_free',
'label' => __( 'Gluten-Free Statement', 'your-textdomain' ),
'field_type' => 'textarea'
),
array(
'id' => 'allergen_warnings',
'label' => __( 'Additional Allergen Warnings', 'your-textdomain' ),
'field_type' => 'textarea'
)
)
);
return $field_groups;
}
Code language: PHP (php)
Available field types and their properties:
$field_types = array(
'weight' => array(
'default_unit' => 'g',
'input_type' => 'number',
'validation' => 'numeric'
),
'volume' => array(
'default_unit' => 'ml',
'input_type' => 'number',
'validation' => 'numeric'
),
'energy' => array(
'default_unit' => 'kJ',
'input_type' => 'number',
'validation' => 'numeric',
'special_display' => true // Shows both kJ and kcal
),
'number' => array(
'default_unit' => '',
'input_type' => 'number',
'validation' => 'numeric'
),
'textarea' => array(
'input_type' => 'editor',
'validation' => 'wp_kses_post'
)
);
Code language: PHP (php)
/**
* Add advanced custom fields with full configuration
*/
add_filter( 'nw_nutrition_field_groups', 'add_advanced_nutrition_fields' );
function add_advanced_nutrition_fields( $field_groups ) {
$field_groups[] = array(
'id' => 'specialty_nutrients_group',
'label' => __( 'Specialty Nutrients', 'your-textdomain' ),
'type' => 'group',
'fields' => array(
array(
'id' => 'probiotics',
'label' => __( 'Probiotics', 'your-textdomain' ),
'description' => __( 'Enter as CFU (Colony Forming Units)', 'your-textdomain' ),
'field_type' => 'number',
'unit' => 'CFU',
'placeholder' => '1000000000', // 1 billion CFU
'step' => '1000000' // Step by millions
),
array(
'id' => 'caffeine',
'label' => __( 'Caffeine', 'your-textdomain' ),
'description' => __( 'Natural and added caffeine content', 'your-textdomain' ),
'field_type' => 'weight',
'unit' => 'mg',
'placeholder' => '95' // Typical coffee amount
),
array(
'id' => 'alcohol_content',
'label' => __( 'Alcohol by Volume', 'your-textdomain' ),
'field_type' => 'number',
'unit' => '%',
'step' => '0.1',
'max' => '100'
)
)
);
return $field_groups;
}
Code language: PHP (php)
Custom fields are automatically stored in the _nw_nutrition_data
meta key alongside standard fields. No additional database setup required.
/**
* Example of retrieving custom field data
*/
function get_custom_nutrition_data( $product_id ) {
$nutrition_data = get_post_meta( $product_id, '_nw_nutrition_data', true );
return array(
'probiotics' => $nutrition_data['probiotics'] ?? '',
'caffeine' => $nutrition_data['caffeine'] ?? '',
'alcohol_content' => $nutrition_data['alcohol_content'] ?? '',
'organic_certified' => $nutrition_data['organic_certified'] ?? ''
);
}
Code language: PHP (php)
// Field registration and modification
'nw_nutrition_field_groups' // Add/modify field groups
Code language: JavaScript (javascript)
This guide provides the foundation for programmatic nutrition data management. Follow WordPress best practices for data validation, sanitization, and error handling when working with nutrition data.