我想通过代码更新woocommerce可变产品价格。我在这里写了一些代码,它创建产品,但不更新可变价格:
$post = array(
\'post_title\' => \'Product Title\',
\'post_content\' => \'\',
\'post_status\' => \'publish\',
\'post_type\' => "product"
);
$new_post_id = wp_insert_post( $post );
wp_set_object_terms ($new_post_id,\'variable\',\'product_type\');
/**
* Add product attribute.
*/
$attr_names = array(
\'Length\' => array( \'12\', \'13\',\'14\',\'15\' )
);
$attr_data = array();
foreach ( $attr_names as $attr_name => $attr_values ) {
$attr_sanitized_name = \'pa_\' . sanitize_title( $attr_name );
$attr_data += array(
$attr_sanitized_name => array(
\'name\' => $attr_name,
\'value\' => implode( \'|\', $attr_values ),
\'is_visible\' => 1,
\'is_variation\' => 1,
\'is_taxonomy\' => 0,
\'position\' => 0,
)
);
}
$variation_id = update_post_meta( $new_post_id, \'_product_attributes\', $attr_data, TRUE );
update_post_meta($variation_id, \'_regular_price\', \'100\');