亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WooCommerce 更新所有變體(以編程方式)

WooCommerce 更新所有變體(以編程方式)

PHP
Helenr 2023-04-15 20:18:39
我正在嘗試在所有產品的每個變體下更新 WooCommerce 中的自定義字段。我獲得了適用于簡單產品的代碼,但無法遍歷變體并更新它們。我在 Stackoverflow 上嘗試了幾乎所有 WooCommerce 變體循環,但它們都返回錯誤或根本不起作用。任何建議將不勝感激。functions.php 中用于簡單產品的當前代碼。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貢獻1802條經驗 獲得超5個贊

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 );

    }

}

這將獲得所有變化。


查看完整回答
反對 回復 2023-04-15
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號