Best web application development company. Best web designing company. Best e-commerce, designing, development, migration services all over European countries and worldwide.

How to add a “Buy Now” button along with the “Add To Cart” button in Drupal Commerce ?

If you want to add a “Buy Now” button and an “Add To Cart” button to your Drupal eCommerce site, follow the code below. This implementation modifies the add-to-cart form to include a “Buy Now” button that directs the user straight to the checkout process.

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')),
);
}
}
Add Now and Add cart button Drupal ecommerce

Explanation:

  1. Form Alter Hook:
    • The custom_module_form_alter function is used to alter the commerce_cart_add_to_cart_form.
    • It checks if the form ID contains commerce_cart_add_to_cart_form and then adds a hidden field for the product ID and a "Buy Now" button.
  2. Hidden Product ID Field:
    • The hidden field buy_now_pid stores the product ID which will be used during the form submission.
  3. Buy Now Button:
    • The "Buy Now" button is added to the form with the label t('Buy Now').
    • It uses a custom submit callback function custom_module_buy_now_call_back.
  4. Callback Function:
    • The custom_module_buy_now_call_back function handles adding the product to the cart and redirecting the user to the checkout page.

Practical Applications:

  • Enhanced User Experience: The "Buy Now" button provides a direct path to purchase, reducing steps and improving conversion rates.
  • Custom Workflows: At the checkout process, additional functionalities, such as promotions or discounts, are added when the 'Buy Now' button is clicked.

Additional Notes:

  • Error Handling: In the callback function Error Handling can be implemented, it manages scenarios where the product ID is invalid or the user is not logged in.
  • Customization: At Sreyas, our expert Drupal developers can customize this solution to fit specific client requirements, including styling adjustments, additional fields, or unique checkout processes.
  • Security Considerations: Ensure that the implementation is secure, especially when handling user input and form submissions. Sanitize and validate all inputs appropriately.

Sreyas Drupal Development Services:

By using this approach, you can effectively add "Buy Now" and "Add To Cart" buttons to your Drupal eCommerce site, enhancing the user experience and streamlining the purchasing process. For any customizations or further assistance, reach out to our expert developers at Sreyas. Sreyas offers the best Drupal development services with our most experienced team. We tailor our solutions to meet the unique needs of our clients, ensuring satisfaction and high-quality results. Whether you need custom eCommerce functionality, module development, or any other Drupal-related services, our team is here to assist you.

Recent Blogs


Posted

in

by

Tags:

To Know Us Better

Browse through our work.

Explore The Technology Used

Learn about the cutting-edge technology and techniques we use to create innovative software solutions.