I am adding and saving a custom field to my variable products using the following functions:
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields' );
function wc_add_custom_fields() {
woocommerce_wp_text_input(
array(
'id' => '_custom_product_pricekg_field',
'label' => __( 'Price per kg', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => false,
'description' => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save' );
function wc_custom_fields_save($post_id)
{
$woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];
if (!empty($woocommerce_custom_product_pricekg_field))
update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
}
I would like to replace the variable price range ($XX-XX) on all archive and product pages with this custom field. I don't know how to do this. The below snippet replaces the variable price range with the default variable, perhaps this could be modified to show my custom field value?
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
The following code replaces the price range with the value from the custom field, if you have additional questions, be welcome.
// Add new field
function wc_add_custom_fields() {
woocommerce_wp_text_input(
array(
'id' => '_custom_product_pricekg_field',
'label' => __( 'Price per kg', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => false,
'description' => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields', 10, 0 );
// Save
function wc_custom_fields_save($post_id) {
$woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];
if ( !empty( $woocommerce_custom_product_pricekg_field ) ) {
update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
}
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save', 10, 1 );
// Variation price
function custom_variation_price( $price, $product ) {
// Get product id
$product_id = $product->get_id();
// Get custom value
$new_price = get_post_meta( $product_id, '_custom_product_pricekg_field', true);
// NOT empty
if ( ! empty( $new_price ) ) {
$price = $new_price;
}
return $price;
}
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
Related
Based on Enhanced WooCommerce Custom Fields for Variations answer code for adding a custom field to a product variation which works.
I have added additional custom fields, 6 at all. When I update the product, the data does not save and does not display on the front end either.
What have I done incorrectly when adding the additional custom fields?
My code:
// Add a custom field to variation settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_model[' . $variation->ID . ']',
'label' => __( 'model', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_model', true )
)
);
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_wattage[' . $variation->ID . ']',
'label' => __( 'wattage', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_wattage', true )
)
);
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_lumen[' . $variation->ID . ']',
'label' => __( 'lumen', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_lumen', true )
)
);
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_material[' . $variation->ID . ']',
'label' => __( 'material', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_material', true )
)
);
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_dimension[' . $variation->ID . ']',
'label' => __( 'dimension', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_dimension', true )
)
);
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_year[' . $variation->ID . ']',
'label' => __( 'year', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_year', true )
)
);
}
// Save custom field value from variation settings
add_action( 'woocommerce_admin_process_variation_object', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation, $loop ) {
if( isset($_POST['_model'][$loop]) ) {
$variation->update_meta_data( '_model', sanitize_text_field($_POST['_model'][$loop]) );
}
if( isset($_POST['_wattage'][$loop]) ) {
$variation->update_meta_data( '_wattage', sanitize_text_field($_POST['_wattage'][$loop]) );
}
if( isset($_POST['_lumen'][$loop]) ) {
$variation->update_meta_data( '_lumen', sanitize_text_field($_POST['_lumen'][$loop]) );
}
if( isset($_POST['_material'][$loop]) ) {
$variation->update_meta_data( '_material', sanitize_text_field($_POST['_material'][$loop]) );
}
if( isset($_POST['_dimension'][$loop]) ) {
$variation->update_meta_data( '_dimension', sanitize_text_field($_POST['_dimension'][$loop]) );
}
if( isset($_POST['_year'][$loop]) ) {
$variation->update_meta_data( '_year', sanitize_text_field($_POST['_year'][$loop]) );
}
}
// Add variation custom field to single variable product form
add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_to_variable_form', 10, 3 );
function add_variation_custom_field_to_variable_form( $variation_data, $product, $variation ) {
$variation_data['model'] = $variation->get_meta('_model');
$variation_data['wattage'] = $variation->get_meta('_wattage');
$variation_data['lumen'] = $variation->get_meta('_lumen');
$variation_data['material'] = $variation->get_meta('_material');
$variation_data['dimension'] = $variation->get_meta('_dimension');
$variation_data['year'] = $variation->get_meta('_year');
return $variation_data;
}
add_action( 'woocommerce_product_additional_information', 'add_html_container_to_display_selected_variation_custom_field' );
function add_html_container_to_display_selected_variation_custom_field( $product ){
echo '<div class="custom_variation-text-field"></div>';
}
// Display selected variation custom field value to product the tab
add_action( 'woocommerce_after_variations_form', 'display_selected_variation_custom_field_js' );
function display_selected_variation_custom_field_js(){
?>
<script type="text/javascript">
(function($){
$('form.cart').on('show_variation', function(event, data) {
$('.custom_variation-text-field').text(data.text_field);
}).on('hide_variation', function(event) {
$('.custom_variation-text-field').text('');
});
})(jQuery);
</script>
<?php
}
To make it save the data, I have made some changes in the 1st function (2nd one stay unchanged):
// Add a custom field to variation settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => '_model[' . $loop . ']',
'label' => __( 'model', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'This is the description text...', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_model', true )
) );
woocommerce_wp_text_input(
array(
'id' => '_wattage[' . $loop . ']',
'label' => __( 'wattage', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'This is the description text...', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_wattage', true )
) );
woocommerce_wp_text_input( array(
'id' => '_lumen[' . $loop . ']',
'label' => __( 'lumen', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'This is the description text...', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_lumen', true )
) );
woocommerce_wp_text_input( array(
'id' => '_material[' . $loop . ']',
'label' => __( 'material', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'This is the description text...', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_material', true )
) );
woocommerce_wp_text_input( array(
'id' => '_dimension[' . $loop . ']',
'label' => __( 'dimension', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'This is the description text...', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_dimension', true )
) );
woocommerce_wp_text_input( array(
'id' => '_year[' . $loop . ']',
'label' => __( 'year', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_year', true )
) );
}
// Save custom field value from variation settings
add_action( 'woocommerce_admin_process_variation_object', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation, $loop ) {
if( isset($_POST['_model'][$loop]) ) {
$variation->update_meta_data( '_model', sanitize_text_field($_POST['_model'][$loop]) );
}
if( isset($_POST['_wattage'][$loop]) ) {
$variation->update_meta_data( '_wattage', sanitize_text_field($_POST['_wattage'][$loop]) );
}
if( isset($_POST['_lumen'][$loop]) ) {
$variation->update_meta_data( '_lumen', sanitize_text_field($_POST['_lumen'][$loop]) );
}
if( isset($_POST['_material'][$loop]) ) {
$variation->update_meta_data( '_material', sanitize_text_field($_POST['_material'][$loop]) );
}
if( isset($_POST['_dimension'][$loop]) ) {
$variation->update_meta_data( '_dimension', sanitize_text_field($_POST['_dimension'][$loop]) );
}
if( isset($_POST['_year'][$loop]) ) {
$variation->update_meta_data( '_year', sanitize_text_field($_POST['_year'][$loop]) );
}
}
It should better work to custom fields data to database and display the saved values in admin.
Now the frontend display part is wrong (your last 2 functions).
You need first to think about how you want to display that multiple custom fields, how should be the html structure and the labels related to each custom field. So edit your question as I can't guess that for you.
Here is a working example with all your custom fields, to display the data in frontend single product pages, for the selected variation:
// Add variation custom field to single variable product form
add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_to_variable_form', 10, 3 );
function add_variation_custom_field_to_variable_form( $variation_data, $product, $variation ) {
$variation_data['model'] = $variation->get_meta('_model');
$variation_data['wattage'] = $variation->get_meta('_wattage');
$variation_data['lumen'] = $variation->get_meta('_lumen');
$variation_data['material'] = $variation->get_meta('_material');
$variation_data['dimension'] = $variation->get_meta('_dimension');
$variation_data['year'] = $variation->get_meta('_year');
return $variation_data;
}
add_action( 'woocommerce_product_additional_information', 'add_html_container_to_display_selected_variation_custom_field' );
function add_html_container_to_display_selected_variation_custom_field( $product ){
echo '<div class="custom_variation-text-field">aaa</div>';
}
// Display selected variation custom field value to product the tab
add_action( 'woocommerce_after_variations_form', 'display_selected_variation_custom_field_js' );
function display_selected_variation_custom_field_js(){
?>
<script type="text/javascript">
(function($){
var a = '.custom_variation-text-field', b = $(a).html();
$('form.cart').on('show_variation', function(event, data) {
outputHtml = '';
if( data.model ) {
outputHtml += '<span><strong><?php _e("Model"); ?><strong>: '+data.model+'<span><br>';
}
if( data.wattage ) {
outputHtml += '<span><strong><?php _e("Wattage"); ?><strong>: '+data.wattage+'<span><br>';
}
if( data.lumen ) {
outputHtml += '<span><strong><?php _e("Lumen"); ?><strong>: '+data.lumen+'<span><br>';
}
if( data.material ) {
outputHtml += '<span><strong><?php _e("Material"); ?><strong>: '+data.material+'<span><br>';
}
if( data.dimension ) {
outputHtml += '<span><strong><?php _e("Dimension"); ?><strong>: '+data.dimension+'<span><br>';
}
if( data.year ) {
outputHtml += '<span><strong><?php _e("Year"); ?><strong>: '+data.year+'<span>';
}
if( outputHtml ) {
$(a).html(outputHtml);
}
}).on('hide_variation', function(event) {
$(a).html(b);
});
})(jQuery);
</script>
<?php
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I'm trying to create custom fields for variable products.
So far I have used the code from this github: https://gist.github.com/maddisondesigns/e7ee7eef7588bbba2f6d024a11e8875a
I have changed the code to my needs and currently have this in my functions.php:
/*
* Add our Custom Fields to variable products
*/
function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_variable_article_number[' . $variation->ID . ']',
'label' => __( 'Article number', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( 'Article number', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_variable_article_number', true ),
)
);
woocommerce_wp_text_input(
array(
'id' => '_variable_ean_code[' . $variation->ID . ']',
'label' => __( 'EAN code', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( 'EAN code', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_variable_ean_code', true ),
)
);
woocommerce_wp_text_input(
array(
'id' => '_variable_shelf_life[' . $variation->ID . ']',
'label' => __( 'Shelf life', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => true,
'description' => __( 'Shelf life', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_variable_shelf_life', true ),
)
);
echo '</div>';
}
// Variations tab
// add_action( 'woocommerce_variation_options', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After variation Enabled/Downloadable/Virtual/Manage Stock checkboxes
// add_action( 'woocommerce_variation_options_pricing', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Price fields
// add_action( 'woocommerce_variation_options_inventory', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Manage Stock fields
// add_action( 'woocommerce_variation_options_dimensions', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Weight/Dimension fields
// add_action( 'woocommerce_variation_options_tax', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Shipping/Tax Class fields
// add_action( 'woocommerce_variation_options_download', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After Download fields
add_action( 'woocommerce_product_after_variable_attributes', 'mytheme_woo_add_custom_variation_fields', 10, 3 ); // After all Variation fields
/*
* Save our variable product fields
*/
function mytheme_woo_add_custom_variation_fields_save( $post_id ) {
// Text Field
$woocommerce_text_field = $_POST['_variable_text_field'][ $post_id ];
update_post_meta( $post_id, '_variable_text_field', esc_attr( $woocommerce_text_field ) );
}
add_action( 'woocommerce_save_product_variation', 'mytheme_woo_add_custom_variation_fields_save', 10, 2 );
/*
* Display our example custom field above the summary on the Single Product Page
*/
function mytheme_display_woo_custom_fields() {
global $post;
$articleNumber = get_post_meta( $post->ID, '_variable_article_number', true );
$eanCode = get_post_meta( $post->ID, '_variable_ean_code', true );
$shelfLife = get_post_meta( $post->ID, '_variable_shelf_life', true );
if ( ! empty( $articleNumber ) ) {
echo '<div>Article number: ' . $articleNumber . '</div>';
}
if ( ! empty( $articleNumber ) ) {
echo '<div>EAN code: ' . $eanCode . '</div>';
}
if ( ! empty( $shelfLife ) ) {
echo '<div>Shelf life: ' . $shelfLife . '</div>';
}
}
add_action( 'woocommerce_single_product_summary', 'mytheme_display_woo_custom_fields', 15 );
The custom fields show correctly in the wordpress back-end and woocommerce template:
See here
But the problem is that custom fields don't update on changing the variation.
Thanks
If you are using WooCommerce 4, they have updated their product hook for variations.
Try using "woocommerce_update_product_variation".
I am having a problem trying to add a custom field called Discount_info to a simple product.
I have created a new tab called discount_info which shows up in the simple product view just fine. Problem is trying to add a custom number field to this tab. I'm using the code below which is causing a 500 error. Any ideas where i am going wrong?
// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_discount_info',
'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
// Create a custom text field
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_discount_info',
'label' => __( 'Discount %', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the % discount here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '1'
)
)
);
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta',
'woocom_save_general_proddata_custom_field' );
/** Hook callback function to save custom fields information */
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Number Field
$number_field = $_POST['_discount_info'];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_discount_info', esc_attr( $number_field ) );
}
}
First remove all related code, and try this instead:
// Add a custom product setting tab to edit product pages options FOR SIMPLE PRODUCTS only
add_filter( 'woocommerce_product_data_tabs', 'discount_new_product_data_tab', 50, 1 );
function discount_new_product_data_tab( $tabs ) {
$tabs['discount'] = array(
'label' => __( 'Discount', 'woocommerce' ),
'target' => 'discount_product_data', // <== to be used in the <div> class of the content
'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
// Add/display custom Fields in the custom product settings tab
add_action( 'woocommerce_product_data_panels', 'add_custom_fields_product_options_discount', 10 );
function add_custom_fields_product_options_discount() {
global $post;
echo '<div id="discount_product_data" class="panel woocommerce_options_panel">'; // <== Here we use the target attribute
woocommerce_wp_text_input( array(
'type' => 'number', // Add an input number Field
'id' => '_discount_info',
'label' => __( 'Percentage Discount', 'woocommerce' ),
'placeholder' => __( 'Enter the % discount.', 'woocommerce' ),
'description' => __( 'Explanations about the field info discount.', 'woocommerce' ),
'desc_tip' => 'true',
'custom_attributes' => array(
'step' => 'any',
'min' => '1'
),
) );
echo '</div>';
}
// Save the data value from the custom fields for simple products
add_action( 'woocommerce_process_product_meta_simple', 'save_custom_fields_product_options_discount', 50, 1 );
function save_custom_fields_product_options_discount( $post_id ) {
// Save Number Field value
$number_field = $_POST['_discount_info'];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_discount_info', esc_attr( $number_field ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
There aren't enough information to help know the exact problem, but it worth testing with a cleaner version of your code:
// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_discount_info', 'wc_general_product_data_custom_field' );
function wc_general_product_data_custom_field() {
// Number Field
woocommerce_wp_text_input( array(
'id' => '_discount_info',
'label' => __( 'Discount %', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the % discount here.', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'min' => '1',
'step' => '1',
),
) );
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'wc_save_general_proddata_custom_field' );
/** Hook callback function to save custom fields information */
function wc_save_general_proddata_custom_field( $post_id ) {
// Save Number Field
$number_field = isset( $_POST['_discount_info'] ) ? $_POST['_discount_info'] : '';
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_discount_info', $number_field );
}
}
The WordPress plugin WooCommerce supports Custom Fields.
Custom Fields can be toggled under Screen Options:
However, these Custom Fields do no get segregated for product variations. There is only one set of Custom Fields that applies to all product variations in the WooCommerce interface, and this is inappropriate because different product variations may have different product MPNs, for example, which is one of the Custom Fields I have set up for myself.
The closest off-site solution I found does not link up to custom fields. The code, shown below, shows how one can set up input fields in the Product Variations interface. How can I modify the solution below to link up the Product Variations interface the custom fields?
This question is distinct from questions concerned with Advanced Custom Fields, as I am not using that plugin. Furthermore, at least one other similar question on Stack Overflow but I'm not proficient with PHP and I don't comprehend it, nor do I know if it is relevant - but I will point out that solution has not been accepted by the OP and I don't want to necropost there.
/**
* Create new fields for variations
*
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_ean_code[' . $variation->ID . ']',
'label' => __( 'EAN-CODE', 'woocommerce' ),
'placeholder' => 'EAN-CODE',
'desc_tip' => 'true',
'description' => __( 'Enter your custom EAN code.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_ean_code', true )
)
);
woocommerce_wp_text_input(
array(
'id' => '_stock_amsterdam[' . $variation->ID . ']',
'label' => __( 'Nr. of Stock in Amsterdam', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter nr. of Stock in Amsterdam', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_stock_amsterdam', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
woocommerce_wp_text_input(
array(
'id' => '_stock_den_bosch[' . $variation->ID . ']',
'label' => __( 'Nr. of Stock in Den Bosch', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter nr. of Stock in Den Bosch', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_stock_den_bosch', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
woocommerce_wp_text_input(
array(
'id' => '_stock_alkmaar[' . $variation->ID . ']',
'label' => __( 'Nr. of Stock in Alkmaar', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter nr. of Stock in Alkmaar', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_stock_alkmaar', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}
/**
* Save new fields for variations
*
*/
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_ean_code'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_ean_code', esc_attr( $text_field ) );
}
$number_field = $_POST['_stock_amsterdam'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_stock_amsterdam', esc_attr( $number_field ) );
}
$number_field = $_POST['_stock_den_bosch'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_stock_den_bosch', esc_attr( $number_field ) );
}
$number_field = $_POST['_stock_alkmaar'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_stock_alkmaar', esc_attr( $number_field ) );
}
}
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
I've been trying to hook this function in one of the "Order Hooks" of the Woocommerce Checkout page:
add_action( 'woocommerce_checkout_before_order_review', 'add_box_conditional' );
function add_box_conditional ( $checkout ) {
woocommerce_form_field( 'test', array(
'type' => 'checkbox',
'class' => array('test form-row-wide'),
'label' => __('conditional test'),
'placeholder' => __(''),
), $checkout->get_value( 'test' ));
}
If i try to get the value of the custom box in any order hooks, the order info just hangs and stops loading. I've tried with another type of custom fields and the same happens.
Example
If I hook the function outside the order contents works perfectly. The custom check box will be used to add a fee (post validation), as it is a very important option for our shop I want it inside the order details, so it can have a strong focus. Is there a way to make the function work on these hooks, or should I put it anywhere and move it with a simple but not so clean CSS overwritte?
You can't just get the value like that $checkout->get_value( 'test' ));.
Hook woocommerce_checkout_create_order and get the value from $_POST there. Then add a custom fee to the order if the checkbox was checked.
Like this:
function add_box_conditional() {
woocommerce_form_field( 'test', array(
'type' => 'checkbox',
'class' => array( 'test form-row-wide' ),
'label' => __( 'conditional test' ),
'placeholder' => __( '' ),
) );
}
add_action( 'woocommerce_checkout_before_order_review', 'add_box_conditional' );
function edit_order( $order, $data ) {
if( ! isset( $_POST[ 'test' ] ) ) {
return;
}
$checkbox_value = filter_var( $_POST[ 'test' ], FILTER_SANITIZE_NUMBER_INT );
if( $checkbox_value ){
$fee = 20;
$item = new \WC_Order_Item_Fee();
$item->set_props( array(
'name' => __( 'Custom fee', 'textdomain' ),
'tax_class' => 0,
'total' => $fee,
'total_tax' => 0,
'order_id' => $order->get_id(),
) );
$item->save();
$order->add_item( $item );
$order->calculate_totals();
}
}
add_action( 'woocommerce_checkout_create_order', 'edit_order', 10, 2 );