0

I have built a custom module for my D7 site that shows a dialogue when the user opens the cart. In the dialogue a few more items are suggested to the user. If the user chooses to tick the box in front of such items, and then click a submit button, the items are added to the cart with the help of an axios request.

This is the axios part in the front end (the variable products holds the SKUs of the chosen items in an array):

  axios.post("/add/related_product", {
    products: products,
  }).then(function (response) {
    if(response.data === 'READY'){
      setTimeout(function() {
        window.location.reload();
      }, 200);
    }
  });

In the backend I have the following callback function, that handles the post request:

// callback to add selected products progammatically to cart
function _related_products_ajax(){
  global $user;
  // If the user is anonymous, force a session start.
  if(!$user->uid > 0) {
    drupal_session_start();
  }
  $order = commerce_cart_order_load($user->uid);
  $order_id = $order->order_id;
  //recieve id's for selected products
  $items = json_decode(file_get_contents("php://input"), $assoc = true);
  if($items['products']){
    foreach($items['products'] as $sku){
      if ($product = commerce_product_load_by_sku($sku)) {
        $line_item = commerce_product_line_item_new($product, 1, $order_id, $data = array(), $type = 'product');
        drupal_alter('commerce_product_calculate_sell_price_line_item', $line_item);
        rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
        $line_item = commerce_cart_product_add($user->uid, $line_item, TRUE);
        commerce_cart_order_session_save($order_id, $completed = FALSE);
        commerce_order_save($order);
      }
    }
    drupal_json_output('READY');
  }else{
    drupal_json_output('FAILED');
  }
}

When adding products to the cart the normal way with the add-to-cart button on product pages, the item in question is always added to the cart, no exceptions.

One obvious cause would be the axios way of using "promises", and that the callback function above sometimes don't get called. But since the Drupal message "Item successfully added to cart" is shown after the page is reloaded, I guess that is proof that the backend is actually called and that the promise is fulfilled. Also, the first argument after then() in the axios snippet wouldn't be called if the promise wasn't fulfilled.

Is there anyone out there that have an idea why items sometimes don't get added with my custom module? It can happen both for logged in and for anonymous users. It seems to me that it's more likely to happen the first time a new user attempts to do this (but not sure about that).

  • You're doing a lot of extra work. See this answer for how to add products to a cart. drupal.stackexchange.com/questions/105627/…. – Charles Bamford Jan 2 at 18:25
  • Thanks for commenting. I began using that answer just using 3 lines for adding items and saving the order. But since sometimes an item is is not added, I have added some extra lines (without luck). Would be nice to understand the reason why. – TBJ Jan 3 at 7:04
  • So if the extra lines aren't helping, why include them in the question? It takes additional time for anyone who comes to help with your problem to read them and try to figure out what you're doing. As to what is causing the issue, my guess is that the axios stuff isn't even reaching your callback. Try setting a breakpoint in the callback, or if you don't have debugging, setting a watchdog. – Charles Bamford Jan 3 at 19:01
  • You are right, I didn't think of removing lines that isn't strictly necessary. Sorry about that. But as I wrote in my question, if the axios request doesn't reach my callback, it's weird that the message "successfully added in cart" shows. Also, since the page actually does reload, the response from the callback must be READY, which should be proof that the callback is reached. Debugging is hard because the error does not occur often. I will try to set a watchdog, thanks again for your time. – TBJ Jan 7 at 7:57

Your Answer

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

Browse other questions tagged or ask your own question.