更新woocommerce产品字段,如sku,尺寸,价格,名称,重量?

时间:2020-02-25 11:35:17

标签: php wordpress woocommerce

我需要更新woocommerce产品sku,尺寸,价格,名称,重量。

我使用下面的代码进行一些调整,效果很好。例如价格,但不适用于名称,重量等...

update_post_meta($product->get_id(), '_name', "New Product")
update_post_meta($product->get_id(), '_name', "New Product");
update_post_meta($product->get_id(), '_size', "New Product");
update_post_meta($product->get_id(), '_weight', "New Product");

有没有可以同时更改产品属性和字段的替代方法?

1 个答案:

答案 0 :(得分:1)

post_meta不包含_name

请改用以下功能:wp_update_post()

一个例子

post_id = $product->get_id();

$my_post = array(
   'ID' =>  $post_id,
   'post_title' => 'my title',
   'post_name' => 'my name',
   'post_status' => 'publish'
);   
wp_update_post( $my_post );

update_post_meta( $post_id, '_sku', 1234 );
update_post_meta( $post_id, '_weight', 5 );