2016-03-01 30 views
1

我想在woocommerce產品管理部分的下拉列表中以特定順序顯示「屬性」。屬性沒有以正確的順序輸入。現在重做它已經太晚了,所以我想在下拉菜單中列出它們應該顯示的內容。輸入數據的人在列表中上下滾動太耗時。如果他們已經按照正確的順序,那會更快。我已經看到了如何迫使他們在前端而不是在管理部分在一個特定的順序顯示 - 感謝我想在woocommerce產品管理部分的下拉列表中以特定順序顯示「屬性」

+0

轉到屬性選項卡並將其拖放到所需的順序。 – helgatheviking

+0

我不想這麼做 - 它太費時 - 屬性已經在不同的時間創建,所以現在當將大約20個屬性應用到一個產品時,我們必須在下拉菜單中上下移動以將它們添加到正確的訂單或我們添加它們,我們通過拖動它們來重新排序 - 兩種方法都非常耗時。如果屬性在下拉列表中按正確順序排列,可以節省大量時間。謝謝 – ESP5466

+0

特定的順序是什麼?我將不得不考慮如何以編程方式執行此操作。按字母順序排列似乎是最可行的。 – helgatheviking

回答

1

只要到

產品 - >屬性

,選擇你想重新排序的屬性。然後點擊「配置條款」圖標。在這裏你會看到所有的屬性條款。只需按您想要的順序拖放即可。提示:

enter image description here

+0

非常感謝,但我不想重新排序容易(拖動)的屬性的「條款」。將產品添加到產品時,我想在產品頁面的下拉菜單中按特定順序顯示屬性。屬性的條款不是問題。 – ESP5466

+1

讓我看看你想要的結構。因爲我認爲沒有人能告訴你想要什麼? –

1

運行該代碼一次,然後將其刪除。因此將其加載到插件中,激活插件,然後停用。我想過自動停用或創建一次運行,但我會讓你處理它。如果代碼第一次不正確,那實際上會很麻煩。

也許你可以修改第二個功能,也可以在save_post上運行,或者在店鋪經理未來按錯誤順序添加屬性時更換attributes_cmp()。實際上,一個名爲update_post_metadata的過濾器,可以讓您在每次更新帖子和保存元數據時始終如一地應用此選項。

警告備份您的數據庫!此代碼具有破壞性,並且將永久更改您的數據庫,並且而不是已被測試,除了看到新的排序功能在演示陣列上工作之外。使用風險自負。

function so_35733629_update_products(){ 
    $args = array(
     'posts_per_page' => -1, 
     'meta_value'  => '', 
     'post_type'  => 'product', 
     'post_status'  => 'any', 
    ); 
    $products_array = get_posts($args); 

    foreach($products_array as $product){ 
     $attributes = get_post_meta($product->ID, '_product_attributes', true); 
     if(! empty($atttributes)){ 
      $attributes = so_35733629_reorder_attributes($attributes); 
      update_post_meta($product->ID, '_product_attributes', $attributes); 
     } 
    } 
} 
add_action('admin_init', 'so_35733629_update_products'); 

function so_35733629_reorder_attributes($attributes){ 

    // here is the desired order 
    $order = array( 
     'pa_maker', 
     'pa_model-name', 
     'pa_man-lady', 
     'pa_model-case-ref', 
     'pa_case-serial' 
    ); 

    $new_attributes = array(); 

    // create new array based on order of $order array 
    foreach($order as $key){ 
     if(isset($attributes[$key])){ 
      // add to new attributes array 
      $new_attributes[$key] = $attributes[$key]; 
      // remove from the attributes array 
      unset($attributes[$key]); 
     } 
    } 

    // merge any leftover $attributes in at the end so we don't accidentally lose anything 
    $new_attributes = array_merge($new_attributes, $attributes); 

    // set the new position keys 
    $i = 0; 
    foreach($new_attributes as $key => $attribute){ 
     // update the position 
     $new_attributes[$key]['position'] = $i; 
     $i++; 
    } 

    return $new_attributes; 
} 
相關問題