我正在处理woocommerce中的儿童主题。在一个函数中, 我尝试使用默认的get_total_sales()和set_total_sales()来增加总销售额并保存从AJAX调用时(单击按钮下载时)的销售额。
function download_count_callback(){
//get product id from AJAX POST
$productid = $_POST['productid'];
//get product by id
$product = wc_get_product( $productid );
//get the total sales number
$downloadcount = $product->get_total_sales();
error_log ("NUMBER was : ".$downloadcount );
//add 1 to total sales
$downloadcount += 1;
error_log ("NUMBER should become : ".$downloadcount );
//get the renewed total sales number
$product->set_total_sales($downloadcount);
error_log ("NUMBER NOW : ".$product->get_total_sales());
wp_die();
}
服务器日志如下所示:
[23-Feb-2020 12:44:44 UTC] NUMBER was : 0
[23-Feb-2020 12:44:44 UTC] NUMBER should become : 1
[23-Feb-2020 12:44:44 UTC] NUMBER NOW : 1
看起来不错, 但没有…下载计数未保存到数据库中。当我刷新页面时,
get_total_sales()将返回到0。
这有什么问题吗?
#1
这有什么不同吗?
function download_count_callback(){
//get product id from AJAX POST
$productid = $_POST['productid'];
//get product by id
$product = wc_get_product( $productid );
//get the total sales number
$downloadcount = $product->get_total_sales();
error_log ("NUMBER was : ".$downloadcount );
//add 1 to total sales
$downloadcount += 1;
error_log ("NUMBER should become : ".$downloadcount );
//get the renewed total sales number
$product->set_total_sales($downloadcount);
$product->save();
error_log ("NUMBER NOW : ".$product->get_total_sales());
wp_die();
}
来源:
https://www.srcmini02.com/64164.html