How can I add qty for custom option before adding product to cart

How can I add qty for custom option before adding product to cart

September 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How can I add qty for custom option before adding product to cart

Customizing Product Options Quantity Before Adding to Cart

Introduction

When developing e-commerce platforms, providing dynamic product configuration options is crucial for enhancing user experience. One common requirement is allowing customers to specify the quantity of each individual option before adding the item to their cart.

In this post, we’ll explore how to achieve this functionality in a Magento environment using custom check boxes for product options and updating prices automatically on both the product page and checkout process.

Understanding the Current Setup

Before diving into the solution, let’s recap your current setup:

  1. You’ve managed to display check boxes for product options with corresponding quantities.
  2. The quantity fields are currently static and don’t update the price dynamically.

The provided code snippet is a starting point that displays each option selection along with its title, price, and a quantity input field:

<span class="label">
    <label for="bundle-option-<?php echo $_option->getId() ?>-<?php echo $_selection->getSelectionId() ?>" >
        <?php echo $this->getSelectionQtyTitlePrice($_selection) ?>
    </label>

    <input <?php if (!$_canChangeQty) echo '' ?> id="bundle-option-<?php echo $_option->getId() ?>-qty-input" class="input-text qty<?php if (!$_canChangeQty) echo '' ?>" type="text" name="bundle_option_qty[<?php echo $_option->getId() ?>]" value="<?php echo $_defaultQty ?>" onclick="bundle.changeSelection(this)"/>
</span>

Implementing Dynamic Quantity Update

Step 1: Modify the Product Options Display

To make the quantity fields dynamic, we need to update the JavaScript function bundle.changeSelection. This function should fetch the selected option’s price and update both the displayed price on the page and the hidden field that holds the total price.

function bundle.changeSelection(input) {
    var selectionId = input.name.match(/(\d+)$/)[1];
    var optionId = input.name.match(/(.*)\[(\d+)\]/)[2];

    // Fetch selected option's data via AJAX and update prices dynamically
    $.ajax({
        url: '/path/to/update-bundle-option',
        type: 'POST',
        dataType: 'json',
        data: {
            selection_id: selectionId,
            quantity: input.value,
            _token: $('input[name="_token"]').val()
        },
        success: function(response) {
            // Update displayed price and hidden total field
            $('#bundle-price-' + optionId).html(response.price);
            $('#bundle-total-price-' + optionId).val(response.total_price);

            // Recalculate the product's total price (if needed)
            bundle.recalculate();
        }
    });
}

Step 2: Create an AJAX Controller to Fetch Option Data

On the server side, create a new controller action that handles the AJAX request and returns the updated prices:

public function updateBundleOption()
{
    $selectionId = (int) $this->getRequest()->getParam('selection_id');
    $quantity = (int) $this->getRequest()->getParam('quantity');

    // Fetch selected option's data from the database or the product model
    $_option = /* Fetch selected option object */;
    $_selection = $_option->getSelectionById($selectionId);
    $_defaultQty = 1; // Default quantity for each selection

    // Calculate updated price based on the given quantity (using the product's price and tax settings)
    $updatedPrice = /* Calculate updated price using $_selection->getPrice() and other factors */;

    // Prepare response data
    $responseData = [
        'price' => Mage::helper('core')->currency($updatedPrice),
        'total_price' => Mage::helper('core')->currency($_option->getTotalPrice()),
    ];

    Mage::app()->getResponse()->setBody(Mage::helper('core')->jsonEncode($responseData));
}

Step 3: Update the Checkout Process

To ensure that the updated prices are carried over to the checkout, update the bundle_option_qty field in the cart item and recalculate the total price when adding items to the cart.

Conclusion

By implementing these changes, you’ll have a dynamic product configuration system where customers can specify quantities for each option before adding items to their cart. This approach enhances user experience by providing real-time pricing updates on both the product page and during the checkout process.

Remember to replace the placeholder code with actual implementation details based on your specific Magento version and customizations.

For more information on customizing Magento themes and functionality, visit Laravel Company for expert guidance and professional services.

Checkout Catalog Configuration Customers

Our network