匿名用户在购物车中获得相同的产品

时间:2021-09-07 20:07:20

I am using drupal commerce module, i have an issue with anonymous users, when users click on add to cart button, cart is showing extra items which are added by other anonymous users.

我使用drupal商务模块,我有匿名用户的问题,当用户点击添加到购物车按钮,购物车显示由其他匿名用户添加的额外项目。

I am using custom code for add to cart using ajax. following is the code.

我使用自定义代码使用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;
    }

I dont know why all anonymous users are getting same products in there cart.

我不知道为什么所有匿名用户都在购物车中获得相同的产品。

Please help me to find out the issue.

请帮我找出问题所在。

UPDATE 1:

I have changed the code for anonymous user as following, then i am getting an error: EntityMetadataWrapperException: Unable to get the data property data as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 451 of /entity.wrapper.inc

我已经更改了匿名用户的代码如下,然后我收到一个错误:EntityMetadataWrapperException:无法获取数据属性数据,因为未设置父数据结构。在EntityStructureWrapper-> getPropertyValue()中(/ entity.wrapper.inc的第451行)

CHANGED CODE

    // 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;
        }
    }

UPDATE 2: ISSUE RESOLVED.

更新2:问题已解决。

Site is crawling by googlebot, which is clicking addtocart link and its adding an product to anonymous user account automatically, so i removed link for addtocart button and used commerce addtocart form.

网站正在通过googlebot抓取,点击addtocart链接并自动将产品添加到匿名用户帐户,因此我删除了addtocart按钮的链接并使用了商务addtocart表单。

1 个解决方案

#1


All anonymous users have uid zero (see documentation). That is the reason why all the items added by different anonymous users are being added to the same shopping cart.

所有匿名用户的uid为零(参见文档)。这就是为什么不同匿名用户添加的所有项目都被添加到同一购物车的原因。

#1


All anonymous users have uid zero (see documentation). That is the reason why all the items added by different anonymous users are being added to the same shopping cart.

所有匿名用户的uid为零(参见文档)。这就是为什么不同匿名用户添加的所有项目都被添加到同一购物车的原因。

相关文章