If you want to add a “Buy Now” button and an “Add To Cart” button to the Drupal eCommerce site, follow the code below.
function custom_module_form_alter(&$form, &$form_state, $form_id) { if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== false) { if (isset($form['product_id']['#value'])) { $product_id = $form['product_id']['#value']; } else { $product_id = $form['product_id']['#default_value']; } $form['buy_now_pid'] = array( '#type' => 'hidden', '#name' => 'buy_now_pid', '#default_value' => $product_id, ); $form['buy_now_submit'] = array( '#type' => 'submit', '#name' => 'buy_now', '#value' => t('Buy Now'), '#submit' => array('custom_module_buy_now_call_back'), '#weight' => '52', '#attributes' => array('class' => array('btn-buy-now')), ); } }
function custom_module_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== false) {
if (isset($form['product_id']['#value'])) {
$product_id = $form['product_id']['#value'];
}
else {
$product_id = $form['product_id']['#default_value'];
}
$form['buy_now_pid'] = array(
'#type' => 'hidden',
'#name' => 'buy_now_pid',
'#default_value' => $product_id,
);
$form['buy_now_submit'] = array(
'#type' => 'submit',
'#name' => 'buy_now',
'#value' => t('Buy Now'),
'#submit' => array('custom_module_buy_now_call_back'),
'#weight' => '52',
'#attributes' => array('class' => array('btn-buy-now')),
);
}
}

Callback function to add product to cart and redirect to checkout. Callback function also handles custom functionalities while adding product to cart.
Sreyas offer the best Drupal development service with our most experienced team, which will greatly satisfy our client all over the world.
Leave a Reply