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+
Related
I recently created a custom field in functions.php to display a custom meta field in the Inventory tab for each product called barcode. What I now want to do is display the data onto my single product template using a PHP call. Here's first of all, the function to create the barcode data (which works):
function add_barcode(){
woocommerce_wp_text_input(
array(
'id' => '_barcode',
'label' => __( 'Barcode', 'your-plugin' ),
'placeholder' => 'Scan Barcode',
'desc_tip' => 'true',
'description' => __( "Scan the product's barcode.", "your-plugin" )
)
);
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');
function add_barcode_save( $product ){
if( isset( $_POST['_barcode'] ) ) {
$product->update_meta_data( '_barcode', sanitize_text_field( $_POST['_barcode'] ) );
} else {
$product->delete_meta_data( '_barcode' );
}
}
add_action( 'woocommerce_admin_process_product_object', 'add_barcode_save' );
Here's the PHP I'm trying to generate that data onto the page:
$product = wc_get_product();
echo '<strong>Barcode: </strong>' .$product->get_attribute( 'barcode' );
All I get here is a blank area after the HTML portion. So what function will allow me to display the barcode text?
I am building my first woocommerce site, Im learning how to create custom fields for products. I would like to create a text field in general tab, save that field and display on the front end to customers.
Here is the code I used to display the text field in the general tab of products.
function prefix_add_text_input() {
$args = array(
'label' =>__('Serial Number', 'woocommerce'), // Text in the label in the editor.
'placeholder' => '', // Give examples or suggestions as placeholder
'class' => '',
'style' => '',
'wrapper_class' => '',
'value' => '', // if empty, retrieved from post_meta
'id' => 'serial_number', // required, will be used as meta_key
'name' => '', // name will be set automatically from id if empty
'type' => '',
'desc_tip' => 'true',
'data_type' => '',
'custom_attributes' => '', // array of attributes you want to pass
'description' => 'Enter the serial number on your rifle here'
);
woocommerce_wp_text_input( $args );
}
How do I get the field to save, and display on the front end. Ideally display in the tabs with product description?
Here below you will find the way to save your product custom field value and display it in the product description tab section:
// Add a Custom product Admin Field
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_product_general_field' );
function add_custom_product_general_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array(
'id' => '_serial_number', // required, will be used as meta_key
'label' =>__('Serial Number', 'woocommerce'), // Text in the label in the editor.
'desc_tip' => 'true',
'description' => __('Enter the serial number on your rifle here', 'woocommerce')
) );
echo '</div>';
}
// Save the field value
add_action( 'woocommerce_admin_process_product_object', 'save_custom_product_general_field' );
function save_custom_product_general_field( $product ){
if( isset($_POST['_serial_number']) )
$product->update_meta_data( '_serial_number', sanitize_text_field( $_POST['_serial_number'] ) );
}
// Display the custom field value below product description tab
add_filter( 'the_content', 'display_product_serial_number' );
function display_product_serial_number( $content ) {
// Only for single product pages
if ( is_product() ) {
global $product;
if( $value = $product->get_meta( '_serial_number' ) ) {
$content .= '<p><strong>' . __("Serial number:", "woocommerce") . '<strong> ' . $value . '<p>';
}
}
return $content;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I followed this tutorial for adding custom fields into WooCommerce variations.
http://www.remicorson.com/woocommerce-custom-fields-for-variations/
Everything worked great...
However the method for inserting the field into the dashboard (product edit) area doesn't mention how to change the order of the field as seen in the dashboard.
.
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field', true )
)
);
}
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
}
Any help would be much appreciated!
Using woocommerce_variation_options_pricing will put it after the price (it may need some CSS). It's the closest hook I found after price input.
Using woocommerce_variation_options will put it before price input
Following called hook in template is woocommerce_variation_options_inventory
In those cases, you can check yourself what is possible by:
finding the related Woocommerce template (in plugin directory)
studying the code to find do_action (or apply_filter for variables) to see "where and when you can do/update stuff". In your case wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-variation-admin.php:159
sometimes your are lucky (merciful devs), sometimes less :)
So I've got the following function to add a barcode field in the Product Inventory tab. However this field is being added after all the other content, and I would like to have this before the SKU code.
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')
));
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');
Is there anyway to place the function/field before the SKU aka before the actual hook, like something in terms of woocommerce_before_product_options_inventory_product_data?
Thanks for any suggestions in advance.
IS NOT POSSIBLE TO PLACE ANY CUSTOM FIELD BEFORE THE SKU
You can take a look to html-product-data-inventory.php source code file which displays product inventory fields.
But you can display your 'Barcode' custom field just after the SKU field (for example)
For that you have to hook your custom function in woocommerce_product_options_sku action hook instead. There is also some missing things in your code to display a saved value.
And finally you need another function to save that value when product is saved or updated.
Here is that complete code:
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' => __('Scan barcode.','woocommerce')
) );
}
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'] ) );
}
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+
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.