Reputation: 35
I add custom fields to product variations (thanks to this post)
I add this code in wp-content\themes\my_theme\woocommerce\single-product\add-to-cart\product-attributes.php template:
<?php
/**
* Product attributes
* @version 3.6.0
*/
defined( 'ABSPATH' ) || exit;
if ( ! $product_attributes ) {
return;
}
?>
<ul id="new_product_attributes" class="accordion" data-accordion data-allow-all-closed="true">
<li class="accordion-item" data-accordion-item>
<a href="#" class="accordion-title">Caratteristiche tecniche</a>
<div class="accordion-content" data-tab-content>
<div class="attributes_container">
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div class="single_variation_wrap">
<div id="variation_clone_box" class="woocommerce-variation single_variation">
<script type="text/template" id="tmpl-variation-template">
<table class="woocommerce-product-attributes shop_attributes">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--my_field">
<th class="woocommerce-product-attributes-item__label">My Field Label</th>
<td id="my_field" class="woocommerce-product-attributes-item__value">{{{ data.variation.my_field }}}</td>
</tr>
</table>
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
<div id="old_box_Price" class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
</script>
</div>
</div>
<div class="datasheets_box">
<a class="button">(i) Scarica la scheda tecnica</a>
</div>
<div class="richiedi_info_box">
<p class="richiedi_info_title">Richiedi informazioni</p>
<p><strong>Assistenza Clienti</strong></p>
<p>
ORARI<br>
Dal Lunedì al Venerdì<br>
8:00 / 13:00 - 14:00 / 17:00
<p>
<a class="button">(i) [email protected]</a>
<a class="button">(i) +39 0131 718477</a>
</p>
</div>
</div><!-- accordion-content -->
</li>
</ul><!-- #new_product_attributes -->
<div id="new_box_AddToCart">
<?php
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
?>
<div class="shipment_info">
<p>Disponibilità immediata</p>
<p>I costi di spedizione sono esclusi</p>
</div>
<button type="submit" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
</div>
This works very well!
In the code above I add many rows. Each line corresponds to a new custom field. To simplify, in this post I paste only one row with only one custom field. The code below is the part of table to hide ...if custom field is empty.
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--my_field">
<th class="woocommerce-product-attributes-item__label">My Field Label</th>
<td id="my_field" class="woocommerce-product-attributes-item__value">
{{{ data.variation.my_field }}}
</td>
</tr>
If {{{ data.variation.my_field }}} is empty, Woocommerce shows the row. I've many custom fields in product variations (see attachment) so I try to show only the rows with a value.
I would like to hide the row if {{{ data.variation.my_field }}} is empty.
I tried this but it does not work:
{{{ #if data.variation.my_field }}}
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--my_field">
<th class="woocommerce-product-attributes-item__label">My Field</th>
<td id="your_field" class="woocommerce-product-attributes-item__value">
{{{ data.variation.my_field }}}
</td>
</tr>
{{{/if }}}
How to Add Custom Fields to Product Variations (this part works)
Now I write you all the code that I used to add custom field to product variations. I don't know if it can help you to solve the problem ;)
To add custom field for variations I wrote this in function.php.
I create new custom field for variations (here called "My Field"):
function my_field_settings( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => "my_field{$loop}",
'label' => __( 'My Field', 'woocommerce' ),
'name' => "my_field[{$loop}]",
'value' => get_post_meta( $variation->ID, 'my_field', true ),
'wrapper_class' => 'form-row form-row-33',
)
);
}
add_action( 'woocommerce_product_after_variable_attributes', 'my_field_settings', 10, 3 );
After, I save the new custom field:
function my_field_save( $variation_id, $loop ) {
$text_field = $_POST['my_field'][ $loop ];
update_post_meta( $variation_id, 'my_field', esc_attr( $text_field ));
}
add_action( 'woocommerce_save_product_variation', 'my_field_save', 10, 2 );
At the end I add my new custom field:
function my_field_load( $variation ) {
$variation['my_field'] = get_post_meta( $variation[ 'variation_id' ], 'my_field', true );
return $variation;
}
add_filter( 'woocommerce_available_variation', 'my_field_load' );
Upvotes: 1
Views: 385
Reputation: 254373
I have revisited all your code, using WooCommerce CRUD Methods and latest WooCommerce hooks.
I have added some jQuery code to allow showing or hiding the "Diametro nominale" display.
I have shortened the code on the template...
Here is that code:
function add_admin_variation_custom_fields( $loop, $variation_data, $variation ) {
$variation_object = wc_get_product($variation->ID);
woocommerce_wp_text_input( array(
'id' => "diametro_nominale_{$loop}",
'label' => __( 'Diametro nominale', 'woocommerce' ),
'value' => $variation_object->get_meta('diametro_nominale'),
'wrapper_class' => 'form-row form-row-33',
) );
}
add_action( 'woocommerce_product_after_variable_attributes', 'add_admin_variation_custom_fields', 10, 3 );
function save_admin_variation_custom_fields( $variation, $i ) {
if ( isset($_POST['diametro_nominale_'.$i]) ) {
$variation->update_meta_data('diametro_nominale', sanitize_text_field($_POST['diametro_nominale_'.$i]));
}
}
add_action( 'woocommerce_admin_process_variation_object', 'save_admin_variation_custom_fields', 10, 2 );
function display_variation_custom_field_values( $variation_data, $product, $variation ) {
$diametro_nominale = $variation->get_meta('diametro_nominale');
$variation_data['diametro_nominale'] = empty($diametro_nominale) ? 0 : $diametro_nominale;
return $variation_data;
}
add_filter( 'woocommerce_available_variation', 'display_variation_custom_field_values', 10, 3 );
function variation_custom_fields_js() {
?>
<script>
jQuery(function($){
// After DOM (on load)
$('.shop_attributes .diametro-nominale').hide();
// On Selected variation or Unselected variation
$('form.cart').on('show_variation', function(event, data) {
// On variation "show"
if ( data.diametro_nominale == '0' ) {
// When "diametro_nominale" is empty
$('.shop_attributes .diametro-nominale').hide();
} else {
// When "diametro_nominale" has data
$('.shop_attributes .diametro-nominale').show();
}
}).on('hide_variation', function(){
// On variations "Hide"
$('.shop_attributes .diametro-nominale').hide();
});
});
</script>
<?php
}
add_action( 'woocommerce_after_variations_form', 'variation_custom_fields_js' );
Code goes in functions.php file of your child theme (or in a plugin).
And the shortened code for single-product/product-attributes.php
template file:
?>
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
<div class="single_variation_wrap">
<div id="variation_clone_box" class="woocommerce-variation single_variation">
<script type="text/template" id="tmpl-variation-template">
<table class="woocommerce-product-attributes shop_attributes">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--diametro_nominale diametro-nominale">
<th class="woocommerce-product-attributes-item__label"><?php _e( 'Diametro nominale', 'woocommerce' ); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.diametro_nominale }}}</td>
</tr>
</table>
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
<div id="old_box_Price" class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
</script>
</div>
</div>
Tested and works.
Upvotes: 1