Skip to content

Commit

Permalink
Add module.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpartington-cti committed Oct 16, 2018
1 parent 77720dc commit 216274f
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 0 deletions.
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "ctidigital/module-catalogjs",
"description": "Adds models that are triggered when a customer selects options on the product page.",
"version": "1.0.3",
"keywords": [
"magento 2"
],
"require": {
"magento/magento-composer-installer": "*"
},
"suggest": {

},
"type": "magento2-module",
"license": [

],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"CtiDigital\\CatalogJS\\": ""
}
}
}
9 changes: 9 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="CtiDigital_CatalogJS" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog" />
</sequence>
</module>
</config>
99 changes: 99 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Overview

The _CatalogJS_ module creates several JavaScript observers on the product page. By listening to these observers, your script can respond to specific events.

The primary use case for this is to find out which option a customer has selected on a configurable product so that your component can perform its own action. For example you may want to display some specific information about the selected simple product option.

# Observers

The following section documents the observers that are available and how you may use them.

## configurable-option

### Purpose

The configurable option observer will be updated every time a customer updates the configurable options. The observer will return the selected simple product ID. In your own UI component, you could search against a JSON object which contains the information for the simple products or you could make an AJAX request to get information about the ID.

### Available Values

The following values can be subscribed to on the `configurable-option` model.

```
simpleProduct // The currently selected simple product ID
```

### Usage

```javascript
define([
'jquery',
'uiComponent',
'CtiDigital_CatalogJS/js/model/configurable-option'
], function($, Component, configurableOption) {
'use strict';

return Component.extend({
selectedProduct: undefined,

initialize: function() {
this._super();
this.subscribeToConfigurableOptionChange();
},
subscribeToConfigurableOptionChange: function() {
let _this = this;
configurableOption.simpleProduct.subscribe(function(simpleProduct) {
_this.selectedProduct = simpleProduct;
_this.myCustomMethod();
});
},
myCustomMethod: function() {
// Access this.selectedProduct to perform some sort of action
}
});
});
```

## qty

### Purpose

The qty observer will be updated every time a customer changes the quantity box and will return the value they've entered.

### Available Values

The following values can be subscribed to on the `qty` model.

```
qty // The current quantity specified by the customer
```

### Usage

```javascript
define([
'jquery',
'uiComponent',
'CtiDigital_CatalogJS/js/model/qty'
], function ($, Component, qtyObserver) {
'use strict';

return Component.extend({
qty: 1,

initialize: function() {
this._super();
this.subscribeToQtyChange();
},
subscribeToQtyChange: function() {
let _this = this;
qtyObserver.qty.subscribe(function(qty) {
_this.qty = qty;
_this.myCustomMethod();
});
},
myCustomMethod: function() {
// Access this.qty to retrieve the current quantity
}
});
});
```
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'CtiDigital_CatalogJS',
__DIR__
);
9 changes: 9 additions & 0 deletions view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="content">
<block class="Magento\Framework\View\Element\Template" name="catalogjs.qty" template="CtiDigital_CatalogJS::qty-observer.phtml" />
</referenceBlock>
</body>
</page>
9 changes: 9 additions & 0 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var config = {
config: {
mixins: {
'Magento_ConfigurableProduct/js/configurable': {
'CtiDigital_CatalogJS/js/mixins/configurable': true
}
}
}
};
14 changes: 14 additions & 0 deletions view/frontend/templates/qty-observer.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script type="text/x-magento-init">
{
"*": {
"Magento_Ui/js/core/app": {
"components": {
"qty-observer": {
"component": "CtiDigital_CatalogJS/js/component/qty-observer",
"qty_element": "#qty"
}
}
}
}
}
</script>
25 changes: 25 additions & 0 deletions view/frontend/web/js/component/qty-observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
define([
'jquery',
'uiComponent',
'ko',
'CtiDigital_CatalogJS/js/model/qty'
], function($, Component, ko, qtyObserver) {
'use strict';

return Component.extend({
initialize: function() {
this._super();
this.updateQtyObs($(this.qty_element));
this.subscribeToQuantityBoxUpdate();
},
updateQtyObs: function (e) {
if (e.target) {
var e = $(e.target);
}
qtyObserver.qty(parseInt(e.val()));
},
subscribeToQuantityBoxUpdate: function() {
$(this.qty_element).on("change paste keyup select", $.proxy(this.updateQtyObs, this));
}
});
});
16 changes: 16 additions & 0 deletions view/frontend/web/js/mixins/configurable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
define([
'jquery',
'CtiDigital_CatalogJS/js/model/configurable-option'
], function($, configurableOption) {
'use strict';

return function (widget) {
$.widget('mage.configurable', widget, {
_configureElement: function(element) {
this._super(element);
configurableOption.simpleProduct(this.simpleProduct);
}
});
return $.mage.configurable;
}
});
9 changes: 9 additions & 0 deletions view/frontend/web/js/model/configurable-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define([
'ko'
], function(ko) {
'use strict';
let simpleProduct = ko.observable('');
return {
simpleProduct: simpleProduct
};
});
10 changes: 10 additions & 0 deletions view/frontend/web/js/model/qty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
define(
['ko'],
function (ko) {
'use strict';
let qty = ko.observable('');
return {
qty: qty
};
}
);

0 comments on commit 216274f

Please sign in to comment.