Skip to content

Commit 14b1049

Browse files
committed
add support for shopping expieriences
- register plugin in base template and not in the buy-widget-form - do not overwrite buy-button to improve compatibility with other plugins
1 parent a67cacb commit 14b1049

File tree

5 files changed

+56
-55
lines changed

5 files changed

+56
-55
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codebarista/buybuttonstatesynchronizer",
3-
"version": "3.0.1",
3+
"version": "3.1.0",
44
"description": "Allows multiple plugins to control the buy button state.",
55
"type": "shopware-platform-plugin",
66
"license": "MIT",
@@ -11,9 +11,9 @@
1111
}
1212
],
1313
"require": {
14-
"shopware/core": "~6.5",
15-
"shopware/administration": "~6.5",
16-
"shopware/storefront": "~6.5"
14+
"shopware/core": "6.5.*",
15+
"shopware/administration": "6.5.*",
16+
"shopware/storefront": "6.5.*"
1717
},
1818
"autoload": {
1919
"psr-4": {

src/Resources/app/storefront/dist/storefront/js/barista-buy-button-state-synchronizer.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/app/storefront/src/buybuttonstatesynchronizer/buybuttonstatesynchronizer.plugin.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Plugin from "src/plugin-system/plugin.class";
22

33
export default class BuyButtonStateSynchronizer extends Plugin {
44
static options = {
5-
showDialogInsteadOfDisableButton: null,
5+
showDialogInsteadOfDisableButton: false,
66
};
77

88
/**
@@ -47,7 +47,7 @@ export default class BuyButtonStateSynchronizer extends Plugin {
4747
* is left.
4848
*/
4949
static disableBuyButton(id, actionLabel = null, actionCallback = null) {
50-
const buyButton = document.getElementsByClassName("btn-buy").item(0);
50+
const buyButton = document.querySelector(".btn-buy");
5151
if (buyButton == null) {
5252
console.warn("Cannot disable buy button: No element with class btn-buy");
5353
return;
@@ -75,7 +75,7 @@ export default class BuyButtonStateSynchronizer extends Plugin {
7575
* @param {string} id The ID of the vote that is withdrawn.
7676
*/
7777
static enableBuyButton(id) {
78-
const buyButton = document.getElementsByClassName("btn-buy").item(0);
78+
const buyButton = document.querySelector(".btn-buy");
7979
if (buyButton == null) {
8080
console.warn("Cannot enable buy button: No element with class btn-buy");
8181
return;
@@ -88,6 +88,15 @@ export default class BuyButtonStateSynchronizer extends Plugin {
8888
}
8989

9090
init() {
91+
const buyForm = document.getElementById("productDetailPageBuyProductForm");
92+
if (buyForm == null) {
93+
return; // We are not on the product details page
94+
}
95+
this.buyButton = document.querySelector(".btn-buy");
96+
if (this.buyButton == null) {
97+
console.warn("Cannot initialize: No element with class btn-buy");
98+
return;
99+
}
91100
if (!window.buyButtonStateSynchronizerActions) {
92101
window.buyButtonStateSynchronizerActions = {};
93102
}
@@ -107,12 +116,14 @@ export default class BuyButtonStateSynchronizer extends Plugin {
107116
setTimeout(this.buyButtonNodeChanged.bind(this));
108117

109118
if (this.options.showDialogInsteadOfDisableButton) {
110-
this.el.addEventListener("click", this.onBuyButtonClicked.bind(this));
111-
this.el.disabled = false;
119+
this.buyButton.addEventListener(
120+
"click",
121+
this.onBuyButtonClicked.bind(this)
122+
);
123+
this.buyButton.disabled = false;
112124
}
113125

114126
// Prevent form submit by pressing enter if buy button is disabled
115-
const buyForm = document.getElementById("productDetailPageBuyProductForm");
116127
buyForm.addEventListener("keydown", (event) => {
117128
if (event.code == "Enter" || event.code == "NumpadEnter") {
118129
return this.onBuyButtonClicked(event);
@@ -126,23 +137,22 @@ export default class BuyButtonStateSynchronizer extends Plugin {
126137
// stop observing attribute changes...
127138
this.stopObservingBuyButtonAttributeChanges();
128139
// ...otherwise this line would cause an infinite loop...
129-
this.el.disabled = this.isDisabled;
140+
this.buyButton.disabled = this.isDisabled;
130141
// ...start observing again
131142
this.startObservingBuyButtonAttributeChanges();
132143
}
133144
}
134145

135146
startObservingBuyButtonAttributeChanges() {
136-
// this.el is the element on which this plugin was initialized -> the buy button
137-
this.buyButtonObserver.observe(this.el, { attributes: true });
147+
this.buyButtonObserver.observe(this.buyButton, { attributes: true });
138148
}
139149

140150
stopObservingBuyButtonAttributeChanges() {
141151
this.buyButtonObserver.disconnect();
142152
}
143153

144154
isBuyButtonDisabled() {
145-
for (let attribute of this.el.attributes) {
155+
for (let attribute of this.buyButton.attributes) {
146156
if (attribute.name.endsWith("-buy-button-disabled")) {
147157
return true;
148158
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{% sw_extends '@Storefront/storefront/base.html.twig' %}
2+
3+
{% set buyButtonStateSynchronizerOptions = {
4+
showDialogInsteadOfDisableButton: config('BaristaBuyButtonStateSynchronizer').config.showDialogInsteadOfDisableButton
5+
} %}
6+
7+
{% block base_body_inner %}
8+
{{ parent() }}
9+
<template data-buy-button-state-synchronizer data-buy-button-state-synchronizer-options='{{ buyButtonStateSynchronizerOptions|json_encode }}'>></template>
10+
{% block buy_button_state_synchronizer_dialog %}
11+
{% if config('BaristaBuyButtonStateSynchronizer').config.showDialogInsteadOfDisableButton %}
12+
<dialog id="buy-button-state-synchronizer-modal" data-cancel-btn-label="{{ 'buybuttonstate.cancel' | trans }}">
13+
<div class="modal-content">
14+
<div class="modal-body">
15+
<p>{{ 'buybuttonstate.product-not-ready' | trans }}</p>
16+
</div>
17+
<div id="buy-button-state-synchronizer-modal-actions" class="modal-footer">
18+
<div id="buy-button-state-synchronizer-modal-cancel-btn">
19+
<button
20+
type="button"
21+
class="btn btn-secondary"
22+
data-dismiss="modal">
23+
{{ 'buybuttonstate.cancel' | trans }}
24+
</button>
25+
</div>
26+
</div>
27+
</div>
28+
</dialog>
29+
{% endif %}
30+
{% endblock %}
31+
{% endblock %}

src/Resources/views/storefront/page/product-detail/buy-widget-form.html.twig

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)