0

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.

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

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

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.

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.

0

I would guess it has to do with:

$order = commerce_cart_order_load($uid);

Since all anonymous users are assigned uid:0, it could be drawing from all products currently associated with that uid (which would be all anonymous users), rather then the products from the current user's session

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.