// Get an instance of the WC_Product object
$product = wc_get_product( $product_id );
$regular_price = 500; // Define the regular price
$sale_price = 465; // Define the sale price (optional)
// Set product sale price
if ( isset($sale_price) && ! empty($sale_price) ) {
$product->set_sale_price($sale_price);
$product->set_price($sale_price); // Set active price with sale price
} else {
$product->set_price($regular_price); // Set active price with regular price
}
// Set product regular price
$product->set_regular_price($regular_price);
// Sync data, refresh caches and saved data to the database
$product->save();
1条答案
按热度按时间dwbf0jvd1#
你实际上是在使用旧的方式来设定产品价格.有两种方法:
A)使用这种旧方法,
1.要仅设置正常价格,您应该使用:用途:
字符串
1.要设置销售价格和正常价格,您应该使用:用途:
型
B)新的方式 (自WooCommerce 3):
由于WooCommerce正在迁移到自定义表,以及其他原因,最好使用所有可用的WC_Product setter方法。
对于产品价格,您将使用以下信息:
型