2017-09-22 8 views
2

Actuellement, je peux ajouter un menu de sélection personnalisé à la zone de modification de mon produit.Plusieurs champs personnalisés de sélection dans l'onglet de configuration des variantes de produit WooCommerce

Cependant, je voudrais ajouter un menu de sélection supplémentaire à la variation du produit, mais je ne sais pas comment.

Voici mon code pour générer un menu dans mon thème enfant functions.php:

// Add Variation Settings 
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3); 
// Save Variation Settings 
add_action('woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2); 
/** 
* Create new fields for variations 
* 
*/ 
function variation_settings_fields($loop, $variation_data, $variation) { 

    // 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') 
      ) 
     ) 
    ); 

} 
/** 
* Save new fields for variations 
* 
*/ 
function save_variation_settings_fields($post_id) { 

    // Select 
    $select = $_POST['_select'][ $post_id ]; 
    if(! empty($select)) { 
     update_post_meta($post_id, '_select', esc_attr($select)); 
    } 

} 

Répondre

1

Pour ajouter 2 champs personnalisés de type « select » dans les paramètres de l'onglet variations de produits, est très facile, vous devez dupliquer les champs de réglage pour chaque différent slug ID et limaces clés. J'ai légèrement modifié le code et cela fonctionne aussi.

Voici ce code:

// Add 2 custom fields in Variation Settings 
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3); 
function variation_settings_fields($loop, $variation_data, $variation) { 

    // Select 1 
    woocommerce_wp_select(array(
     'id'   => 'first_custom_field_' . $variation->ID, 
     'label'  => __('My Select Field 1', 'woocommerce'), 
     'value'  => get_post_meta($variation->ID, '_first_custom_field', true), 
     'options' => array(
      '' => __('Please choose a value', 'woocommerce'), 
      'value 1' => __('Option 1', 'woocommerce'), 
      'value 2' => __('Option 2', 'woocommerce'), 
      'value 3' => __('Option 3', 'woocommerce') 
     ) 
    )); 

    // Select 2 
    woocommerce_wp_select(array(
     'id'   => 'second_custom_field_' . $variation->ID, 
     'label'  => __('My Select Field 2', 'woocommerce'), 
     'value'  => get_post_meta($variation->ID, '_second_custom_field', true), 
     'options' => array(
      '' => __('Please choose a value', 'woocommerce'), 
      'value 1' => __('Option 1', 'woocommerce'), 
      'value 2' => __('Option 2', 'woocommerce'), 
      'value 3' => __('Option 3', 'woocommerce') 
     ) 
    )); 
} 
// Save 2 custom fields values in Variation post meta data 
add_action('woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2); 
function save_variation_settings_fields($post_id) { 

    // Select 1 
    $select = $_POST["first_custom_field_$post_id"]; 
    if(! empty($select)) { 
     update_post_meta($post_id, '_first_custom_field', esc_attr($select)); 
    } 

    // Select 2 
    $select = $_POST["second_custom_field_$post_id"]; 
    if(! empty($select)) { 
     update_post_meta($post_id, '_second_custom_field', esc_attr($select)); 
    } 
} 

Code va dans le fichier function.php de votre thème enfant actif (ou le thème) ou encore dans un fichier de plug-in.

Tout le code a été testé sur Woocommerce 3+ et fonctionne. Vous obtiendrez ceci:

enter image description here


Exemple d'utiliser ou d'afficher ces valeurs (en frontend):

// You need to get the variation Id from somewhere 
$variation_id = 41; 

// Then you get this custom post meta data 
$value1 = get_post_meta($variation_id, '_first_custom_field', true); 
$value2 = get_post_meta($variation_id, '_second_custom_field', true); 

// And may be display it 
echo '<p>My value 1: ' . $value1 . '</p>'; 
echo '<p>My value 2: ' . $value2 . '</p>'; 
+1

Un grand merci. Dès que je serai de retour à mon bureau dans 30 minutes je vais essayer ceci :-) – michaelmcgurk

+0

Ça marche super - juste un suivi. Comment afficher cette valeur sur le frontal? – michaelmcgurk

+1

@michaelmcgurk Mis à jour ma réponse, concernant l'utilisation ... – LoicTheAztec