2017-05-18 3 views
1

Ich möchte eine Variation Aktien Text Ich habe mit unten Code, aber ich bin auf der Suche nach einer geeigneten Methode. Ich bekomme unten angegebenen Code von templates/single-product/add-to-cart/variable.php Datei von woocomeremece Plugin. bitte jede Hilfe wird geschätzt.So finden Sie Varation Lager woocommerece

 $terms = wc_get_product_terms($product->id, $attribute, array('fields' => 'all')); 

      foreach ($terms as $term) { 

       if (in_array($term->slug, $options)) { 
        $product_variations_cus = $product->get_available_variations(); // my custom code start from here 
        foreach($product_variations_cus as $var_cus){ 
         foreach($var_cus['attributes'] as $stock) { 
          if($term->slug==$stock) 
          $availability_html = $var_cus['availability_html']; // end here 
         } 

        } 

        $html .= '<option value="' . esc_attr($term->slug) . '" ' . selected(sanitize_title($args['selected']), $term->slug, false) . '>' . esc_html(apply_filters('woocommerce_variation_option_name', $term->name)) .' '.$availability_html.'</option>'; 
       } 
      } 

Eigentlich möchte ich Stock Text zu Varation DropDown hinzufügen kann in Bild sehen. enter image description here

Antwort

1
<?php 
    global $product; 
    $product_variations = $product->get_available_variations(); 

    foreach ($product_variations as $variation) { 
     $var_data = $variation['attributes']; 
     $var_data['in_stock'] = $variation['is_in_stock']; 
    } 

?> 

prüfen diese

+0

Vielen Dank Kumpel. Ja, ich weiß das, aber ich will diesen Code von term_id oder term slug. Bedeutet, dass ich "term_id" übergeben möchte, dann haben sie einen Vorrat. – Coder

+0

Bitte sehen Sie in meinem Code wählen Sie Option in letzten Ich habe auf Lager usw. – Coder

1

nicht aus Vorlagen bearbeiten. fügen Sie dies stattdessen in die functions.php Ihres Themes ein.

add_filter('woocommerce_variation_option_name', 'woocommerce_variation_option_name', 10); 
function woocommerce_variation_option_name($name) { 
    global $product; 

    foreach($product->get_children() as $variation_id) { 
     $variation = wc_get_product($variation_id); 

     if (in_array(strtolower($name), $variation->get_attributes())) { 
      $availability = $variation->get_availability(); 
      return $name . ' ' . $availability['availability']; 
     } 
    } 

    return $name; 
} 

wird es in etwa so aussehen: enter image description here

+0

geschrieben @Reigal funktioniert nicht. Im Bild können Sie sehen, dass ich den Namen und das Array $ variation-> get_attributes() ausgegeben habe. In_array ist nicht zufrieden, denke ich? – Coder

+0

Nun, die Idee ist schon da ... Sie sollten in der Lage sein, den Code zu reparieren, der sich auf das bezieht, was Sie haben ... – Reigel