我正在嘗試在所有產(chǎn)品的每個(gè)變體下更新 WooCommerce 中的自定義字段。我獲得了適用于簡單產(chǎn)品的代碼,但無法遍歷變體并更新它們。我在 Stackoverflow 上嘗試了幾乎所有 WooCommerce 變體循環(huán),但它們都返回錯(cuò)誤或根本不起作用。任何建議將不勝感激。functions.php 中用于簡單產(chǎn)品的當(dāng)前代碼。add_action('init', 'bulk_update_post_meta_data'); function bulk_update_post_meta_data() { $args = array( 'posts_per_page' => -1, 'post_type' => 'product', 'suppress_filters' => true ); $posts_array = get_posts( $args ); foreach($posts_array as $post_array) { $Cogcost = get_post_meta( $post_array->ID, '_regular_price', true ); update_post_meta($post_array->ID, '_wc_cog_cost', $Cogcost); } }
1 回答

12345678_0001
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
add_action( 'init', 'bulk_update_post_meta_data' );
function bulk_update_post_meta_data() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'product_variation',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
foreach ( $posts_array as $post_array ) {
$Cogcost = get_post_meta( $post_array->ID, '_regular_price', true );
update_post_meta( $post_array->ID, '_wc_cog_cost', $Cogcost );
}
}
這將獲得所有變化。
- 1 回答
- 0 關(guān)注
- 115 瀏覽
添加回答
舉報(bào)
0/150
提交
取消