Für ein spezielles Anliegen musste ich herausfinden, wie man Custom Fields in WooCommerce Produktvariationen anlegt. Auf der Suche nach einer Lösung wurde ich auch schnell fündig und landete bei WooCommerce Custom Fields for Variations.
Doch innerhalb des letzten Jahres muss WooCommerce das Produkt-Variationen-Handling derart verändert haben, dass die Vorgehensweise nicht mehr zum gewünschten Ergebnis führte.
Beim Durchsuchen der WooCommerce Dateien stellte sich heraus, dass es den Hook woocommerce_product_after_variable_attributes_js
gar nicht mehr gab, und woocommerce_process_product_meta_variable
offenbar durch woocommerce_save_product_variation
abgelöst worden war. Dafür musste woocommerce_product_after_variable_attributes
nun mit drei Parametern gehookt werden ( $loop, $variation_data, $variation )
. Auch die Form in der die Daten vom Formular übergeben werden, dürfte nicht mehr dieselbe sein, denn die for-Schleife um jedes Feld fiel ebenfalls weg.
Der neue Code für WooCommerce Custom Fields for Variations
<?php
//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 3 );
//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 2 );
/**
* Create new fields for variations
*
*/
function variable_fields( $loop, $variation_data, $variation ) {
?>
<div class="variable_custom_fields">
<?php
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field[' . $variation->ID . ']',
'label' => __( 'My Text Field', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_text_field', true )
)
);
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_number_field[' . $variation->ID . ']',
'label' => __( 'My Number Field', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Enter the custom number here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_number_field', true ),
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea[' . $variation->ID . ']',
'label' => __( 'My Textarea', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea', true ),
)
);
// Select
woocommerce_wp_select(
array(
'id' => '_select[' . $variation->ID . ']',
'label' => __( 'My Select Field', 'woocommerce' ),
'description' => __( 'Choose a value.', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_select', true ),
'options' => array(
'one' => __( 'Option 1', 'woocommerce' ),
'two' => __( 'Option 2', 'woocommerce' ),
'three' => __( 'Option 3', 'woocommerce' )
)
)
);
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_checkbox[' . $variation->ID . ']',
'label' => __('My Checkbox Field', 'woocommerce' ),
'description' => __( 'Check me!', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_checkbox', true ),
)
);
// Hidden field
woocommerce_wp_hidden_input(
array(
'id' => '_hidden_field[' . $variation->ID . ']',
'value' => 'hidden_value'
)
);
?>
</div>
<?php
}
/**
* Save custom field values for variations
*
*/
function save_variable_fields( $variation_id, $i ) {
// Text Field
if ( isset( $_POST['text_field'][$i] ) ) {
$text_field_value = stripslashes( $_POST['text_field'][$i] );
update_post_meta( $variation_id, '_text_field', $text_field_value );
}
// Number Field
if ( isset( $_POST['number_field'][$i] ) ) {
$number_field_value = stripslashes( $_POST['number_field'][$i] );
update_post_meta( $variation_id, '_number_field', $number_field_value );
}
// Textarea
if ( isset( $_POST['textarea'][$i] ) ) {
$textarea_value = stripslashes( $_POST['textarea'][$i] );
update_post_meta( $variation_id, '_textarea', $textarea_value );
}
// Select
if ( isset( $_POST['select'][$i] ) ) {
$select_value = stripslashes( $_POST['select'][$i] );
update_post_meta( $variation_id, '_select', $select_value );
}
// Checkbox
if ( isset( $_POST['checkbox'][$i] ) ) {
$checkbox_value = stripslashes( $_POST['checkbox'][$i] );
update_post_meta( $variation_id, '_checkbox', $checkbox_value );
}
}
Schreibe einen Kommentar