我在wordpress主题中使用ajax将产品添加到购物车。这是php函数:
function updateCart(){
$cart = array();
$prodID = "";
$response = array();
$cookie_name = 'cart';
if(isset($_REQUEST['prodID'])){
$prodID = $_REQUEST['prodID'];
}else{
$prodID = false;
}
if(isset($_COOKIE['cart'])){
$cart = json_decode( stripcslashes($_COOKIE[$cookie_name]), true);
array_push($cart, $prodID);
if(setcookie( $cookie_name, json_encode($cart, true), time() + (24 * 60 * 60), COOKIEPATH, COOKIE_DOMAIN )){
$response['cookie'] = 'cart_exist';
wp_send_json_success( $response );
}else{
wp_send_json_error( );
}
}else{
if($prodID){
array_push($cart, $prodID);
}
if(setcookie( $cookie_name, json_encode($cart, true), time() + (24 * 60 * 60))){
$response['cookie'] = 'cart_not_exist';
$response['cart'] = $_COOKIE['cart'];
wp_send_json_success( $response );
}else{
wp_send_json_error( );
}
}
}
页面重新加载后,我看不到并访问$ _COOKIE ['cart']。在我的网站的桌面版本上,此代码非常完美。这只发生在移动版本上(我在移动网站上使用了另一种版本的wordpress主题)。