¿Cómo obtener el ID de Variación en WooCommerce?
14 ene 2014, 19:44:48
Vistas: 27.1K
Votos: 3
Estoy tratando de crear funcionalidad adicional en mi backend de WooCommerce donde agrego un campo personalizado a la sección de variaciones del backend. Sin embargo, no logro encontrar la manera de obtener el ID de la variación actual para obtener el post meta.
Esto es con lo que he estado trabajando:
<?php
// Obtener variaciones
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );
foreach ( $variations as $variation ) {
$variation_id = absint( $variation->ID );$variable_id = $this['variation_id'];
$variation_post_status = esc_attr( $variation->post_status );
$variation_data = get_post_meta( $variation_id );
$variation_data['variation_post_id'] = $variation_id;
echo get_post_meta( $variation_data['variation_post_id'], '_my_custom_field', true) . ' - TEST';
}
?>
Cuando reviso el backend, parece que está extrayendo todos los post meta en cada variación de esta manera:
Sin embargo, si uso el ID de variación real como se muestra a continuación, funciona para esa variación:
echo get_post_meta( 134, '_my_custom_field', true) . ' - Test Variation #134';
ACTUALIZACIÓN:
Aquí está todo en su totalidad por si ayuda.
// Mostrar Campos
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields' );
//JS para agregar campos para nuevas variaciones
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
// Guardar Campos
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
function variable_fields( $loop, $variation_data, $variation ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'Mi Campo Personalizado', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_custom_field'][0]; ?>"/>
<?php
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_custom_field'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
}
echo get_post_meta( $variation_id, '_my_custom_field', true) . ' - Test';
endfor;
endif;
//funciona echo get_post_meta( 134, '_my_custom_field', true) . ' - Test 134';
?>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label><?php _e( 'Mi Campo Personalizado', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_custom_field'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}

Derek
373
Comentarios
Mostrar los 5 comentarios restantes
Preguntas relacionadas
3
respuestas
3
respuestas
2
respuestas
7
respuestas
3
respuestas