Skip to content

Instantly share code, notes, and snippets.

@cezarpopa
Created February 16, 2015 09:19
Show Gist options
  • Save cezarpopa/718858f48032df453f53 to your computer and use it in GitHub Desktop.
Save cezarpopa/718858f48032df453f53 to your computer and use it in GitHub Desktop.
custom field single & variation
/*-----------------------------------------------------------*/
/* WooCommerce : add custom fields to product variations
/* https://gist.github.com/corsonr/9152652#file-gistfile1-php
/*----------------------------------------------------------*/
//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 );
/**
* Create new fields for variations
*
*/
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<?php
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field['.$loop.']',
'label' => __( 'Dimensiune produs', 'woocommerce' ),
'placeholder' => 'Cod produs',
'desc_tip' => 'true',
'value' => $variation_data['_text_field'][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
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[ + loop + ]',
'label' => __( 'Dimensiune produs', 'woocommerce' ),
'placeholder' => 'Cod produs',
'desc_tip' => 'true',
'value' => $variation_data['_text_field'][0]
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field[ + loop + ]',
'value' => 'hidden_value'
)
);
?>
</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'];
// Text Field
$_text_field = $_POST['_text_field'];
for ($i = 0; $i < sizeof($variable_sku); $i++) :
$variation_id = (int)$variable_post_id[$i];
if (isset($_text_field[$i])) {
update_post_meta($variation_id, '_text_field', stripslashes($_text_field[$i]));
}
endfor;
// Hidden field
$_hidden_field = $_POST['_hidden_field'];
for ($i = 0; $i < sizeof($variable_sku); $i++) :
$variation_id = (int)$variable_post_id[$i];
if (isset($_hidden_field[$i])) {
update_post_meta($variation_id, '_hidden_field', stripslashes($_hidden_field[$i]));
}
endfor;
endif;
}
/*
* Custom field for single product
* http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
*/
// 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_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field_single',
'label' => __( 'Dimensiune produs', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
echo '</div>';
}
function woo_add_custom_general_fields_save( $post_id ){
// Text Field
$woocommerce_text_field = $_POST['_text_field_single'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field_single', esc_attr( $woocommerce_text_field ) );
// Product Field Type
$product_field_type = $_POST['product_field_type'];
update_post_meta( $post_id, '_product_field_type_ids', $product_field_type );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment