Woocommerce Additional Information Tab: Adding product custom field value - php

I have the following code working to create the Additional information tab in woocommerce with my own values however I want to pull the information from the built-in product custom field.
add_filter( 'woocommerce_display_product_attributes', 'custom_product_additional_information', 10, 2 );
function custom_product_additional_information( $product_attributes, $product ) {
// First row
$product_attributes[ 'attribute_' . 'custom-one' ] = array(
'label' => __('Label One'),
'value' => __('Value 1'),
);
// Second row
$product_attributes[ 'attribute_' . 'custom-two' ] = array(
'label' => __('Label Two'),
'value' => __('Value 2'),
);
return $product_attributes;
}
This is the current code I use to output the custom field, how do I output this in Value 1 in functions.php file in the working code above?
echo get_post_meta( get_the_ID(), 'Size', true );

Try the following replacing in your code:
'value' => __('Value 1'),
with:
'value' => $product->get_meta('Size'),
It should work.

Related

WooCommerce how to update product category description and thumbnail ID

I've got an array of data with which I'd like to update my products categories (taxanomy) metadata. Specifically, I'm trying to update description as well as thumbnail url values. I tried to use multiple wordpress functions but none of them worked! I didn't get any error but those values didn't get updated either.
$row_data = array(
'Term ID' => 150,
'Name' => "my 1st category",
'Slug' => "my-1st-category",
'Term URI' => "",
'Parent Term ID' => "",
'Description' => "My best description on this category that would change your life forever!",
'Display Type' => "",
'Image' => "https://myexample.site/wp-content/"
);
// This did not work!
wp_update_term($row_data['Term ID'], 'product_cat', $row_data);
// This did not work either!
update_term_meta($row_data['Term ID'], 'description', $row_data['Description']);
// This did not work either!
update_woocommerce_term_meta($row_data['Term ID'], 'thumbnail_id', $row_data['Image']);
Is there something that i'm missing?
Is thumbnail_id the right field name that i'm using here?
Is update_woocommerce_term_meta the right function for updating the thumbnail url?
Thank you.
The main problem come from your array keys that are wrong to insert or update a term.
From your custom array (updated):
$row_data = array(
'Term ID' => 150,
'Name' => 'my 1st category',
'Slug' => 'my-1st-category',
'Taxonomy' => 'product_cat'
'Parent Term ID' => 0,
'Description' => __("My best description on this category that would change your life forever!", "woocommerce"),
'Display Type' => "",
'Image ID' => "356",
);
To update term description, use WordPress wp_update_term() function as follows:
wp_update_term( $row_data['Term ID'], $row_data['Taxonomy'], array('description' => $row_data['Description']) );
To update the thumbnail ID (and not an Image URL), use update_term_meta() function as follows:
update_term_meta( $row_data['Term ID'], 'thumbnail_id', $row_data['Image ID'] );
Both are tested an works.
To insert a new term you will use WordPress wp_insert_term() function like:
$term_data = wp_insert_term( $row_data['Name'], $row_data['Taxonomy'], array(
'description' => $row_data['Description'],
'slug' => $row_data['Slug'],
'parent' => $row_data['Parent Term ID'],
) );
if ( ! empty($row_data['Display Type']) ) {
update_term_meta( $row_data['Term ID'], 'display_type', $row_data['Display Type'] );
if ( ! empty($row_data['Image ID']) ) {
update_term_meta( $row_data['Term ID'], 'thumbnail_id', $row_data['Image ID'] );
}

Show one WooCommerce Product meta

I am using one plugin for WooCommerce GST implementation. It has one HSN field inside product page which is stored in the database as the product meta.
The plugin does not have any specific code to get the HSN value and use it anywhere.
The code for HSN in the plugin is below:
public function fn_add_product_custom_meta_box() {
woocommerce_wp_text_input(
array(
'id' => 'hsn_prod_id',
'label' => __('HSN Code', 'woocommerce' ),
'description' => __( 'HSN Code is mandatory for GST.', 'woocommerce' ),
'custom_attributes' => array( 'required' => 'required' ),
'value' => get_post_meta( get_the_ID(), 'hsn_prod_id', true )
)
);
}
public function fn_save_license_field( $post_id ) {
$value = ( $_POST['hsn_prod_id'] )? sanitize_text_field( $_POST['hsn_prod_id'] ) : '' ;
update_post_meta( $post_id, 'hsn_prod_id', $value );
}
I want to get the HSN value by importing it from product meta and I have used this code to get that without any result:
<?php $meta = get_post_meta( get_the_ID(), 'hsn_prod_id', true ); ?>
Can anyone please check my code and correct me how to add the HSN value anywhere using the code?
I am using the code inside a different plugin to get the HSN code.
Thank you.

Add and save admin product variations custom field in Woocommerce

So I've got the following code which makes me add a Barcode field to the Inventory Options of a product.
Now I also want to add this to each variations so I can easily add Variation Products when I scan the Barcode of the product via the WooCommerce Point of Sale plugin.
Here is what I got currently:
// Add Barcode field in simple product inventory options
add_action('woocommerce_product_options_sku','add_barcode',10,0);
function add_barcode(){
global $woocommerce,$post;
woocommerce_wp_text_input(
array(
'id' => '_barcode',
'label' => __('Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('Scan barcode.','woocommerce'),
'value' => get_post_meta($post->ID,'_barcode',true)
)
);
}
// Save Barcode field value for simple product inventory options
add_action('woocommerce_process_product_meta','save_barcode',10,1);
function save_barcode($post_id){
if(!empty($_POST['_barcode']))
update_post_meta($post_id,'_barcode',sanitize_text_field($_POST['_barcode']));
}
// Add a Barcode field in product variations options
add_action('woocommerce_product_after_variable_attributes','add_barcode_variations',10,3);
function add_barcode_variations($loop,$variation_data,$variation){
woocommerce_wp_text_input(
array(
'id' => '_barcode[' . $variation->ID . ']',
'label' => __('Variation Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('Scan barcode.','woocommerce'),
'value' => get_post_meta($variation->ID,'_barcode',true)
)
);
}
// Save Barcode field for product variations options
add_action( 'woocommerce_save_product_variation','save_barcode_variations',10,2);
function save_barcode_variations($post_id){
$barcode = $_POST['_barcode'][$post_id];
if(!empty($barcode)) update_post_meta($post_id,'_barcode',sanitize_text_field($barcode));
}
// Set POS Custom Code
add_filter('woocommerce_pos_barcode_meta_key','pos_barcode_field');
function pos_barcode_field(){
return '_barcode';
}
But the problem here is, that with that I now added a part for the variation, that if I update the product the main barcode field in the Inventory settings shows "Array" instead of the provided barcode.
I assume that this has something to do with the ID being the same for the variations as the original field other than the variationID at the end. The reason the ID requires to be the same as the WooCommerce POS plugin I'm using, is being filtered on that ID when I scan a product.
But currently can't figure out, to what I have to change to make both the Inventory Barcode Field and the Variation Barcode field to be saved properly.
As well as I'd like to add the variation field below the variation SKU field, but can't directly find the proper hook to do this.
Thanks in advance for further information.
In your last hooked function you have a missing argument, which is a similar to $loop argument in your 3rd function. So I have made little changes in your code:
// Add product Barcode custom field
add_action('woocommerce_product_options_sku','add_barcode_custom_field' );
function add_barcode_custom_field(){
woocommerce_wp_text_input( array(
'id' => '_barcode',
'label' => __('Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('This is the Scan barcode field for this product.','woocommerce')
) );
}
// Save product Barcode custom field
add_action( 'woocommerce_process_product_meta', 'save_barcode_custom_field', 10, 1 );
function save_barcode_custom_field( $post_id ){
if( isset($_POST['_barcode']) )
update_post_meta( $post_id, '_barcode', esc_attr( $_POST['_barcode'] ) );
}
// Add Variation Barcode custom field
add_action( 'woocommerce_variation_options_pricing', 'add_barcode_variation_custom_field', 10, 3 );
function add_barcode_variation_custom_field( $loop, $variation_data, $variation ){
$variation_barcode = get_post_meta($variation->ID,"_barcode", true );
if( ! $variation_barcode ) $variation_barcode = "";
woocommerce_wp_text_input( array(
'id' => '_barcode['.$loop.']',
'label' => __('Variation Barcode','woocommerce'),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __('This is the Scan barcode field for this variation.','woocommerce'),
'value' => get_post_meta($variation->ID,"_barcode", true ),
) );
}
// Save Variation Barcode custom field value
add_action( 'woocommerce_save_product_variation', 'save_barcode_variation_custom_field', 10, 2 );
function save_barcode_variation_custom_field( $variation_id, $i ){
if( isset($_POST['_barcode'][$i]) )
update_post_meta( $variation_id, '_barcode', sanitize_text_field($_POST['_barcode'][$i]) );
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce version 2.6+ and 3.0+

Output product custom field values in order email notification

I have a custom field for a Woocommerce's product, and I want to display it's value in order emails.
As I'm using custom product submission form, I added this code for custom field below to create a custom field:
<?php
WCVendors_Pro_Form_Helper::select( array(
'post_id' => $object_id,
'class' => 'select2',
'id' => 'wcv_custom_product_ingredients',
'label' => __( 'What time?', 'wcvendors-pro' ),
'placeholder' => __( 'Pick a time', 'wcvendors-pro' ),
'wrapper_start' => '<div class="all-100">',
'wrapper_end' => '</div>',
'desc_tip' => 'true',
'description' => __( 'Pick a time', 'wcvendors-pro' ),
'options' => array( '12:00 midnight' => __('12:00 midnight', 'wcv_custom_product_ingredients'), '12:15 midnight'=> __('12:15 midnight', 'wcv_custom_product_ingredients') )
) );
?>
I also tried adding code below to functions.php, but this only displays "What time?" without value in order emails...
add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email');
function wcv_ingredients_email() {
$output = get_post_meta( get_the_ID(), 'wcv_custom_product_ingredients', true );
echo 'What time? ' . $output . '<br>';
}
What could be the issue?
You are using the correct hook, but you just forgot the hook arguments in the hooked function.
Also as you are targeting a product custom field value, and as in an order you can have many items (products), the code below will display this value for each order item.
Try the code below, it works now:
// Tested on WooCommerce version 2.6.x and 3+ — For simple products only.
add_action('woocommerce_email_after_order_table', 'wcv_ingredients_email', 10, 4);
function wcv_ingredients_email( $order, $sent_to_admin, $plain_text, $email ){
foreach($order->get_items() as $item_values){
// Get the product ID for simple products (not variable ones)
$product_id = $item_values['product_id'];
$output = get_post_meta( $product_id, 'wcv_custom_product_ingredients', true );
echo 'What time? ' . $output . '<br>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Adding programmatically a custom settings tab to admin product data in WooCommerce

I want to programmatically add a settings tab to Product data metabox like this:
The "Verzendkosten" tab was added with firebug (which means "Shipping costs).
How would I programmatically add the "Verzendkosten" custom tab in woocommerce edit-product pages settings?
(and How would I be abled to populate it with data?)
Updated on November 2017:
Corrected some mistakes, cleaned and added available options
Added 'Usage' and 'naming conventions' for custom fields slugs, at the end.
1) You create a custom tab in the custom post type Metabox (Here for "product"), 2) then you can add fields to populate this tab, with different kind of fields (You will find one of each type, so this is a very complete example).
And finally you will find a function that save the data when submitted.
Here it is what you will get visually (for the 6 different custom fields types):
Here is the related code:
// Step 1 - Adding a custom tab to the Products Metabox
add_filter( 'woocommerce_product_data_tabs', 'add_shipping_costs_product_data_tab', 99 , 1 );
function add_shipping_costs_product_data_tab( $product_data_tabs ) {
$product_data_tabs['shipping-costs'] = array(
'label' => __( 'Shipping costs', 'my_theme_domain' ), // translatable
'target' => 'shipping_costs_product_data', // translatable
);
return $product_data_tabs;
}
// Step 2 - Adding and POPULATING (with data) custom fields in custom tab for Product Metabox
add_action( 'woocommerce_product_data_panels', 'add_shipping_costs_product_data_fields' );
function add_shipping_costs_product_data_fields() {
global $post;
$post_id = $post->ID;
echo '<div id="shipping_costs_product_data" class="panel woocommerce_options_panel">';
## THE 6 DIFFERENT FIELD TYPES
# 1. Text input field
woocommerce_wp_text_input( array(
'id' => '_input_text',
// 'name' => '_input_text', // (optional) for different ID attribute than name attribute
// 'class' => 'some-class', // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'placeholder' => __( 'Enter some data', 'theme_domain' ), // (optional)
'label' => __( 'input text Label', 'theme_domain' ), // (optional)
'description' => __( 'input text Description', 'theme_domain' ), // (optional)
'desc_tip' => true, // (optional) To show the description as a tip
// 'data_type' => '', // (optional formatting options) can be 'price', 'decimal', 'stock' or 'url'
// 'type' => '', // (optional additional custom attribute)
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 2. Textarea input field
woocommerce_wp_textarea_input( array(
'id' => '_input_textarea',
// 'name' => 'input_textarea', // (optional) for different ID attribute than name attribute
'class' => 'widefat', // (optional)
// 'style' => '' // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'placeholder' => __( 'Enter some data', 'theme_domain' ), // (optional)
'label' => __( 'input textarea Label', 'theme_domain' ),
'description' => __( 'input textarea Description', 'theme_domain' ),
'desc_tip' => true, // (optional) To show the description as a tip
// 'rows' => 2, // (optional) defining number of rows
// 'cols' => 20, // (optional) defining number of columns
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 3. Checkbox field
woocommerce_wp_checkbox( array(
'id' => '_input_checkbox',
// 'name' => 'input_checkbox', // (optional) for different ID attribute than name attribute
// 'class' => 'some-class', // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'label' => __( 'input checkbox Label', 'theme_domain' ),
'description' => __( 'input checkbox Description', 'theme_domain' ),
'desc_tip' => true, // (optional) To show the description as a tip
// 'cbvalue' => 'yes', // to make it selected by default
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 4. Radio Buttons field
woocommerce_wp_radio( array(
'id' => '_input_radio',
// 'name' => 'input_radio', // (optional) for different ID attribute than name attribute
// 'class' => 'some-class', // (optional)
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'label' => __(' ', 'my_theme_domain'),
'description' => __( 'input Radio Description', 'my_theme_domain' ),
'desc_tip' => true,
'options' => array(
'option_value_1' => __('Displayed option 1'),
'option_value_2' => __('Displayed option 2'),
'option_value_3' => __('Displayed option 3'),
),
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 5. Select field
woocommerce_wp_select( array(
'id' => '_select_field',
// 'name' => '_select_field', // (optional) for different ID attribute than name attribute
// 'wrapper_class' => 'show_if_simple', // (optional) example here for simple products type only
'label' => __(' ', 'my_theme_domain'),
'description' => __( 'input Radio Description', 'my_theme_domain' ),
'desc_tip' => true,
'options' => array(
'' => __('Chose an option'), // Default empty value
'option_value_1' => __('Displayed option 1'),
'option_value_2' => __('Displayed option 2'),
'option_value_3' => __('Displayed option 3')
),
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
// 6. Hidden input field
woocommerce_wp_hidden_input( array(
'id' => '_hidden_input',
// 'name' => '_hidden_input', // (optional) for different ID attribute than name attribute
'class' => 'some_class',
// 'value' => $value, // (optional) for a static value (can be conditionally set for $value variable)
) );
echo '</div>';
}
// Step 3 - Saving custom fields data of custom products tab metabox
add_action( 'woocommerce_process_product_meta', 'shipping_costs_process_product_meta_fields_save' );
function shipping_costs_process_product_meta_fields_save( $post_id ){
// save the text field data
if( isset( $_POST['_input_text'] ) )
update_post_meta( $post_id, '_input_text', esc_attr( $_POST['_input_text'] ) );
// save the textarea field data
if( isset( $_POST['_input_textarea'] ) )
update_post_meta( $post_id, '_input_textarea', esc_attr( $_POST['_input_textarea'] ) );
// save the checkbox field data
if( isset( $_POST['_input_checkbox'] ) )
update_post_meta( $post_id, '_input_checkbox', esc_attr( $_POST['_input_checkbox'] ) );
// save the radio button field data
if( isset( $_POST['_input_radio'] ) )
update_post_meta( $post_id, '_input_radio', esc_attr( $_POST['_input_radio'] ) );
// save the selector field data
if( isset( $_POST['_select_field'] ) )
update_post_meta( $post_id, '_select_field', esc_attr( $_POST['_select_field'] ) );
// save the hidden input data
if( isset( $_POST['_hidden_input'] ) )
update_post_meta( $post_id, '_hidden_input', esc_attr( $_POST['_hidden_input'] ) );
}
Naturally this goes on function.php file of your active child theme (or theme) or in any plugin file.
You have to use the same custom field ID (slug names) in Step 2 and 3.
This code is tested and fully functional
You can add custom options with ANY DATA, using custom code, custom variables, or any kind of functions in Step 2.
Usage
To get or retrieve the data you will use get_post_meta() function for a defined Post ID:
$custom_field_data = get_post_meta( $post_id, '_custom_field_slug', true );
Where:
$post_id is the current post ID (from product, order, coupon… post-types).
custom_field_slug is the ID (the slug) of your custom field.
true or false: Whether to return a single value (data string or arrays)
It's the same process each kind of fields
Advice - Custom field slug names (Custom field ID)
If you don't use an underscore character ( _slug_name ) at the beginning of the slug names of your custom fields, they will appear and be accessible to authorized users in the custom fields Metabox, after submitting the data (Update button).
See this screen shot (here we get input_text custom field slug):
References:
Add tab which is visible only in admin side of product in woocommerce
Package WooCommerce > Admin > Functions
WooCommerce Multi Select for Single Product Field
WP Code Reference - get_post_meta()
WP Code Reference - update_post_meta()

Categories