Skip to content

Instantly share code, notes, and snippets.

@amenk
Last active October 12, 2015 20:53
Show Gist options
  • Save amenk/fa8f6260210f33442637 to your computer and use it in GitHub Desktop.
Save amenk/fa8f6260210f33442637 to your computer and use it in GitHub Desktop.
Magento 2 Javascript Widget which multiplies two values and stores the result in a Bundle Option Quantity
/**
* Multiplies pages and copies and puts the result into the qty field
*/
define([
'jquery',
'underscore',
'mage/template',
], function ($, _, mageTemplate, utils) {
'use strict';
$.widget('mage.ACMEProductMultiply', {
/**
* @private
*/
_init: function initACMEProductMultiply() {
},
/**
* @private
*/
_create: function createACMEProductMultiply() {
var wrapper = this.element,
pages = $(this.options.pagesSelector, wrapper),
copies = $(this.options.copiesSelector, wrapper);
pages.on('change', this._onFactorQtyFieldChanged.bind(this));
copies.on('change', this._onFactorQtyFieldChanged.bind(this));
},
_onFactorQtyFieldChanged: function onFactorQtyFieldChanges() {
var wrapper = this.element,
pages = $(this.options.pagesSelector, wrapper),
copies = $(this.options.copiesSelector, wrapper),
qty = $(this.options.qtyFieldSelector);
qty.val(pages.val() * copies.val());
qty.trigger('change');
}
});
return $.mage.ACMEProductMultiply;
});
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?php /* @var $block \Magento\Bundle\Block\Catalog\Product\View\Type\Bundle\Option\Radio */ ?>
<?php $_option = $block->getOption(); ?>
<?php $_selections = $_option->getSelections(); ?>
<?php $_default = $_option->getDefaultSelection(); ?>
<?php list($_defaultQty, $_canChangeQty) = $block->getDefaultValues(); ?>
<script type="text/x-magento-init">
{
"#qty-multi-<?php echo $_option->getId() ?>": {
"ACMEProductMultiply": {
"pagesSelector": "#bundle-option-<?php echo $_option->getId() ?>-qty-input-pages",
"copiesSelector": "#bundle-option-<?php echo $_option->getId() ?>-qty-input-copies",
"qtyFieldSelector": "#bundle-option-<?php echo $_option->getId() ?>-qty-input"
}
}
}
</script>
<div class="field option <?php echo ($_option->getRequired()) ? ' required' : ''; ?>">
<label class="label">
<span><?php echo $block->escapeHtml($_option->getTitle()) ?></span>
</label>
<div class="control">
<div class="nested options-list">
<?php if ($block->showSingle()): ?>
<?php echo $block->getSelectionTitlePrice($_selections[0]) ?>
<input type="hidden"
class="bundle-option-<?php echo $_option->getId() ?> product bundle option"
name="bundle_option[<?php echo $_option->getId() ?>]"
value="<?php echo $_selections[0]->getSelectionId() ?>"/>
<?php else: ?>
<?php if (!$_option->getRequired()): ?>
<div class="field choice">
<input type="radio"
class="radio product bundle option"
id="bundle-option-<?php echo $_option->getId() ?>"
name="bundle_option[<?php echo $_option->getId() ?>]"
<?php echo ($_default && $_default->isSalable()) ? '' : ' checked="checked" ' ?>
value=""/>
<label class="label" for="bundle-option-<?php echo $_option->getId() ?>">
<span><?php echo __('None') ?></span>
</label>
</div>
<?php endif; ?>
<?php foreach ($_selections as $_selection): ?>
<div class="field choice">
<input type="radio"
class="radio product bundle option change-container-classname"
id="bundle-option-<?php echo $_option->getId() ?>-<?php echo $_selection->getSelectionId() ?>"
<?php if ($_option->getRequired()) echo 'data-validate="{\'validate-one-required-by-name\':true}"' ?>
name="bundle_option[<?php echo $_option->getId() ?>]"
<?php if ($block->isSelected($_selection)) echo ' checked="checked"' ?>
<?php if (!$_selection->isSaleable()) echo ' disabled="disabled"' ?>
value="<?php echo $_selection->getSelectionId() ?>"/>
<label class="label"
for="bundle-option-<?php echo $_option->getId() ?>-<?php echo $_selection->getSelectionId() ?>">
<span><?php echo $block->getSelectionTitlePrice($_selection) ?></span>
</label>
</div>
<?php endforeach; ?>
<div id="bundle-option-<?php echo $_option->getId() ?>-container"></div>
<?php endif; ?>
<div class="qty-multi-wrapper" id="qty-multi-<?php echo $_option->getId() ?>">
<div class="field qty qty-holder">
<label class="label" for="bundle-option-<?php echo $_option->getId() ?>-qty-input-pages">
<span><?php echo __('Pages') ?></span>
</label>
<div class="control">
<input
id="bundle-option-<?php echo $_option->getId() ?>-qty-input-pages"
class="input-text qty-pages"
type="number"
name="bundle_option_qty_pages[<?php echo $_option->getId() ?>]"
value="1"/>
</div>
</div>
<div class="field qty qty-holder">
<label class="label" for="bundle-option-<?php echo $_option->getId() ?>-qty-input-copies">
<span><?php echo __('Copies') ?></span>
</label>
<div class="control">
<input
id="bundle-option-<?php echo $_option->getId() ?>-qty-input-copies"
class="input-text qty-copies"
type="number"
name="bundle_option_qty_copies[<?php echo $_option->getId() ?>]"
value="<?php echo $_defaultQty ?>"/>
</div>
</div>
</div>
<input <?php if (!$_canChangeQty) echo ' disabled="disabled"' ?>
id="bundle-option-<?php echo $_option->getId() ?>-qty-input"
class="input-text qty<?php if (!$_canChangeQty) echo ' qty-disabled' ?>"
type="hidden"
name="bundle_option_qty[<?php echo $_option->getId() ?>]"
value="<?php echo $_defaultQty ?>"/>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment