2017-07-11 17 views
1

私は私の店でこのような類似した何かを実装したい:私は、各バリエーションの隣に、このコードが、このショーの量を持っている表示在庫状況

enter image description here

を。

function get_stock_variations_from_product(){ 
global $product; 
$variations = $product->get_available_variations(); 
foreach($variations as $variation){ 
    $variation_id = $variation['variation_id']; 
    $variation_obj = new WC_Product_variation($variation_id); 
    $stock = $variation_obj->get_stock_quantity(); 
} 
} 

そしてまた、このコード:

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

//List all attributes with stock available or not array.. 
echo '<pre>'; 
print_r($var_data); 
echo '</pre>'; 
die; 

私は次の各属性値に在庫状況を表示するには、変数の製品をカスタマイズすることができますどのように?あなたはwoocommerce_variation_option_nameフィルターフックに引っかけカスタム関数を使用してそれを行うことができます

おかげ

答えて

2

更新WooCommerceの互換性または以前のバージョンの2.6.x

。これは...ここ

はコードそれらの変形のためのユニークな属性を持っている製品に対してのみ実行可能です。

add_filter('woocommerce_variation_option_name', 'customizing_variations_terms_name', 10, 1); 
function customizing_variations_terms_name($term_name){ 

    if(is_admin()) 
     return $term_name; 

    global $product; 
    $second_loop_stoped = false; 

    // Get available product variations 
    $product_variations = $product->get_available_variations(); 

    // Iterating through each available product variation 
    foreach($product_variations as $variation){ 

     $variation_id = $variation['variation_id']; 
     $variation_obj = new WC_Product_Variation($variation_id); 

     ## WOOCOMMERCE RETRO COMPATIBILITY ## 
     if (version_compare(WC_VERSION, '3.0', '<')) # BEFORE Version 3 (older) 
     { 
      $stock_status = $variation_obj->stock_status; 
      $stock_qty = intval($variation_obj->stock); 

      // The attributes WC slug key and slug value for this variation 
      $attributes_arr = $variation_obj->get_variation_attributes(); 
     } 
     else # For newest verions: 3.0+ (and Up) 
     { 
      $stock_status = $variation_obj->get_stock_status(); 
      $stock_qty = $variation_obj->get_stock_quantity(); 

      // The attributes taxonomy key and slug value for this variation 
      $attributes_arr = $variation_obj->get_attributes(); 
     } 

     if(count($attributes_arr) != 1) // Works only for 1 attribute set in the product 
      return $term_name; 

     // Get the terms for this attribute 
     foreach($attributes_arr as $attr_key => $term_slug){ 
      // Get the attribute taxonomy 
      $term_key = str_replace('attribute_', '', $attr_key); 

      // get the corresponding term object 
      $term_obj = get_term_by('slug', $term_slug, $term_key); 
      if($term_obj->name == $term_name){ // If the term name matches we stop the loops 
       $second_loop_stoped = true; 
       break; 
      } 
     } 
     if($second_loop_stoped) 
      break; 
    } 
    if($stock_qty>0) 
     return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')'; 
    else 
     return $term_name .= ' - ' . $stock_status; 

} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに入るか、また、任意のプラグインファイルに保存します。

このコードはWooCommerce 2.6.xおよび3+で動作しテストされています。


あなたはこの(例えば)を取得します:あなたが望むデータを取得し、表示するために、他のWC_Product_VariationまたはWC_Product方法を使用することができます

enter image description here

...

+0

こんにちは、LoicはWoo 3.Xで動作します。 Woo 2.Xでも動作するようにコードを修正できますか? – DrMTR

+0

ありがとう! – DrMTR

+0

コードは更新され、テストされ、バージョン2.6.xでも動作します – LoicTheAztec

関連する問題