2017-05-18 28 views
1

私は以下のコードで行ったバリエーションの株式テキストを探したいですが、私は適切な方法を探しています。 woocommereceプラグインのファイルtemplates/single-product/add-to-cart/variable.phpから以下のコードを取得します。助けていただければ幸いです。どのように変動株を見つけるために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>'; 
       } 
      } 

実際には、私は画像で見ることができますvarationドロップダウンにストックテキストを追加したいと思います。 enter image description here

答えて

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']; 
    } 

?> 

テンプレートを編集していない

+0

はあなたがバディありがとうございます。うん、私はそれを知っているが、私はterm_idまたは用語スラグでこのコードが欲しい。私はterm_idを渡したいと思っています。 – Coder

+0

あなたは私のコードで最後に私は株式などに書いている選択オプションを見ることができます – Coder

1

これをチェックしてください。 これをあなたのテーマのfunctions.phpに貼り付けてください。

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; 
} 

それは次のようになります:enter image description here

+0

@Reigalは動作しません。画像では、名前と$ variation-> get_attributes()配列を表示しているのがわかりますか? In_arrayは満足していないと思いますか? – Coder

+0

まあ、そのアイデアは既にあります。あなたが持っているものに関連するコードを修正できるはずです... – Reigel

関連する問題