This may be some simple question, How to show the Add to cart button on the custom page, I have listed the products in the custom page by programmatically, but displaying the "Add to Cart" button makes problem,

As I have displays the cart button using the below code,

  $line_item = commerce_product_line_item_new($productinfo);
  $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
  $line_item->data['context']['product_ids'] = $productid;
  $line_item->quantity = 1;
  $qty = 1;
  $form_id = commerce_cart_add_to_cart_form_id(array($productid), $qty);
  $addtocart_form = drupal_get_form($form_id, $line_item);
  print render($addtocart_form);

it shows the Add to cart button but while clicking this, I got the below error,

Notice: Undefined index: quantity in commerce_cart_add_to_cart_form_validate() (line 2088 of \commerce\modules\cart\commerce_cart.module).
You must specify a valid quantity to add to the cart.
Notice: Undefined index: #datatype in commerce_cart_add_to_cart_form_validate() (line 2094 of \commerce\modules\cart\commerce_cart.module).
Notice: Undefined index: line_item_fields in commerce_cart_add_to_cart_form_validate() (line 2107 of \commerce\modules\cart\commerce_cart.module).
Warning: array_merge() [function.array-merge]: Argument #1 is not an array in field_default_extract_form_values() (line 36 of \modules\field\field.default.inc).
Recoverable fatal error: Argument 2 passed to drupal_array_get_nested_value() must be an array, null given, called in \modules\field\field.default.inc on line 38 and defined in drupal_array_get_nested_value() (line 6811 of \includes\common.inc)

I think this error comes because of quantity field not in the add to cart form, how to add the quantity field in the add to cart form,

I am using drupal commerce,

what is wrong in my code?

thanks in advance.

  • As far as I remember you can use 'Add to cart form' as a formatter in your product's display - isn't it much easier? (Or I'm forgettingg something)
    – rreiss
    Apr 17, 2014 at 5:02
  • i am listing this in home page,now i have no idea how to show addtocart button.., i am new to drupal commerce, can u help to show addtocart button
    – Helphin
    Apr 17, 2014 at 11:44


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.