2017-08-16 49 views
1

是否可以定製woocommerce變化下拉,就像如果我們有2個變化顏色大小,用一種顏色規模「大」是缺貨的,是有可能加入「出來股票「與每個變化下拉。WooCommerce定製變異下拉

+0

是的,我custmozed但我沒有得到可用數量,所以我可以選擇缺貨的選項。 謝謝 –

+0

這是可能的,只有當你有1下拉,但有多個,這是不可能的,不合邏輯...我已經回答了這樣的問題,但只有一個下拉。多於一個它不能工作。 – LoicTheAztec

+0

這是我解決這個問題的答案:[更改WooCommerce變量產品選擇器中的庫存狀態名稱?](https://stackoverflow.com/questions/45190106/change-stock-status-names-in-woocommerce-variable -products-selector/45191198#45191198),也是這樣:[顯示WooCommerce變量產品中每個屬性值旁邊的庫存狀態](https://stackoverflow.com/questions/45037405/show-stock-status-next-to - 每個屬性值在woocommerce - 變量產品/ 45041602#45041602)...所以這是可以做什麼... – LoicTheAztec

回答

0

在主題/ functions.php中試試這個

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; 

} 

張貼在這裏感謝loictheaztec

Show stock status next to each attribute value in WooCommerce variable products