Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

How to add custom qty field to custom option of simple product magento

By default in magento there is no qty field of simple product for custom option. If you want to add qty as per your custom option you can add now using this post.

Suppose you have custom option 'Color: Red, Blue, White' of your simple product and you want to add different qty option for your color option you can do it now.

magento custom options quantity

1. go to app\design\adminhtml\default\default\template\catalog\product\edit\options\type\select.phtml

Find the below code:
'<th class="type-sku"><?php echo Mage::helper('catalog')->__('SKU') ?></th>'+

Now add following code just after above code.
'<th class="type-qty"><?php echo Mage::helper('catalog')->__('Qty') ?></th>'+

Find the below code:
'<td><input type="text" class="input-text" name="product[options][{{id}}][values][{{select_id}}][sku]" value="{{sku}}"></td>'+

Now add following code just after above code.
'<td><input type="text" class="input-text" name="product[options][{{id}}][values][{{select_id}}][qty]" value="{{qty}}"></td>'+

2. go to app\code\core\Mage\Adminhtml\Block\Catalog\Product\Edit\Tab\Options\Option.php

Find the below code:
$value['sku'] = $this->htmlEscape($option->getSku());

Now add following code just after above code.
$value['qty'] = $this->htmlEscape($option->getQty());

Find the below code:
'sku' => $this->htmlEscape($_value->getSku()),

Now add following code just after above code.
'qty' => $this->htmlEscape($_value->getQty()),

3. Add field in “catalog_product_option_type_value” table
`qty` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Qty',

Now clear your cache and check in you magento admin. You can hit below if post is help full for you.

Please support us, Like us on Facebook.

  1. Works fantastic.

    However might need a fix as csv files do not have the 'qty'. instead the values seems to go under '_custom_option_row_sku' in the csv file.

    ReplyDelete
  2. Nice! But....
    is it combine the calculation?
    I mean lets say we have 4 t-shirts in stock and customer wants to buy two black
    both in XL size.
    The stock has the following combination: 2 red XL; 1 black XL; 1 black XXL;
    The above code suggests me that it's possible to the customer selecting two black
    XL t-shirts, however you have only one in stock, because the simple product qty is
    4, the size option qty for XL is 3, the color option qty for black is 2.

    In short, I don't see the connection in your code between the different options.
    Or do I miss something?

    ReplyDelete
    Replies
    1. You need to add some code to check this all.

      Delete
  3. It's working in the admin, but I can place orders with a higer QTY then what is have added in the admin.

    ReplyDelete
    Replies
    1. For that you need to make some validation before adding item in the cart. You can validate this on your product details page.

      Delete
  4. Hi. Where do I find catalog_product_option_type_value ?
    Thanks

    ReplyDelete
    Replies
    1. Open your database and find catalog_product_option_type_value table in it.

      Delete
  5. Hi
    Do you have sample code to validate the quantity before adding item to the cart?

    Thanks
    Your help is highly appreciated

    ReplyDelete
  6. hi
    how can i maintain stock for custom option for configurable product.

    please i am stuck with manage stock for packages

    please help me out

    contact me swzvikas@gmail.com

    ReplyDelete
  7. Helo I am having trouble : In admin a new field qty is added but when i pdate it , it is not saved. Please help

    ReplyDelete
    Replies
    1. May i know which magento version you are using? and please make sure you have added all steps.

      Delete
  8. hi,

    Your blog helped me to bring the qty option in custom option.

    If you have the sample code to validate the qty in the front end, pls share it.

    Help needed urgently

    ReplyDelete
  9. how to add price during option change . add price to ad to cart

    ReplyDelete
  10. No sense how it will WORK on frontend store? price * qty in basket?

    ReplyDelete
  11. Hi,
    Qty text box can see in the admin panel. But when I save the qty is always set to zero, that means not saving.
    Also how can i display how many qty is exist in the that custom option drop down box ?

    like color 1 => 10 qty like that.

    Im using Magento ver. 1.9.0.1.

    Thanks

    ReplyDelete
  12. Hi,
    Thanks for the code update. I have successfully added the qty code, but but when customer purchased the product but qty is not reducing(Size M has 3 after the purchase 2 M products still the Size M has 3). Let me how to make this work

    ReplyDelete
  13. Hi,
    Thanks for the code. When we buy a product of size M 2 out of 4, but still the quantity didn't reduced it was still 4. Kindly help to fix this issue. (I am Using Magento 1.9.2)

    ReplyDelete
    Replies
    1. Hi Ramesh,
      You need to add observer event to change option quantity after order is placed.

      Thanks

      Delete
  14. Dear Author,

    Can you guide me to Add Observer event for this operation,

    Thank You.

    ReplyDelete
  15. what code i have to add for quantity validation in product detail page please share with me.

    ReplyDelete
  16. Hardik g can you provide the solution with validation code and stock

    ReplyDelete
  17. Asslam O Alikum, Nice work bro great article keep it up.
    Me face miner problem when i add back-end product custom quantity, when i add to cart product custom quantity not decrease from back-end. Please help me.
    http://www.rivaj-uk.com

    ReplyDelete
    Replies
    1. Hello,

      You need to use observer to decrease qty when you add product to cart.

      Delete
    2. What is observer n where i can apply?

      Delete
    3. I think you are not aware with magento code customization.
      Read this answer for how to create observer event in magento https://magento.stackexchange.com/a/41280

      For further discussion you can contact me via contact us page.
      https://hkpatel201.blogspot.in/p/contact-us.html

      Thanks,

      Delete
  18. hi hardik, how to Change weight based on custom option. I did'nt find any solution even in stackoverflow.
    I need weight field in custom option so that shipping will calculate on the basis of weight of custom option.

    ReplyDelete
  19. I have create the custom field weight & weight is successfully added to DB. but now i want to replace the general weight attribute value to selected custom weight value so that correct shipping will calculate on the basis of weight of custom option.

    for that i have create observer.

    getQuoteItem();
    $product = $item->getProduct();
    $productOptions = $product->getTypeInstance(true)->getOrderOptions($product);
    $weight = '';
    foreach ($productOptions['options'] as $key => $value) {

    $product = Mage::getModel("catalog/product")->load($product->getId());

    foreach ($product->getOptions() as $o) {

    $values = $o->getValues();

    foreach ($values as $v) {

    if ($value['option_value'] == $v->getOptionTypeId()) {
    $weight = $v->getWeight();

    }

    }
    }
    }
    if(!empty($weight)){
    return $item->getProduct()->setWeight($weight);
    }else{
    return;
    }
    }
    }

    //echo $weight; display the selected weight in the observer

    & I am getting the selected custom field weight but when check the shopping cart page. using the below code.

    getQuote()
    ->getShippingAddress()
    ->getWeight();

    echo $weight; die;

    ?>

    it print the general attribute weight instead of selected custom option weight.

    pls help

    ReplyDelete
    Replies
    1. Hi Pankaj,

      First of all try this simple thing before adding custom option value.

      $item = $observer->getQuoteItem();
      $item->setWeight(10);

      and check if its working or not.

      Thanks,

      Delete

 

Copyright @ 2017 HKBlog.