Ich versuche, 4 Mehrfachauswahloptionen bei Woocommerce-Produktvariationen zu erstellen.
Beispiel: Ich verkaufe Bäume und möchte anzeigen, in welcher Jahreszeit der Baum verfügbar ist. Wir haben also 4 Jahreszeiten (Frühling, Sommer, Herbst, Winter). Einige Bäume sind in zwei oder drei Jahreszeiten erhältlich.
Ich habe diesen Code zu meiner functions.php hinzugefügt, aber er speichert die ausgewählten Optionen nicht. Wenn ich die Option speichere und die Seite neu lade, sind die Optionen wieder leer.
Und ich habe mich auch gefragt, wie ich die ausgewählten Optionen auf der einzelnen Produktseite (Frontend) als Symbol anzeigen kann.
Vorerst funktioniert die Funktion mit den Optionen bei den Produktvariationen. Bitte sehen Sie sich diesen Screenshot an (Produktvariation mit Multi-Select-Optionen) :
Mein Code:
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
/**
* Create custom field type
*
*/
function woocommerce_wp_select_multiple( $field ) {
global $thepostid, $post, $woocommerce;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$field['value'] = isset( $field['value'] ) ? $field['value'] : ( get_post_meta( $thepostid, $field['id'], true ) ? get_post_meta( $thepostid, $field['id'], true ) : array() );
echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><select id="' . esc_attr( $field['id'] ) . '" name="' . esc_attr( $field['name'] ) . '" class="' . esc_attr( $field['class'] ) . '" multiple="multiple">';
foreach ( $field['options'] as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '" ' . ( in_array( $key, $field['value'] ) ? 'selected="selected"' : '' ) . '>' . esc_html( $value ) . '</option>';
}
echo '</select> ';
if ( ! empty( $field['description'] ) ) {
if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
echo '<img class="help_tip" data-tip="' . esc_attr( $field['description'] ) . '" src="'%20.%20esc_url(%20WC()->plugin_url()%20)%20.%20'/assets/images/help.png" height="16" width="16" />';
} else {
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
}
}
echo '</p>';
}
/**
* Create new fields for variations
*
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_select_multiple( array(
'id' => 'season_' . $variation->ID,
'class' => 'season',
'label' => __('Season', 'woocommerce'),
'value' => get_post_meta( $variation->ID, '_season', true ),
'options' => array(
'spring' => 'Spring',
'summer' => 'Summer',
'autumn' => 'Autumn',
'winter' => 'Winter',
))
);
}
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $post_id ) {
$select = $_POST["season_$post_id"];
if( ! empty( $select ) ) {
update_post_meta( $post_id, '_season', esc_attr( $select ) );
}
}
Zu handhaben variable Produkte für Mehrfachauswahlfelder ist eine andere Sache und einige Änderungen müssen vorgenommen werden, damit es funktioniert. Ich habe 2 Antworten nach unten gemacht:
Der Erste für Produktvariationen (für Sie)
Die andere für alle anderen Produktarten
Die Hauptfunktion, die Mehrfachauswahlfelder im WooCommerce-Backend ermöglicht, ist also:
function woocommerce_wp_multi_select( $field, $variation_id = 0 ) {
global $thepostid, $post;
if( $variation_id == 0 )
$the_id = empty( $thepostid ) ? $post->ID : $thepostid;
else
$the_id = $variation_id;
$field['class'] = isset( $field['class'] ) ? $field['class'] : 'select short';
$field['wrapper_class'] = isset( $field['wrapper_class'] ) ? $field['wrapper_class'] : '';
$field['name'] = isset( $field['name'] ) ? $field['name'] : $field['id'];
$meta_data = maybe_unserialize( get_post_meta( $the_id, $field['id'], true ) );
$meta_data = $meta_data ? $meta_data : array() ;
$field['value'] = isset( $field['value'] ) ? $field['value'] : $meta_data;
echo '<p class="form-field ' . esc_attr( $field['id'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '"><label for="' . esc_attr( $field['id'] ) . '">' . wp_kses_post( $field['label'] ) . '</label><select id="' . esc_attr( $field['id'] ) . '" name="' . esc_attr( $field['name'] ) . '" class="' . esc_attr( $field['class'] ) . '" multiple="multiple">';
foreach ( $field['options'] as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '" ' . ( in_array( $key, $field['value'] ) ? 'selected="selected"' : '' ) . '>' . esc_html( $value ) . '</option>';
}
echo '</select> ';
if ( ! empty( $field['description'] ) ) {
if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {
echo '<img class="help_tip" data-tip="' . esc_attr( $field['description'] ) . '" src="'%20.%20esc_url(%20WC()->plugin_url()%20)%20.%20'/assets/images/help.png" height="16" width="16" />';
} else {
echo '<span class="description">' . wp_kses_post( $field['description'] ) . '</span>';
}
}
}
Der Code wird in die function.php-Datei Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Diese Funktion verarbeitet jetzt alle Arten von Produkten, einschließlich Produktvariationen.
Verwandt: Multi-Checkbox-Felder im Woocommerce-Backend
1). Für Produktvariationen (für dich):
// Add custom multi-select fields in variation setting tab
add_action( 'woocommerce_product_after_variable_attributes', 'add_variation_settings_fields', 20, 3 );
function add_variation_settings_fields( $loop, $variation_data, $variation_post ) {
woocommerce_wp_multi_select( array(
'id' => '_season',
'name' => '_season['.$loop.'][]',
'class' => '',
'label' => __('Season', 'woocommerce'),
'options' => array(
'spring' => __("Spring", "woocommerce"),
'summer' => __("Summer", "woocommerce"),
'autumn' => __("Autumn", "woocommerce"),
'winter' => __("Winter", "woocommerce"),
)
), $variation_post->ID );
}
// Save custom multi-select fields for variations
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $variation_id, $i ) {
if( isset( $_POST['_season'][$i] ) ){
$post_data = $_POST['_season'][$i];
// Multi data sanitization
$sanitize_data = array();
if( is_array($post_data) && sizeof($post_data) > 0 ){
foreach( $post_data as $value ){
$sanitize_data[] = esc_attr( $value );
}
}
update_post_meta( $variation_id, '_season', $sanitize_data );
}
}
Der Code wird in die function.php-Datei Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.
2). Für alle anderen Produktarten (außer Produktvariationen, bei denen wir dieses benutzerdefinierte Feld ausblenden):
// Add custom fields for product general option settings (hidding it for variable products)
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_settings_fields', 20 );
function add_custom_settings_fields() {
global $post;
echo '<div class="options_group hide_if_variable"">'; // Hidding in variable products
woocommerce_wp_multi_select( array(
'id' => '_season',
'name' => '_season[]',
'class' => '',
'label' => __('Season', 'woocommerce'),
'options' => array(
'spring' => __("Spring", "woocommerce"),
'summer' => __("Summer", "woocommerce"),
'autumn' => __("Autumn", "woocommerce"),
'winter' => __("Winter", "woocommerce"),
)
) );
echo '</div>';
}
// Save custom multi-select fields to database when submitted in Backend (for all other product types)
add_action( 'woocommerce_process_product_meta', 'save_product_options_custom_fields', 30, 1 );
function save_product_options_custom_fields( $post_id ){
if( isset( $_POST['_season'] ) ){
$post_data = $_POST['_season'];
// Multi data sanitization
$sanitize_data = array();
if( is_array($post_data) && sizeof($post_data) > 0 ){
foreach( $post_data as $value ){
$sanitize_data[] = esc_attr( $value );
}
}
update_post_meta( $post_id, '_season', $sanitize_data );
}
}
Der Code wird in die function.php-Datei Ihres aktiven untergeordneten Designs (oder aktiven Designs) eingefügt. Getestet und funktioniert.