【发布时间】:2015-05-06 03:39:29
【问题描述】:
我正在使用 drupal 商务模块,我遇到了匿名用户的问题,当用户单击添加到购物车按钮时,购物车显示其他匿名用户添加的额外商品。
我正在使用自定义代码使用 ajax 添加到购物车。以下是代码。
// Add a product to cart on ajax call.
function mymodule_custom_add_to_cart($product_id,$uid){
$line_item = commerce_product_line_item_new(commerce_product_load($product_id));
commerce_cart_product_add($uid, $line_item);
$order = commerce_cart_order_load($uid);
commerce_cart_order_refresh($order);
// loads data array from order object
$data = mymoudle_custom_cart_load_all_variables($order->order_id);
$jsonencoded = json_encode($data);
print $jsonencoded;
}
我不知道为什么所有匿名用户都在购物车中获得相同的产品。
请帮我找出问题。
更新 1:
我已将匿名用户的代码更改如下,然后出现错误:EntityMetadataWrapperException:由于未设置父数据结构,无法获取数据属性数据。在 EntityStructureWrapper->getPropertyValue() 中(/entity.wrapper.inc 的第 451 行
更改代码
// Add a product to cart on ajax call.
function mymodule_custom_add_to_cart($product_id,$uid){
if($uid == 0){
$order_id = commerce_cart_order_id($uid);
if($order_id == false){
$order = commerce_cart_order_new(0, 'checkout_checkout');
} else {
$order = commerce_order_load($order_id);
}
// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
commerce_line_item_save($line_item);
// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
// Save the order again to update its line item reference field.
commerce_order_save($order);
// loads data array from order object
$data = mymoudle_custom_cart_load_all_variables($order->order_id);
$jsonencoded = json_encode($data);
print $jsonencoded;
}else{
$line_item = commerce_product_line_item_new(commerce_product_load($product_id));
commerce_cart_product_add($uid, $line_item);
$order = commerce_cart_order_load($uid);
commerce_cart_order_refresh($order);
// loads data array from order object
$data = mymoudle_custom_cart_load_all_variables($order->order_id);
$jsonencoded = json_encode($data);
print $jsonencoded;
}
}
更新 2: 问题已解决。
网站正在被 googlebot 抓取,它点击 addtocart 链接并自动将产品添加到匿名用户帐户,所以我删除了 addtocart 按钮的链接并使用了 commerce addtocart 表单。
【问题讨论】: