以编程方式设定价格产品的Woocommerce问题

时间:2020-02-10 08:41:54

标签: wordpress woocommerce

我正在与Woocommerce一起为电子商务编写脚本,该脚本读取csv并设置产品的新价格/销售价格。 下面的变量是一个示例。

使用此代码,我可以设置产品的价格和销售价格,但是当销售数据过期时,价格不会恢复为正常价格。

$price = '40';
$sale_price = array();
$sale_price[1] = '20';
$sale_price[2] = '2020-02-10';
$sale_price[3] = '2020-02-11';

update_post_meta( $product_id, '_price', $price );
update_post_meta( $product_id, '_regular_price', $price);

if( !empty($sale_price) ){
    update_post_meta( $product_id, '_price', $sale_price[1] );
    update_post_meta( $product_id, '_sale_price', $sale_price[1] );
    update_post_meta( $product_id, '_sale_price_dates_from', $sale_price[2] );
    update_post_meta( $product_id, '_sale_price_dates_to', $sale_price[3] );
}

如果我未设置'_price'post_meta,则看不到正常价格或销售价格。

2 个答案:

答案 0 :(得分:4)

您应该这样做:

@JsonIgnoreProperties

答案 1 :(得分:0)

请尝试以下可能对您有帮助的代码

$price = '40';
$sale_price = array();
$sale_price[1] = '20';
$sale_price[2] = strtotime('2020-02-10');
$sale_price[3] = strtotime('2020-02-11');

update_post_meta( $product_id, '_price', $price );
update_post_meta( $product_id, '_regular_price', $price);

if( !empty($sale_price) ){
    update_post_meta( $product_id, '_price', $sale_price[1] );
    update_post_meta( $product_id, '_sale_price', $sale_price[1] );
    update_post_meta( $product_id, '_sale_price_dates_from', $sale_price[2] );
    update_post_meta( $product_id, '_sale_price_dates_to', $sale_price[3] );
}
相关问题