In Woocommerce I use some custom fields for product specifications, and save the specifications in the post_meta.
I'm trying to make an if loop to write down in the post_meta another product specification.
The code I now use is:
add_action( 'woocommerce_product_options_general_product_data', 'BTW_field' );
function BTW_field() {
woocommerce_wp_radio(
array(
'id' => '_BTW',
'default' => '21% BTW',
'required' => true,
'options' => array(
'Prijs is incl. 21% BTW' => '21% BTW',
'Margeproduct' => 'Marge Product',
)
)
);
}
add_action( 'woocommerce_process_product_meta', 'BTW_save' );
function BTW_save( $post_id ){
$BTW = $_POST['_BTW'];
if( !empty( $BTW ) )
update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
}
An now I try to rewrite the BTW_save function so it will save another post_meta.
function BTW_save( $post_id ){
$BTW = $_POST['_BTW'];
if( !empty( $BTW ) ){
update_post_meta( $post_id, '_BTW', esc_attr( $BTW ) );
}
if ($BTW == "Margeproduct (vrijgesteld van BTW)"){
$BTW2 = "Margeproduct*"
} else {
$BTW2 = "21%"
}
update_post_meta( $post_id, '_BTW_NAME', esc_attr( $BTW2 ) );
}
I don't know how I can check if $BTW is equal to the post_meta _BTW and how I can rewrite it so $BTW2 will also save in the post meta as _BTW_NAME.
Updated: As you are setting 2 different values, it could be better to use a select field instead.
Also I have make some changes in your code regarding correct variable naming and field keys naming (You should be able to rename them easily keeping in mind that lowercase and underscores are recommended).
Here is the code:
add_action( 'woocommerce_product_options_general_product_data', 'add_btw_field' );
function add_btw_field() {
global $post;
// Get the selected value
$value = get_post_meta( $post->ID, '_btw', true );
if( empty( $value ) ) $value = 'btw'; // Default value
woocommerce_wp_select( array(
'id' => 'btw_select',
'label' => __( 'BTW-prijsopties', 'woocommerce' ),
'options' => array(
'btw' => __( '21% BTW', 'woocommerce' ),
'marge' => __( 'Marge Product', 'woocommerce' ),
),
'value' => $value, // Displaying the selected value
) );
}
add_action( 'woocommerce_process_product_meta', 'save_btw_field' );
function save_btw_field( $post_id ){
if( empty( $_POST['btw_select'] ) ) return; // exit (in case of)
update_post_meta( $post_id, '_btw', esc_attr( $_POST['btw_select'] ) );
if ( $_POST['btw_select'] == 'btw' )
$label = __( 'BTW 21%', 'woocommerce' );
else
$label = __( 'Margeproduct (vrijgesteld van BTW)', 'woocommerce' );
update_post_meta( $post_id, '_btw_label', esc_attr( $label ) );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works. You will get something like that:
By default when creating or updating a product, both custom fields will be saved for 'btw' option as product meta data…
You will be able to get both product post_meta custom fields values using get_post_meta():
// Set HERE the product ID (or get it dynamically)
$product_id = 37;
$btw = get_post_meta( $product_id, '_btw', true ); // Values can be 'yes' or 'no'
$btw_label = get_post_meta( $product_id, '_btw_label', true );
// Output (testing):
echo $btw_label;
Related
We managed to put a custom field for variation products following Remi Corson guide here
At this point, we are able to show the custom text field in the single product page when users select the variation, but this is not enough in the purchase process since we need to:
A) Display this text in Cart and Checkout
B) Save this information so it shows in Thank You Page, Emails and Admin Order Edit Page
Something similar to Save and display product custom meta on WooCommerce orders and emails, but with product variations instead of simple products.
This is the code we added to our functions.php to add the custom field to the product variations
// 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 );
/**
* Create new fields for variations
*
*/
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 )
)
);
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field[' . $variation->ID . ']',
'value' => 'hidden_value'
)
);
}
/**
* Save new fields for variations
*
*/
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 ) );
}
// Hidden field
$hidden = $_POST['_hidden_field'][ $post_id ];
if( ! empty( $hidden ) ) {
update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
}
}
// Add New Variation Settings
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
/**
* Add custom fields for variations
*
*/
function load_variation_settings_fields( $variations ) {
// duplicate the line for each field
$variations['text_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );
return $variations;
}
So the goal here is how can we show this custom field for each variation in the Cart and Checkout below the items (Something like the image below - Look at the Shipping delay notice)
And to save that custom field info that each variation has to the Thank You Page, Emails and Order Page (We did it for simple products with this code, but this doesn't work for variable ones)
// Save and display "Custom Field for Simple Products" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item', 10, 4 );
function action_wc_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
// Get the Custom Field
$value = $values['data']->get_meta( 'custom_field_for_simple_products' );
if( ! empty( $value ) ) {
// Save it and display it
$item->update_meta_data( __( 'Custom Fields', 'woocommerce' ), $value );
}
}
Please help!!
Updated
There are some mistakes in your code… The following revisited code will solve your issue:
// Display Variation custom fields (admin)
add_action( 'woocommerce_product_after_variable_attributes', 'display_variation_setting_custom_fields', 10, 3 );
function display_variation_setting_custom_fields( $loop, $variation_data, $variation ) {
echo '<div>';
woocommerce_wp_text_input( array( // Text Field
'id' => "_text_field[$loop]",
'label' => __("My Text Field", "woocommerce"),
'placeholder' => "http://",
'desc_tip' => true,
'description' => __("Enter the custom value here.", "woocommerce"),
'wrapper_class' => 'form-row form-row-full',
'value' => get_post_meta( $variation->ID, '_text_field', true ),
) );
woocommerce_wp_hidden_input( array( // Hidden field
'id' => "_hidden_field[$loop]",
'value' => 'hidden_value',
) );
echo '</div>';
}
// Save Variation custom fields
add_action( 'woocommerce_save_product_variation', 'save_variation_custom_fields', 10, 2 );
function save_variation_custom_fields( $variation_id, $i ) {
// Save Text Field
if( isset( $_POST['_text_field'][$i] ) && ! empty( $_POST['_text_field'][$i] ) )
update_post_meta( $variation_id, '_text_field', sanitize_text_field( $_POST['_text_field'][$i] ) );
// Save Hidden Field
if( isset( $_POST['_hidden_field'][$i] ) && ! empty( $_POST['_hidden_field'][$i] ) )
update_post_meta( $variation_id, '_hidden_field', esc_attr( $_POST['_hidden_field'][$i] ) );
}
// Include our variation custom field
add_filter( 'woocommerce_available_variation', 'include_variation_custom_field', 10, 3) ;
function include_variation_custom_field( $data, $product, $variation ) {
$data['text_field'] = $variation->get_meta( '_text_field' );
return $data;
}
// Save and display "Custom Field for Simple Products" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item_2', 10, 4 );
function action_wc_checkout_create_order_line_item_2( $item, $cart_item_key, $values, $order ) {
// Get the Custom Field
$value = $values['data']->get_meta( '_text_field' );
if( ! empty( $value ) ) {
// Save it and display it
$item->update_meta_data( __( 'Custom Field', 'woocommerce' ), $value );
}
}
Code goes in functions.php file of your active child theme (or active theme) . Tested and works.
Related: Save and display product custom meta on WooCommerce orders and emails
Somme screenshots
On admin product variations settings:
On order received page (thankyou):
On admin edit orders pages:
I am trying to figure out how to modify the singe product options so the product admin can pick from the drop down list the condition of product, i.e. new/ used.
Below is a code that allows product admin to enter the condition of product manually.
// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input( array( // Text Field type
'id' => '_Stan',
'label' => __( 'Stan', 'woocommerce' ),
'placeholder' => 'i.e: nowa; uzywana...',
'desc_tip' => 'true',
'description' => __( 'Podaj stan plyty.', 'woocommerce' )
) );
echo '</div>'; // Closing </div> tag HERE
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$Stan_field = $_POST['_Stan'];
if( !empty( $Stan_field ) )
update_post_meta( $post_id, '_Stan', esc_attr( $Stan_field ) );
}
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 45);
function woo_display_custom_general_fields_values() {
global $product;
echo '<p class="custom-Stan">Stan: ' . get_post_meta( $product->id, '_Stan', true ) . '</p>';
}
There is some errors and mistakes so I have revisited a little bit your code. Now you will have to replace woocommerce_wp_text_input() by woocommerce_wp_select() to get a select field instead, this way:
// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
echo '<div class="options_group">';
woocommerce_wp_select( array( // Text Field type
'id' => '_Stan',
'label' => __( 'Stan', 'woocommerce' ),
'description' => __( 'Podaj stan plyty.', 'woocommerce' ),
'desc_tip' => true,
'options' => array(
'' => __( 'Select product condition', 'woocommerce' ),
'Nowa' => __('Nowa', 'woocommerce' ),
'Uzywana' => __('Uzywana', 'woocommerce' ),
)
) );
echo '</div>';
}
// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields', 30, 1 );
function woo_save_custom_general_fields( $post_id ){
// Saving "Conditions" field key/value
$posted_field_value = $_POST['_Stan'];
if( ! empty( $posted_field_value ) )
update_post_meta( $post_id, '_Stan', esc_attr( $posted_field_value ) );
}
// Display In front end
add_action( 'woocommerce_product_meta_start', 'woo_display_custom_general_fields_values', 50 );
function woo_display_custom_general_fields_values() {
global $product;
// compatibility with WC +3
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo '<span class="stan">Stan: ' . get_post_meta( $product_id, '_Stan', true ) . '</span>';
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
Is better to avoid capitals in meta keys and they should start with an underscore.
In woocommerce I am using some code to add a Metabox with a custom field in product edit pages.
How can I display the value of this custom field under short description in single product pages?
Here is my code:
add_action ('add_meta_boxes','add_info_meta_box');
function add_info_meta_box()
{
add_meta_box('new_meta', 'info','info_meta_fields_output','product', 'side');
}
function info_meta_fields_output($post)
{
$new_meta = get_post_meta($post->ID,'_new_meta',true);
echo ('<label for="new_meta"> Custom Text </label>');
echo ('<input type="text" id="new_meta" name="new_meta" value="'.esc_attr($new_meta).'"/>');
}
add_action('save_post','save_info_meta_box');
function save_info_meta_box($post_id)
{
$new_meta=sanitize_text_field($_POST['new_meta']);
update_post_meta ($post_id,'_new_meta',$new_meta);
}
// Displaying the value on single product pages
function meta_product($product_id) {
$new_meta2 = get_post_meta(get_the_ID(),'_new_meta', true);
echo ('<p id="value-on-single-product">' . $new_meta2 . '</p>');
}
add_action('woocommerce_single_product_summary', 'meta_product',30);
But it doesn't display the custom field value.
Updated (added a 2nd custom field as asked in comments)
You should try the following, that will set your custom field in product general tab Metabox and will display this custom field value under product short description:
// Add the custom field
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_field_to_general_product_metabox' );
function add_custom_field_to_general_product_metabox() {
global $post;
// Get the selected value
$value = get_post_meta( $post->ID, '_new_meta', true );
if( empty( $value ) ) $value = ''; // Default value
woocommerce_wp_text_input( array(
'id' => 'new_meta',
'label' => __( 'Thông tin thêm', 'woocommerce' ),
'placeholder' => __( '', 'woocommerce' ),
'description' => __( '', 'woocommerce' ),
'value' => $value, // Displaying the selected value
) );
// Get the selected value
$value2 = get_post_meta( $post->ID, '_new_meta2', true );
if( empty( $value2 ) ) $value2 = ''; // Default value
woocommerce_wp_text_input( array(
'id' => 'new_meta2',
'label' => __( 'Thông tin thêm', 'woocommerce' ),
'placeholder' => __( '', 'woocommerce' ),
'description' => __( '', 'woocommerce' ),
'value' => $value2, // Displaying the selected value
) );
}
// Save the custom field
add_action( 'woocommerce_process_product_meta', 'save_custom_field_to_general_product_metabox' );
function save_custom_field_to_general_product_metabox( $post_id ){
if( isset( $_POST['new_meta'] ) )
update_post_meta( $post_id, '_new_meta', esc_attr( $_POST['new_meta'] ) );
if( isset( $_POST['new_meta2'] ) )
update_post_meta( $post_id, '_new_meta2', esc_attr( $_POST['new_meta2'] ) );
}
// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_single_product_summary', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;
$custom_field = get_post_meta( $product->get_id(),'_new_meta', true );
if( ! empty( $custom_field ) )
echo '<p id="value-on-single-product">' . $custom_field . '</p>';
$custom_field2 = get_post_meta( $product->get_id(),'_new_meta2', true );
if( ! empty( $custom_field2 ) )
echo '<p id="value-on-single-product">' . $custom_field2 . '</p>';
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works.
I have one issue that dont know how to resolve myself, so need your help. I have inserted Custom Field into my WooCommerce site product page. I have used this code to show my custom field bellow "Add to Cart" page, and also bellow product into cart page.
// Display Product settings Fields
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
woocommerce_wp_text_input( array(
'id' => '_pd_number',
'label' => __( 'Delivery Time', 'woocommerce' ),
'placeholder' => 'Some text here',
'desc_tip' => 'true',
'description' => __( 'Enter some text', 'woocommerce' )
));
}
// Save Product settings Fields
add_action( 'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$pd_number = $_POST['_pd_number'];
if( !empty( $pd_number ) )
update_post_meta( $post_id, '_pd_number', esc_attr( $pd_number ) );
}
// Show text in Product Page
add_action( 'woocommerce_after_add_to_cart_button',
'add_cf_before_addtocart_in_single_products', 1, 0 );
function add_cf_before_addtocart_in_single_products()
{
global $product;
$pd_number = get_post_meta( $product->get_id(), '_pd_number', true );
if( !empty( $pd_number ) )
echo '<div class="pd-number">'. $pd_number .'</div><br>';
}
// Displaying the product custom field in the Cart items
add_filter( 'woocommerce_cart_item_name', 'add_cf_after_cart_item_name', 10,
3 );
function add_cf_after_cart_item_name( $name_html, $cart_item, $cart_item_key
)
{
$product_id = $cart_item['product_id'];
if( $cart_item['variation_id'] > 0 )
$product_id = $cart_item['variation_id'];
$pd_number = get_post_meta( $product_id, '_pd_number', true );;
if( !empty( $pd_number ) )
$name_html .= '<br><span class="pd-number">'.$pd_number .'</span>';
return $name_html;
}
but show only on my default site language Dansk. If choise English from top bar, custom field is not shown. I use WPML as language plugin. Any help ?
In this image, if you put the blank custom field in other languages then it does not appear in front end
I am trying to update WooCommerce product meta data using update_post_meta() function, but it does''t work.
Here is my code:
function woo_add_deal_general_fields_save( $post_id ){
$post_id = (int)$post_id; // tried to convert into integer
$woocommerce_textarea = $_POST['_deal_textarea'];
if( !empty( $woocommerce_textarea ) )
if ( get_post_meta($post_id, '_deal_textarea', FALSE ) ) {
$test= update_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );
} else {
add_post_meta($post_id, '_deal_textarea', $woocommerce_textarea );
}
var_dump($test);exit;
}
If I try it with a fixed product ID, it works:
$test= update_post_meta(70, '_deal_textarea', $woocommerce_textarea );
Why its not working with $post_id, (int)$post_id, & either get_the_ID();?
Here is the part of my code like function calls:
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_deal_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
$feature_product=get_post_meta(get_the_ID(), '_featured', true );
if($feature_product=='yes'){
echo '<div class="options_group">';
// Custom fields will be created here...
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_deal_textarea',
'label' => __( 'Deal Caption', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' )
)
);
echo '</div>';
}
}
Thanks
Here is your revisited tested and fully functional code, based on this answer:
// Inserting a Custom Admin Field in general tab products pages
add_action( 'woocommerce_product_options_general_product_data', 'add_deal_custom_general_product_field' );
function add_deal_custom_general_product_field() {
global $post;
$feature_product = get_post_meta( $post->ID, '_featured', true );
if( $feature_product == 'yes' ){
echo '<div class="options_group">';
woocommerce_wp_textarea_input( array(
'id' => '_deal_textarea',
'label' => __( 'Deal Caption', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the Deal Product Text value here. (will be shown on home page)', 'woocommerce' )
) );
echo '</div>';
}
}
// Saving the Custom Admin Field in general tab products pages when submitted
add_action( 'woocommerce_process_product_meta', 'save_deal_custom_general_product_field' );
function save_deal_custom_general_product_field( $post_id ){
$wc_field = $_POST['_deal_textarea'];
$feature_product = get_post_meta( $post_id, '_featured', true );
if( !empty($wc_field) && $feature_product == 'yes')
update_post_meta( $post_id, '_deal_textarea', esc_attr( $wc_field ) );
}
The 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
I don't know where is the bug, but i simplified the code a bit. Please try it:
function woo_add_deal_general_fields_save( $post_id ){
$woocommerce_textarea = $_POST['_deal_textarea'];
if( !empty( $woocommerce_textarea ) ) {
$test = update_post_meta( $post_id, '_deal_textarea', $woocommerce_textarea );
var_dump( $test ); exit;
}
}