-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix swatch-renderer.js product id and isProductViewExist #8484
Conversation
This is a great improvement, @mimarcel, and but it's unfortunate that you still have to work so directly with CSS class selectors to derive the relevant product ID. I think it would be a lot better if there were data attributes in the HTML so that you could always know which product was attached to a given DOM element. Using the schema.org microdata approach, for instance, you could add <div itemscope
itemtype="http://schema.org/Product"
class="product details product-item-details">
<meta itemprop="productID" content="<?php echo $_item->getId() ?>"> Then, you could have a universal JavaScript function, perhaps in a separate module, that would always return the product ID for the current context. define(['mage/itemscope'], function(itemscope) {
// in some function:
var scope = itemscope(this.element);
if (scope && scope.itemType === itemscope.PRODUCT && scope.productID) {
// scope contains data for the product being displayed with `this.element`.
}
}); Might be a useful, general-purpose thing to have. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from the overall structural change I'm asking about in my other comment, and the small bit of jQuery usage I recommend below, this looks good.
|
||
if (!productId) { | ||
// Check individual product. | ||
var product = document.getElementsByName('product')[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may throw an exception if there is no such element, since document.getElementsByName()
can return null. Since jQuery is already present, you should perhaps replace this with var product = $('[product]')[0];
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @zetlen ,
Thank you for looking into this PR with so much care.
- I did refactor the code to use jQuery instead of plain Javascript for an easier read: e9c42d2.
- However, it's worth mentioning that
document.getElementsByName('product')[0]
doesn't return an exception becausedocument.getElementsByName('product')
returns empty array in case no elements are found anddocument.getElementsByName('product')[0]
will return undefined. - The previous code
document.getElementsByName('product')[0].value
was indeed suspicious to throw an exception - issue which this PR fixed.
_determineProductData: function() { | ||
// Check if product is in a list of products. | ||
var productId = this.element.parents('.product-item-details') | ||
.find('.price-box.price-final_price').attr('data-product-id'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a general comment about how sad it is that it has to be done this way. This is a common need and should be separated into a general utility function/module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @zetlen ,
This sounds like a very good idea, but I feel it's out of the scope of this PR. There are other few things I noticed that need refactoring in swatch-renderer.js, but I tried to keep the code as similar as possible with the old code and just fix the issues, so the code review will be as easy as possible. I would rather leave the refactoring on Magento team's side.
@mimarcel can you please merge with the latest develop branch |
…x/swatch-renderer # Conflicts: # app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js
@mimarcel Thank you for the contribution |
Summary
This pull requests allows you to add a list of products, with swatches, on Product Page, without conflicting with the global product's own swatches.
Pre-requisites
and change this code
to this code
Ignore that we're changing the core here.
Steps to Replicate
Expected
Image for related product B is updated
Actual
Images for product C are updated