Guest User

Magento Simple Product Dynamic SKU

a guest
Jul 23rd, 2015
300
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE_AFL.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category design
  22. * @package base_default
  23. * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
  24. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  25. */
  26. /* @var $this Mage_Catalog_Block_Product_View_Options */
  27. ?>
  28.  
  29. <?php $_options = Mage::helper('core')->decorateArray($this->getOptions()) ?>
  30. <?php if (count($_options)):?>
  31.  
  32.  
  33.  
  34.  
  35. <dl>
  36. <?php $options_to_sku = array();?>
  37.  
  38. <?php foreach($_options as $_option): ?>
  39. <?php echo $this->getOptionHtml($_option) ?>
  40. <?php
  41. if ($_option->getType() == 'drop_down')
  42. {
  43. $options_to_sku["{$_option->getId()}"] = array();
  44. foreach ($_option->getValues() as $k => $v)
  45. {
  46. $options_to_sku["{$_option->getId()}"]["{$v->getData('option_type_id')}"] = $v->getData('sku');
  47. }
  48. }
  49. ?>
  50. <?php endforeach; ?>
  51. </dl>
  52. <script type="text/javascript">
  53.  
  54.  
  55. var options_to_sku = <?php echo json_encode($options_to_sku);?>;
  56.  
  57. $$('.product-custom-option').each(function(elm) {
  58. $(elm).observe('change', function() {
  59. var sku_suffix = '';
  60. $$('.product-custom-option').each(function(opt) {
  61. if ($(opt).value.length==0) // no value selected
  62. return;
  63.  
  64. var opt_id = $(opt).readAttribute('id').replace('select_','');
  65.  
  66. if (!!!options_to_sku[opt_id]) // no option values array found
  67. return;
  68.  
  69. if (!!!options_to_sku[opt_id][$(opt).value]) // no sku found matching value id
  70. return;
  71.  
  72. sku_suffix += '-' + options_to_sku[opt_id][$(opt).value]; // add to suffix
  73. });
  74.  
  75. /**
  76. * add here the code that adds the suffix to your SKU
  77. */
  78. console.log(sku_suffix);
  79. });
  80. });
  81. $("sku-container").update("<strong>Configuration: <?php echo $this->getProduct()->getSku(); ?></strong>" + (sku_suffix));
  82. </script>
  83.  
  84. <div id="sku-container"></div>
  85.  
  86.  
  87.  
  88.  
  89. <?php endif; ?>
RAW Paste Data