I have a products with the following size parameters:
Length (Woocomerce standard)
Width (Woocomerce standard)
Height (Woocomerce standard)
Diameter
Thickness
Circuit
In the product edit page I have only Length, Width, Height (Woocomerce standard)
I would like to add my other sizes parameters
How can I add this extra sizes properly?
Is there any filter for this?
There is multiple ways…
1) Using Product attributes: (without any coding need):
You should create 3 new product attributes (one for each other missing dimension).
You should set each of them in each product with the correct options (displayed on products option) and values.
Advantage: Those other dimention attributes will be displayed on product pages.
Disadvantages:
Each attribute Needs to be set for each product and each value is going to be a term of this product attribute.
There are no specific existing WC_Product methods to get those custom product attributes like the default dimension ones. So is going to be more complicated to get them outside the product page.
2) Using custom setting fields:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_dimensions', 'add_product_options_other_dimensions');
function add_product_options_other_dimensions(){
global $product_object;
$product_id = method_exists( $product_object, 'get_id' ) ? $product_object->get_id() : $product_object->id;
echo '</div><div class="options_group">'; // New option group
woocommerce_wp_text_input( array(
'id' => '_diameter',
'label' => __( 'Diameter', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Diameter description help.', 'woocommerce' )
) );
woocommerce_wp_text_input( array(
'id' => '_thickness',
'label' => __( 'Thickness', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Thickness description help.', 'woocommerce' )
) );
woocommerce_wp_text_input( array(
'id' => '_circuit',
'label' => __( 'Circuit', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Circuit description help.', 'woocommerce' )
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_product_options_other_dimensions' );
function save_product_options_other_dimensions( $post_id ){
if( isset( $_POST['_diameter'] ) )
update_post_meta( $post_id, '_diameter', esc_attr( $_POST['_diameter'] ) );
if( isset( $_POST['_thickness'] ) )
update_post_meta( $post_id, '_thickness', esc_attr( $_POST['_thickness'] ) );
if( isset( $_POST['_circuit'] ) )
update_post_meta( $post_id, '_circuit', esc_attr( $_POST['_circuit'] ) );
}
// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );
function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){
$variation_diameter = get_post_meta($variation->ID,"_diameter", true );
if( ! $variation_diameter ) $variation_diameter = "";
$variation_thickness = get_post_meta($variation->ID,"_thickness", true );
if( ! $variation_thickness ) $variation_thickness = "";
$variation_circuit = get_post_meta($variation->ID,"_circuit", true );
if( ! $variation_circuit ) $variation_circuit = "";
echo '<p class="form-field dimensions_field">';
woocommerce_wp_text_input( array(
'id' => '_diameter' . '_' . $loop,
'label' => __( 'Diameter', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Diameter description help.', 'woocommerce' ),
'value' => $variation_diameter
) );
woocommerce_wp_text_input( array(
'id' => '_thickness' . '_' . $loop,
'label' => __( 'Thickness', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Thickness description help.', 'woocommerce' ),
'value' => $variation_thickness
) );
woocommerce_wp_text_input( array(
'id' => '_circuit' . '_' . $loop,
'label' => __( 'Circuit', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Circuit description help.', 'woocommerce' ),
'value' => $variation_circuit
) );
echo '</p>';
}
// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );
function save_variation_options_other_dimensions( $variation_id, $loop ){
$built_lenght = $_POST["_diameter_$loop"];
if( isset($built_lenght) )
update_post_meta( $variation_id, '_built_lenght', esc_attr($built_lenght) );
$built_width = $_POST["_thickness_$loop"];
if( isset($built_width) )
update_post_meta( $variation_id, '_built_width', esc_attr($built_width) );
$built_height = $_POST["_circuit_$loop"];
if( isset($built_height) )
update_post_meta( $variation_id, '_built_height', esc_attr($built_height) );
}
This code goes on function.php file of your active child theme (or theme).
You will get that for products:
And that for product variations (in variable products "variations settings):
Now to display and get those values, you will have to override the woocommerce template via your theme as explained in this documentation.
The file to copy and override via your theme is: single-product/product-attributes.php template.
You will have to add some code to it to display your custom additional dimension labels and values just after the displayed default dimentions.
To output the correct values you will use get_post_meta() function.
For example to display the diameter value you will use:
<?php echo get_post_meta( $product->get_id(), '_diameter', true ); ?>
You can use custom_fields or product attributes for this.
Related
I have some custom fields for my Woocommerce products inside my Inventory Tab using woocommerce_wp_text_input() function. I would like to add one more custom text field just for displaying a value for reference.
I want to lock the textfield by default so as not to be able to write something in it.
Is it possible?
Yes it is possible adding the as custom_attributes the readonly property in the field arguments array using:
'custom_attributes' => array('readonly' => 'readonly'),
So your code will be like:
add_action( 'woocommerce_product_options_stock_status', 'display_product_options_inventory_custom_fields', 20 );
function display_product_options_inventory_custom_fields() {
global $post;
echo '</div><div class="options_group">'; // New separated section
// Text field (conditionally readonly)
woocommerce_wp_text_input( array(
'id' => '_text_field_ro',
'type' => 'text',
'label' => __( 'Read only field', 'woocommerce' ),
'placeholder' => __( 'placeholder text', 'woocommerce' ),
'description' => __( 'Custom description: your explanations.', 'woocommerce' ),
'desc_tip' => true,
'custom_attributes' => $readonly, // Enabling read only
) );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Update: Adding a checkbox to enable the readonly fields:
add_action( 'woocommerce_product_options_stock_status', 'display_product_options_inventory_custom_fields', 20 );
function display_product_options_inventory_custom_fields() {
global $post;
echo '</div><div class="options_group">'; // New separated section
// Checkbox
woocommerce_wp_checkbox( array(
'id' => '_enable_readonly',
'label' => __( 'Enable readonly fields', 'woocommerce' ),
'description' => __( 'Enable some fields to be readonly', 'woocommerce' ),
'desc_tip' => true,
));
// Get the checkbox value
$checkbox = get_post_meta( $post->ID, '_enable_readonly', true );
// We set the field attribute "readonly" conditionally based on the checkbox
$readonly = empty($checkbox) ? '' : array('readonly' => 'readonly');
// Text field 1 (conditionally readonly)
woocommerce_wp_text_input( array(
'id' => '_text_field_ro1',
'type' => 'text',
'label' => __( 'Read only field 1', 'woocommerce' ),
'placeholder' => __( 'placeholder text 1', 'woocommerce' ),
'description' => __( 'Custom description 1: your explanations.', 'woocommerce' ),
'desc_tip' => true,
'custom_attributes' => $readonly, // Enabling read only
) );
// Text field 2 (conditionally readonly)
woocommerce_wp_text_input( array(
'id' => '_text_field_ro2',
'type' => 'text',
'label' => __( 'Read only field 2', 'woocommerce' ),
'placeholder' => __( 'placeholder text 2', 'woocommerce' ),
'description' => __( 'Custom description 2: your explanations.', 'woocommerce' ),
'desc_tip' => true,
'custom_attributes' => $readonly, // Enabling read only
) );
}
add_action( 'woocommerce_process_product_meta', 'save_product_custom_fields' );
function save_product_custom_fields( $post_id ) {
// 1. readonly checkbox
$readonly = isset( $_POST['_enable_readonly'] ) ? esc_attr( $_POST['_enable_readonly'] ) : '';
update_post_meta( $post_id, '_enable_readonly', $readonly );
// 2. Readonly fields: allow saving when readonly is disabled
if( ! isset( $_POST['_enable_readonly'] ) ){
// Save text field 1 value
if( isset( $_POST['_text_field_ro1'] ) ){
update_post_meta( $post_id, '_text_field_ro1', sanitize_text_field( $_POST['_text_field_ro1'] ) );
}
// Save text field 2 value
if( isset( $_POST['_text_field_ro2'] ) ){
update_post_meta( $post_id, '_text_field_ro2', sanitize_text_field( $_POST['_text_field_ro2'] ) );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Checkbox is disabled (fields are not readonly):
Checkbox is enabled (fields are readonly):
I have been searching everywhere for an answer and just keep hitting road block after road block I have added custom variations fields in WooCommerce using http://www.remicorson.com/woocommerce-custom-fields-for-variations/ These fields save correctly when updating products.
I have installed WooCommerce Customer / Order CSV Export and have added a meta_field column placing the meta_key found in the code below.
Screenshot of the fields
However when I export the CSV file the fields are empty. Anyone have any experience with getting the custom variation field data to appear when exporting order data via CSV.
Listed below is the custom variation code being used. Any guidance would be greatly appreciated.
/**
* Create new fields for variations
*
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
// Text Field
woocommerce_wp_text_input(
array(
'id' => 'source_code[' . $variation->ID . ']',
'label' => __( 'Supplier', 'woocommerce' ),
'desc_tip' => 'true',
'value' => get_post_meta( $variation->ID, 'source_code', true )
)
);
woocommerce_wp_text_input(
array(
'id' => 'source_prod_id[' . $variation->ID . ']',
'label' => __( 'Supplier ID', 'woocommerce' ),
'desc_tip' => 'true',
'value' => get_post_meta( $variation->ID, 'source_prod_id', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
woocommerce_wp_text_input(
array(
'id' => 'pkg_desc[' . $variation->ID . ']',
'label' => __( 'Package Size', 'woocommerce' ),
'desc_tip' => 'true',
'value' => get_post_meta( $variation->ID, 'pkg_desc', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
woocommerce_wp_text_input(
array(
'id' => 'cost[' . $variation->ID . ']',
'label' => __( 'Cost', 'woocommerce' ),
'desc_tip' => 'true',
'value' => get_post_meta( $variation->ID, 'cost', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
/**
* Save new fields for variations
*
*/
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['source_code'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, 'source_code', esc_attr( $text_field ) );
}
$number_field = $_POST['source_prod_id'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, 'source_prod_id', esc_attr( $number_field ) );
}
$number_field = $_POST['pkg_desc'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, 'pkg_desc', esc_attr( $number_field ) );
}
$number_field = $_POST['cost'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, 'cost', esc_attr( $number_field ) );
}
}
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
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 );
Is it possible to do add some extra fields in WooCommerce products pages shipping tab settings in backend, as I need to add something like 12 custom fields.
I have tried to find some related hooks without success. The only way that I have found was over attributes, but it was not a convenient solution…
How can I add custom fields to WooComerce product setting pages in the shipping tab?
This is possible and you will get this (here I have set to custom text fields):
Here is the code:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_shipping', 'add_custom_shipping_option_to_products');
function add_custom_shipping_option_to_products(){
global $post, $product;
echo '</div><div class="options_group">'; // New option group
woocommerce_wp_text_input( array(
'id' => '_custom_text_field1',
'label' => __( 'My Text Field one', 'woocommerce' ),
'placeholder' => 'something',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $post->ID, '_custom_meta_field1', true ),
) );
woocommerce_wp_text_input( array(
'id' => '_custom_text_field2',
'label' => __( 'My Text Field two', 'woocommerce' ),
'placeholder' => 'something',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $post->ID, '_custom_meta_field2', true ),
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_custom_shipping_option_to_products' );
function save_custom_shipping_option_to_products( $post_id ){
$custom_text_field1 = $_POST['_custom_text_field1'];
if( isset( $custom_text_field1 ) )
update_post_meta( $post_id, '_custom_meta_field1', esc_attr( $custom_text_field1 ) );
$custom_text_field2 = $_POST['_custom_text_field2'];
if( isset( $custom_text_field2 ) )
update_post_meta( $post_id, '_custom_meta_field2', esc_attr( $custom_text_field2 ) );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce 3+ and works