How to customize custom options price in frontend of magento

First of all let you go to download an extension Magebuzz_Customoption to automatically select one of the specified options in the drop-down. After installing you’ll see a menu in the admin panel to automatically select one option by default. 

screenshot-magento-default-custom-option

 

Create custom option as much as you need. Let’s assume 4 custom option (Material, Thickness, Width, Ring Size)

Add multiple options to these custom option with its value.

Now here is the calculation I need

 

  1. Final Price= ?
  2.  
  3. Material Price = user defined value
  4.  
  5. Final Price=Material Price
  6.  
  7. Thickness Price = Final Price * Thickness Price Value
  8.  
  9. Final Price=Thickness Price
  10.  
  11. Width Price = (Final Price / 8 ) * Width Price Value
  12.  
  13. Final Price=Width Price
  14.  
  15. Ring Size Price = Final Price + (% of Ring Size Price Value)
  16.  
  17. Final Price=Ring Size Price

To show that calculation on pages of cart, etc, let’s go to \app\code\core\Mage\Catalog\Model\Product\Type\Price.php

Edit _applyOptionsPrice method of Mage_Catalog_Model_Product_Type_Price class 

 

  1. protected function _applyOptionsPrice($product, $qty, $finalPrice) {
  2.  
  3. if ($optionIds = $product->getCustomOption('option_ids')) {
  4. $basePrice = $finalPrice;
  5. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  6. if ($option = $product->getOptionById($optionId)) {
  7. $confItemOption = $product->getCustomOption('option_' . $option->getId());
  8.  
  9. $group = $option->groupFactory($option->getType())
  10. ->setOption($option)
  11. ->setConfigurationItemOption($confItemOption);
  12.  
  13. if (in_array($option['default_title'], array('material', 'Material'))) {
  14. // MP = MP;
  15. $finalPrice = $finalPrice + $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  16. } elseif (in_array($option['default_title'], array('Thickness', 'thickness'))) {
  17. // TP = MP * TV;
  18. $thikness_price = $finalPrice * $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  19. $finalPrice = $thikness_price;
  20. } elseif (in_array($option['default_title'], array('Width', 'width'))) {
  21. //WP = TP / 8 * WV;
  22. $width_price = ($finalPrice / 8 ) * $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  23. $finalPrice = $width_price;
  24. } elseif (in_array($option['default_title'], array('Ring Size', 'ring size', 'Ring size', 'ring Size', 'ring_size'))) {
  25. //RSP = WP * RS%;
  26. $rsp = $finalPrice + (($finalPrice / 100) * $group->getOptionPrice($confItemOption->getValue()) );
  27. $finalPrice = $rsp;
  28. } else {
  29. $finalPrice += $group->getOptionPrice($confItemOption->getValue(), $basePrice);
  30. }
  31. }
  32. }
  33. }
  34.  
  35. return $finalPrice;
  36. }

If now you’ll see your product detail page & your cart page, both price will be differ, because, till now you have only configured custom price on cart & onward pages. Now let’s move to customize product detail page custom price

First of all we’ll go to \app\code\local\Magebuzz\Customoption\Block\Catalog\Product\View\Options\Type\Select.php to add a new html attribute to select element. 

  1. Add
  2.  
  3.  
  4. $extraParams .= ' label="' . $_option->getTitle() . '"';
  5.  
  6. after
  7.  
  8. $extraParams .= ' onchange="opConfig.reloadPrice()"';

Now let’s go to assign label of custom option to a variable which’ll be sent ahead for further process.

search for : if (typeof (configOptions[selectOption.value]) != ‘undefined’)

inside the if block add this code

  1. configOptions[selectOption.value].options_label = element.getAttribute("label");

Now let’s go to js file which will change price dynamically according to your choice on the product detail page. 

File path: \js\varien\product.js

search for : Object.values(this.customPrices).each(function (pair)

inside that add this :

  1. if (pair.excludeTax && pair.includeTax) {
  2. if (pair.options_label == 'Material') {
  3. //MP : Material Price
  4. subPrice += parseFloat(pair.excludeTax);
  5. subPriceincludeTax += parseFloat(pair.includeTax);
  6. } else if (pair.options_label == 'Thickness') {
  7. //TP : Thinkness price
  8. var thikness_price = subPrice * pair.price;
  9. subPrice = parseFloat(thikness_price);
  10. subPriceincludeTax = parseFloat(thikness_price);
  11. } else if (pair.options_label == 'Width') {
  12. //WP : Width Price
  13. var width_price = (subPrice / 8) * pair.price;
  14. subPrice = parseFloat(width_price);
  15. subPriceincludeTax = parseFloat(width_price);
  16.  
  17. } else if (pair.options_label == 'Ring Size') {
  18. //RSP : Ring Size Price
  19. var rsp = subPrice + ((subPrice / 100) * pair.price);
  20. //var width_price = (subPrice / 8 ) * pair.price;
  21. subPrice = parseFloat(rsp);
  22. subPriceincludeTax = parseFloat(rsp);
  23. } else {
  24. subPrice += parseFloat(pair.excludeTax);
  25. subPriceincludeTax += parseFloat(pair.includeTax);
  26. }
  27. //subPrice += parseFloat(pair.excludeTax);
  28. //subPriceincludeTax += parseFloat(pair.includeTax);
  29. } else {
  30. if (pair.options_label == 'Material') {
  31. //MP : Material Price
  32. subPrice += parseFloat(pair.excludeTax);
  33. subPriceincludeTax += parseFloat(pair.includeTax);
  34. } else if (pair.options_label == 'Thickness') {
  35. //TP : Thinkness price
  36. var thikness_price = subPrice * pair.price;
  37. subPrice = parseFloat(thikness_price);
  38. subPriceincludeTax = parseFloat(thikness_price);
  39. } else if (pair.options_label == 'Width') {
  40. //WP : Width Price
  41. var width_price = (subPrice / 8) * pair.price;
  42. subPrice = parseFloat(width_price);
  43. subPriceincludeTax = parseFloat(width_price);
  44.  
  45. } else if (pair.options_label == 'Ring Size') {
  46. //RSP : Ring Size Price
  47. var rsp = subPrice + ((subPrice / 100) * pair.price);
  48. //var width_price = (subPrice / 8 ) * pair.price;
  49. subPrice = parseFloat(rsp);
  50. subPriceincludeTax = parseFloat(rsp);
  51. } else {
  52. subPrice += parseFloat(pair.price);
  53. subPriceincludeTax += parseFloat(pair.price);
  54. }
  55. // subPrice += parseFloat(pair.price);
  56. // subPriceincludeTax += parseFloat(pair.price);
  57. }

Now you’ll be able to see the custom price on product detail page as well