I use this code to get the form with button 'Add to cart'.

$price_arr = commerce_product_calculate_sell_price($product);
$price = commerce_currency_format($price_arr['amount'], $price_arr['currency_code'], $product);

$cart_form_id = commerce_cart_add_to_cart_form_id(array($product->product_id));
$line_item = commerce_product_line_item_new($product);
$line_item->data['context']['product_ids'] = array($product->product_id);

$cart_form_arr = drupal_get_form($cart_form_id, $line_item);
$cart_form_arr['#action'] = '/' . $lang . '/gallery';

$cart_form_html = drupal_render($cart_form_arr);

When I do

return $cart_form_html;

there is no problem. This code works in the back-end.

I have an ajax callback (a Drupal menu link) - '/get-product-button/ajax/[productID]'. This callback returns a value through

drupal_json_output($cart_form_html);

(the same variable as in the back-end) which I print using JS.

This returned value contains an HTML code with a button, but it contains another form token, and this code does not work (it does not add to cart anything, but reloads the page).

Any idea?

Comments

ArtuDrop created an issue. See original summary.

ArtuDrop’s picture

Issue summary: View changes
subhojit777’s picture

I am also doing the same in this module. Can you refer the code in the module.

subhojit777’s picture

Status: Active » Closed (won't fix)

Maybe you can use ajax commands to render the form.