2017-04-06 140 views
1

我製作了可變類型的自定義導入產品。然後我創建了一個文件來更新變化的產品價格。我正在使用update_post_meta方法。這些值出現在每個變體字段的編輯產品頁面中,但似乎並未在產品的首頁中更新價格。更新變體產品價格 - 在產品頁面中不可見 - Woocommerce

我必須在管理面板中更新產品(通過單擊更新按鈕)以使用新的價格。

我試過使用$ product-> variable_product_sync();但它似乎沒有工作。有任何想法嗎?

樣品我的代碼:

foreach ($variations as $variationProduct) { 
    $variationProductId = $variationProduct["variation_id"]; 
    $productPrice = number_format($productPrice, 2, '.', ''); 
    update_post_meta($variationProductId, '_regular_price', $productPrice); 
} 

任何幫助或解決方案?

回答

1

解決!最後我通過woocommerce API找到它。如果您使用的是woocommerce 2.7或更新版本,則可以使用以下行:

$product->save(); 
0

請使用以下腳本更新變化價格。點擊這裏獲取完整的代碼。 https://www.pearlbells.co.uk/bulk-update-product-variation-price-woocommerce/

function getExistingProducts($updatedPrices,$skuArray) { 

$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1)); 

while ($loop->have_posts()) : $loop->the_post(); 

    $id = get_the_ID(); 
    $product = wc_get_product($id); 
    $sku = get_post_meta($id, '_sku', true); 

    if(in_array($sku, $skuArray )) { 

     $attributes = $product->get_attributes(); 
     $attributes['medium-quantity-price']['value'] = $updatedPrices[$sku][4]; 
     $attributes['low-quantity-price']['value'] = $updatedPrices[$sku][3]; 
    $attributes['v-low-quantity-price']['value'] = $updatedPrices[$sku][2]; 
     update_post_meta($id,'_product_attributes',$attributes); 
     echo ' Update Sku : '.$sku.' '.PHP_EOL; 

    } 

endwhile; 

} 
相關問題