[IMP] website_sale: change product url to include variants

Hash containing variants unique id added to the url.
In the shop when the product page is loaded, the variants are now
selected using this hash.
The product.product website_url is no longer the template's url, it is
now using the hash and will redirect you to the right product.

task-1958091
This commit is contained in:
fja-odoo
2019-05-31 13:47:20 +00:00
parent 1f5c9e363b
commit 0a79d9a90f
2 changed files with 52 additions and 4 deletions
+8
View File
@@ -430,6 +430,14 @@ class Product(models.Model):
product_variant_image_ids = fields.One2many('product.image', 'product_variant_id', string="Extra Variant Images")
website_url = fields.Char('Website URL', compute='_compute_product_website_url', help='The full URL to access the document through the website.')
@api.depends('product_tmpl_id.website_url', 'attribute_value_ids')
def _compute_product_website_url(self):
for product in self:
attributes = ','.join(str(x) for x in product.attribute_value_ids.ids)
product.website_url = "%s#attr=%s" % (product.product_tmpl_id.website_url, attributes)
@api.multi
def website_publish_button(self):
self.ensure_one()
@@ -169,11 +169,29 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, {
start: function () {
var def = this._super.apply(this, arguments);
var hash = window.location.hash.substring(1);
if (hash) {
var params = $.deparam(hash);
if (params['attr']) {
var attributeIds = params['attr'].split(',');
var $inputs = this.$('input.js_variant_change, select.js_variant_change option');
_.each(attributeIds, function (id) {
var $toSelect = $inputs.filter('[data-value_id="' + id + '"]');
if ($toSelect.is('input[type="radio"]')) {
$toSelect.prop('checked', true);
} else if ($toSelect.is('option')) {
$toSelect.prop('selected', true);
}
});
this._changeColorAttribute();
}
}
_.each(this.$('div.js_product'), function (product) {
$('input.js_product_change', product).first().trigger('change');
});
// This has to be triggered to compute the "out of stock" feature
// This has to be triggered to compute the "out of stock" feature and the hash variant changes
this.triggerVariantChange(this.$el);
this.$('select[name="country_id"]').change();
@@ -209,6 +227,28 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, {
// Private
//--------------------------------------------------------------------------
/**
* Sets the url hash from the selected product options.
*
* @private
*/
_setUrlHash: function ($parent) {
var $attributes = $parent.find('input.js_variant_change:checked, select.js_variant_change option:selected');
var attributeIds = _.map($attributes, function (elem) {
return $(elem).data('value_id');
});
history.replaceState(undefined, undefined, '#attr=' + attributeIds.join(','));
},
/**
* Set the checked color active.
*
* @private
*/
_changeColorAttribute: function () {
$('.css_attribute_color').removeClass("active")
.filter(':has(input:checked)')
.addClass("active");
},
/**
* @private
*/
@@ -607,9 +647,7 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, {
* @param {Event} ev
*/
_onChangeColorAttribute: function (ev) { // highlight selected color
$('.css_attribute_color').removeClass("active")
.filter(':has(input:checked)')
.addClass("active");
this._changeColorAttribute();
},
/**
* @private
@@ -678,6 +716,8 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, {
$el.attr('selected', $el.is(':selected'));
});
this._setUrlHash($component);
return VariantMixin.onChangeVariant.apply(this, arguments);
},
/**