Magento 1.9 – Stop ‘Please Match the Requested Format: Qty.’ Error in Shopping Cart

magento-1.9shopping-cart-price-rules

Google searches for both "Magento please match the requested format: Qty" and "magento decimal quantites not updating in cart" have turned up nothing useful me.

Hi, I'm using CE 1.9.2.0 w/ patch 6482 installed. My store is early in development and has no themes or extensions whatsoever. I have items in my store with the following inventory attribute : value pairs:

Minimum Qty Allowed in Shopping Cart : .25

Qty Uses Decimals : Yes

Enable Qty Increments : Yes

Qty Increments : .25

I am able to add quantities to the cart if they are a multiple of .25 right from the product detail page. Magento does complain in the form of highlighting the quantity field in red, but the quantity is added to the cart nonetheless. If I do nothing else, I can proceed to checkout at this point without issue.

However, from the shopping cart, I cannot change an integer quantity to a decimal, nor can I change an integer quantity to a different integer quantity while another product's quantity is a decimal, nor can I change a decimal quantity to a different decimal quantity. I can change a decimal quantity to an integer quantity, whether or not other decimal quantities are in the cart.

Magento displays a "Please match the requested format: Qty." message below the quantity field when these violations are detected.

How can I get the form validation for the quantity field to behave as expected?

Best Answer

Welcome to Magento.SE!

Ah yes. This is wonderful. Magento supports fractional quantities, but the RWD reference theme (the theme that ships with Magento) does not:

enter image description here

In order to handle this you'll need to edit the input field which contains the validation requirements.

In app/design/frontend/rwd/default/template/checkout/cart/item/default.phtml:

    <input type="text" pattern="\d*" name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4"
           data-cart-item-id="<?php echo $this->jsQuoteEscape($_item->getSku()) ?>"
           title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" maxlength="12" />

The important part is pattern="\d*" - this is a regular expression that Magento parses to validate that you've input the right value.

For a decimal value you have a couple choices:

\d+(\.\d{1,2})? allows optional decimals

  • Accepts 1
  • Accepts 1.2
  • Accepts 1.23
  • Rejects 1.002

\d+(\.\d{2}) always enforces two decimal places

  • Rejects 1
  • Rejects 1.2
  • Accepts 1.23
  • Rejects 1.002

Hope that helps!

Related Topic