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')),
);
}
}
Explanation:
- Form Alter Hook:
- The
custom_module_form_alter
function is used to alter thecommerce_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.
- The
- Hidden Product ID Field:
- The hidden field
buy_now_pid
stores the product ID which will be used during the form submission.
- The hidden field
- 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
.
- The "Buy Now" button is added to the form with the label
- 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.
- The
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.