Drupal 7 在购物车中获取相同产品项目的匿名用户
我正在使用drupal商务模块,我对匿名用户有一个问题,当用户单击“添加到购物车”按钮时,购物车显示其他匿名用户添加的额外项目 我使用自定义代码添加到购物车使用ajax。下面是代码Drupal 7 在购物车中获取相同产品项目的匿名用户,drupal-7,drupal-commerce,Drupal 7,Drupal Commerce,我正在使用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(comm
// 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:无法获取数据属性数据,因为未设置父数据结构。在/entity.wrapper.inc的EntityStructureWrapper->getPropertyValue行451中
更改代码
更新2:问题已解决
谷歌机器人正在抓取网站,它正在点击addtocart链接,并自动将产品添加到匿名用户帐户,所以我删除了addtocart按钮的链接,并使用了commerce addtocart表单。所有匿名用户都没有uid,请参见。这就是为什么不同匿名用户添加的所有项目都被添加到同一个购物车的原因
// 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;
}
}