2017-07-04 6 views

答えて

0

まずあなたがこの方法あなたの製品バリエーションIDの配列を取得:

global $wpdb; 
$posts_table = $wpdb->prefix . "posts"; 
$variation_ids = $wpdb->get_col("SELECT `ID` FROM $posts_table WHERE `post_status` LIKE 'publish' AND `post_type` LIKE 'product_variation'"); 

次にあなたがこの製品バリエーションのためにこのように体重を編集することができます。

// Iterating through all product variations 
foreach($variation_ids as $variation_id){ 

    // Get an instance of the product variation object 
    $variation_object = wc_get_product($variation_id); 

    // Get the variation weight 
    $weight = $variation_object->get_weight(); 


    ## -- USE CONDITIONS TO SET/UPDATE WEIGHT AS YOU NEED -- ## 

    // Example of conditions below 
    if($weight <= 0.2 && $weight > 0.3){ 
     $variation_object->set_weight(0.28); 
    } elseif($weight <= 0.3 && $weight > 0.5){ 
     $variation_object->set_weight(0.42); 
    } 

    // Example of empty weight condition 
    if(empty($weight)){ 
     $variation_object->set_weight(0.16); 
    } 

    // Example Based on variation IDs from 125 to 129 
    if($variation_id >= 125 && $variation_id < 130){ 
     $variation_object->set_weight(0.45); 
    } 
} 

次に、ニーズに合わせて条件をカスタマイズし、これを一度実行する関数に埋め込むことができます。

あなたも、あなたの条件での製品バリエーションオブジェクト$variation_object上の任意のWC_ProductWC_Product_Variationメソッドを使用することができます。

この方法では、ユーザーが設定/更新するすべての複数必要なバリエーションの重量

コードは、あなたのアクティブな子テーマ(またはテーマ)の任意のPHPファイルに、あるいはまた任意のプラグインのPHPファイルになります。

このコードがテストされ、

WooCommerce 3.0+で動作します
関連する問題