2017-09-05 5 views
0

Je suis en train de créer un site où les produits Variable WooCommerce (avec Variations de produits) sont insérés par programme. Je l'ai suivi avec succès ce tutoriel:
Insert WooCommerce Products & Variations ProgrammaticallyDéfinir les variations des valeurs d'attributs par défaut pour un produit variable Par programme

J'ai besoin juste quelque chose qui manque:
Comment définir les attributs par défaut des variations des valeurs pour un produit variable? Est-ce possible?

Répondre

1

1) Vous devez insérer dans votre json données pour chaque produit variable de ce morceau de tableau comme:

 "variations_default_attributes": 
     [ 
      { 
       "size" : "Medium", 
       "color" : "Blue" 
      } 
     ] 

Ou

"variations_default_attributes": 
    { 
     "size" : "Medium", 
     "color" : "Blue" 
    } 

Ce tableau est bonne avec l'attribut slug court (sans 'pa_') et le terme par défaut nom valeurs.

2) Ensuite, la fonction dédiée:

function insert_variations_default_attributes($post_id, $products_data){ 
    foreach($products_data as $attribute => $value) 
     $variations_default_attributes['pa_'.$attribute] = get_term_by('name', $value, 'pa_'.$attribute)->slug; 
    // Save the variation default attributes to variable product meta data 
    update_post_meta($post_id, '_default_attributes', $variations_default_attributes); 
} 

3) Vous devrez déclencher cette fonction en ajoutant une ligne à la fin:

function insert_product ($product_data) 
{ 
    $post = array(// Set up the basic post data to insert for our product 

     'post_author' => 1, 
     'post_content' => $product_data['description'], 
     'post_status' => 'publish', 
     'post_title' => $product_data['name'], 
     'post_parent' => '', 
     'post_type' => 'product' 
    ); 

    $post_id = wp_insert_post($post); // Insert the post returning the new post id 

    if (!$post_id) // If there is no post id something has gone wrong so don't proceed 
    { 
     return false; 
    } 

    update_post_meta($post_id, '_sku', $product_data['sku']); // Set its SKU 
    update_post_meta($post_id,'_visibility','visible'); // Set the product to visible, if not it won't show on the front end 

    wp_set_object_terms($post_id, $product_data['categories'], 'product_cat'); // Set up its categories 
    wp_set_object_terms($post_id, 'variable', 'product_type'); // Set it to a variable product type 

    insert_product_attributes($post_id, $product_data['available_attributes'], $product_data['variations']); // Add attributes passing the new post id, attributes & variations 
    insert_product_variations($post_id, $product_data['variations']); // Insert variations passing the new post id & variations 

    ## Insert variations default attributes passing the new post id & variations_default_attributes 
    insert_variations_default_attributes($post_id, $products_data['variations_default_attributes']);  
} 

Code va dans le fichier function.php de votre thème enfant actif (ou thème) ou aussi dans n'importe quel fichier plugin.

Ce code a été testé et fonctionne.

+1

Cela fonctionne grâce :) –

+0

Essayé update_post_meta ($ post_id, '_default_attributes', $ variations_default_attributes); dans wc3.0 cela ne fonctionne pas. J'ai donc essayé ce qui suit: $ standardValArr = array(); $ standardValArr ['pa_groesse'] = 'M'; $ parent_product-> set_default_attributes ($ standardValArr); Malheureusement, cela ne fonctionne pas. –

+0

@JanineKroser Qu'essayez-vous de faire? quel est le problème ou votre problème? Peut-être devriez-vous poser une nouvelle question, car cette réponse pourrait ne pas vous convenir. – LoicTheAztec