Skip to content

Instantly share code, notes, and snippets.

@alphadc
Created March 9, 2015 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphadc/da163cc95cfd1cede34a to your computer and use it in GitHub Desktop.
Save alphadc/da163cc95cfd1cede34a to your computer and use it in GitHub Desktop.
/* ADD CUSTOM FIELDS TO VARIATION PRODUCT */
//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
//Save variation fields
add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );
add_action( 'woocommerce_process_product_meta_variable-subscription' , 'save_variable_fields' , 10 , 1 ) ;
/**
* Create new fields for variations
*
*/
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_weightdesc['.$loop.']',
'label' => __( 'Weight Description', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => $variation_data['_weightdesc'][0],
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field['.$loop.']',
'value' => 'hidden_value'
)
);
?>
</td>
</tr>
<?php
}
/**
* Create new fields for new variations
*
*/
function variable_fields_js() {
?>
<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_weightdesc[ + loop + ]',
'label' => __( 'My Textarea', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => $variation_data['_weightdesc'][0],
)
);
?>
</td>
</tr>
<?php
}
/**
* Save new fields for variations
*
*/
function save_variable_fields( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
// Textarea
$_weightdesc = $_POST['_weightdesc'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_weightdesc[$i] ) ) {
update_post_meta( $variation_id, '_weightdesc', stripslashes( $_weightdesc[$i] ) );
}
endfor;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment