Hi,

I am posting again because i feel that i wasn't clear enough on my last post.

I would like a little help in getting the "add to cart" link to add more than one of an item when it is clicked.

A little push in the right direction would be greatly appreciated in this matter.

I am using version 4.6.

Thanks in advance.

Comments

wpd’s picture

As far as I know, there is not a configuration setting to allow this. The cart software can accept a different quantity.

You need to change the add to cart link from:
http://www.example.com/cart/add/16
To:
http://www.example.com/cart/add/16/$qty
16 is the node/product id.

I think this is a needed feature, but it is more likely to get added to 4.7/cvs first... (Unless you do it yourself).

Scott

Erofee’s picture

Thanks Scott, that got me 50% of the way there. If i create a link like so: /product/add/13/5 it will add 5 of product 13. The next hurdle i need to get over is placing a text box next to the add link that can contain a qty to add. Does anyone have any suggestions?

wpd’s picture

Title: product.module add feature » product.module add multiple items at once
Category: support » feature

I would get rid of the link and add a text box and a submit button.
Then modify the cart code to look for the GET or POST variable.
Maybe try looking at the update cart code for an example.

Other:
-Actually modifing the text link is basically impossibly without using some javascript.
-I have not looked into this, but I assume that the text links will cause problems when bots spider your site. You will end up with a many
shopping carts filled with products.

I think this would be easy to do in 4.7. I am not as familiar with the 4.6 code. BTW, the feature is more likely to get into the 4.7 codebase than 4.6. I can help test with 4.7 version.

dgtlmoon’s picture

I dont know how drupal feels about this, i mean it works but it works by running some javascript set resets the form action, have a look..
this is product.module from current CVS (rc1)

function product_link($type, $node = null, $teaser = false) {
  $links = array();

  if ($type == 'node' && $node->ptype) {
    /* Determine whether or not the 'add to cart' link needs to be displayed. */
    $item = module_invoke('cart', 'get_items');
    if ($node->hide_cart_link == 0) {

      /* Right here we need to check if a given product type is in stock */
      if (module_invoke($node->ptype, 'productapi', $node, 'in_stock')) {
        // Is it already in our cart?
        if ($item[$node->nid]->qty) {
          $links[] = t('This item is in <a href="%cart_view">your shopping cart</a>.', array('%cart_view' => url('cart/view')));
        }
        else {
             $links[]='<script language="JavaScript">
                        function setQuantityFormAction(quantity) {
                            document.forms["Product_'.$node->nid.'"].action = "?q=cart/add/'.$node->nid.'/"+quantity;
                        }
                    </script>
                    <FORM METHOD=POST ACTION="?q=cart/add/$node->nid/1" NAME="Product_'.$node->nid.'">
                    <B>Quantity: </B><input type=text size=3 value=1 name=quantity >
                    <input type=submit value="Add to cart" ONCLICK="setQuantityFormAction(quantity.value)">
                    </FORM>
                    ';

        }
      }
      else {
        $links[] = t('sold out');
      }
    }
  }

  return $links;
}

Erofee’s picture

Thanks for the code snippet. This is absolutely brilliant and exactly what i was looking for. Javascript is not my forte and this has helped a lot. There is just one small problem which i think can be fixed relatively easily but i am not sure how to do it. When you have multiple products on one page, you get an error when trying to add one of them to the cart. I think that this issue can be resolved by uniquely naming each input type, ie call the input type [nid]quantity. Do you know how i can do this?

Many thanks,
Erofee.

neclimdul’s picture

that's pretty much a hack around the cart adding page. one line there to check for a posted quantity would be easier and less messy.

Erofee’s picture

neclimdul, are you referring to my reply?

neclimdul’s picture

Not really, sorry. I was refering to the javascript. It doesn't work on browsers with javascript turned off. So it isn't a very permanent solution.

sime’s picture

Version: 4.6.x-1.x-dev » master
Component: product.module » product

Pushing to HEAD. Not sure it's on the right track, but there is history there.

neclimdul’s picture

Status: Active » Closed (won't fix)

I'm going to say this is really a wont fix. You can do this on a per product level by disabling the add to cart link though the product api and then having a form on your item page. the cart add page should even accept posted quantity values so something like

$form['qty'] = array('#type' => 'textbox', '#title' => 'quantity');
$form['submit'] =array('#type' => 'submit', '#value' => 'Add to cart');
$form['#action'] = url("cart/add/$node->nid");

should work.