2017-08-13 8 views
3

すべての変動idと更新価格をループで取得する必要があります。Woocommerceで可変商品のすべての変動価格を更新する

(‘post_parent’ => $product->get_id()) 

またはこの:

($variation_ID = $variation->ID;). 
+0

作品:

次に、あなたの代わりにあなたのコードのこのカスタマイズされたバージョンを使用するように試みることができます関数myprefix_function_to_run()。この関数は、cron_schedulesフックを呼び出します。このデータは毎日更新されます。 –

+1

curencyのようなポストメタデータがあります。 1日に1回、製品価格は為替レートを使用して更新する必要があります。今私はちょうどsimle製品でそれが動作するすべての変動価格を更新しようとします。 –

答えて

1

あなたのコードまたはで最初に私はこれを働いていないと思います

$params = array(
    ‘posts_per_page’ => -1, 
    ‘post_type’ => ‘product_variation’, 
    ‘post_parent’ => $product->get_id() // tried $post-ID 
); 
$variations = get_posts($params); 
foreach ($variations as $variation) { 
    $variation_ID = $variation->ID; // tried $post-ID, $product->get_id() 
    $regular_price=34; 
    update_post_meta($variation_ID, ‘_regular_price’, (float)$regular_price); 
    update_post_meta($variation_ID, ‘_price’, (float)$regular_price); 
} 

: シンプルなクエリとループは次のようになります'に置き換えてください。 また$post-ID$post->ID

がどのようにこのコードを使用している依存に置き換える必要があります使用している場合、あなたはWP_Postオブジェクトを使用できるように最初のglobal $post;を含めるようにしてください$post

global $post; 

$regular_price = 13; 

// Only for product post type 
if($post->post_type == 'product') 
    $product = wc_get_product($post->ID); // An instance of the WC_Product object 

// Only for variable products 
if($product->is_type('variable')){ 

    foreach($product->get_available_variations() as $variation_values){ 
     $variation_id = $variation_values['variation_id']; // variation id 
     // Updating active price and regular price 
     update_post_meta($variation_id, '_regular_price', $regular_price); 
     update_post_meta($variation_id, '_price', $regular_price); 
     wc_delete_product_transients($variation_id); // Clear/refresh the variation cache 
    } 
    // Clear/refresh the variable product cache 
    wc_delete_product_transients($post->ID); 
} 

このコードはWooCommerceのバージョンでテストされ3+と私はのfuction にfuction.phpでこのコードを使用して

+1

あなたのコードは完全に動作します。ありがとうございました –

+0

@KatiaKovtun 'cron_schedules'フックに取り組んでいることを非常に嬉しく思っています...ありがとう。 – LoicTheAztec

関連する問題