Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Modify variant picker to use url

Maciek Jabłoński9ca00638dd25
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index aaf878166f..c440c55ce0 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -5,8 +5,13 @@ from django.contrib.auth.models import AnonymousUser
from ..cart.utils import get_cart_from_request, get_or_create_cart_from_request
from ..core.utils import to_local_currency
from .forms import get_form_class_for_product
-from .models.utils import get_attributes_display_map
from .models import Product
+from .models.utils import get_attributes_display_map
+
+try:
+ from urllib.parse import urlencode
+except ImportError:
+ from urllib import urlencode
def products_visible_to_user(user):
@@ -128,7 +133,7 @@ def get_variant_picker_data(product, discounts=None):
'pk': attribute.pk,
'display': attribute.display,
'name': attribute.name,
- 'values': [{'pk': value.pk, 'display': value.display}
+ 'values': [{'pk': value.pk, 'display': value.display, 'slug': value.slug}
for value in attribute.values.all()]})
for variant in variants:
@@ -171,3 +176,18 @@ def price_range_as_dict(price_range):
return None
return {'maxPrice': price_as_dict(price_range.max_price),
'minPrice': price_as_dict(price_range.min_price)}
+
+
+def get_variant_url_from_product(product, attributes):
+ return '%s?%s' % (product.get_absolute_url(), urlencode(attributes))
+
+
+def get_variant_url(variant):
+ attributes = {}
+ values = {}
+ for attribute in variant.product.product_class.variant_attributes.all():
+ attributes[str(attribute.pk)] = attribute
+ for value in attribute.values.all():
+ values[str(value.pk)] = value
+
+ return get_variant_url_from_product(variant.product, attributes)
diff --git a/saleor/static/js/components/variantPicker/VariantPicker.js b/saleor/static/js/components/variantPicker/VariantPicker.js
index 506587ebde..848366dfe2 100644
--- a/saleor/static/js/components/variantPicker/VariantPicker.js
+++ b/saleor/static/js/components/variantPicker/VariantPicker.js
@@ -6,6 +6,7 @@ import React, { Component, PropTypes } from 'react';
import AttributeSelectionWidget from './AttributeSelectionWidget';
import QuantityInput from './QuantityInput';
+import * as queryString from 'query-string';
@observer
export default class VariantPicker extends Component {
@@ -21,17 +22,34 @@ export default class VariantPicker extends Component {
constructor(props) {
super(props);
- const { store, variants } = this.props;
+ const { variants } = this.props;
const variant = variants.filter(v => !!Object.keys(v.attributes).length)[0];
- const selection = variant ? variant.attributes : {};
-
+ const params = queryString.parse(location.search);
+ let selection = {};
+ if (params) {
+ Object.keys(params).some((name) => {
+ const valueName = params[name];
+ const attribute = this.matchAttributeByName(name);
+ const value = this.matchAttributeValueByName(attribute, valueName);
+ if (attribute && value) {
+ selection[attribute.pk] = value.pk.toString();
+ } else {
+ // if attribute doesn't exist - show variant
+ selection = variant ? variant.attributes : {};
+ // break
+ return true;
+ }
+ });
+ } else if (variant) {
+ selection = variant.attributes;
+ }
this.state = {
errors: {},
quantity: 1,
selection: selection
};
- store.setVariant(variant);
+ this.matchVariantFromSelection();
}
handleAddToCart = () => {
@@ -60,6 +78,15 @@ export default class VariantPicker extends Component {
selection: Object.assign({}, this.state.selection, { [attrId]: valueId })
}, () => {
this.matchVariantFromSelection();
+ let params = {};
+ Object.keys(this.state.selection).forEach(attrId => {
+ const attribute = this.matchAttribute(attrId);
+ const value = this.matchAttributeValue(attribute, this.state.selection[attrId]);
+ if (attribute && value) {
+ params[attribute.name] = value.slug;
+ }
+ });
+ history.pushState(null, null, '?' + queryString.stringify(params));
});
}
@@ -67,6 +94,28 @@ export default class VariantPicker extends Component {
this.setState({quantity: parseInt(event.target.value)});
}
+ matchAttribute = (id) => {
+ const { variantAttributes } = this.props;
+ const match = variantAttributes.filter(attribute => attribute.pk.toString() === id);
+ return match.length > 0 ? match[0] : null;
+ }
+
+ matchAttributeByName = (name) => {
+ const { variantAttributes } = this.props;
+ const match = variantAttributes.filter(attribute => attribute.name === name);
+ return match.length > 0 ? match[0] : null;
+ }
+
+ matchAttributeValue = (attribute, id) => {
+ const match = attribute.values.filter(attribute => attribute.pk.toString() === id);
+ return match.length > 0 ? match[0] : null;
+ }
+
+ matchAttributeValueByName = (attribute, name) => {
+ const match = attribute ? attribute.values.filter(value => value.slug === name) : [];
+ return match.length > 0 ? match[0] : null;
+ }
+
matchVariantFromSelection() {
const { store, variants } = this.props;
let matchedVariant = null;