-
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
Add fallback for Product_links position attribute if not set in request #12650
Add fallback for Product_links position attribute if not set in request #12650
Conversation
… was not set in API request
…:mohammedsalem/magento2 into bug-fix-product-links-position-attribute
@mohammedsalem looks like @mzeis is going to process this pull request for you. If you need anything else just drop me or Matthias a message here. Thanks. |
@dmanners many thanks, for sure gonna do :) |
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.
Hi @mohammedsalem,
thank you very much for your PR! Please have a look at my code review, there is a minor change I requested.
@@ -18,7 +18,7 @@ class SaveHandler | |||
/** | |||
* @var ProductLinkRepositoryInterface | |||
*/ | |||
protected $productLinkRepository; | |||
protected $productLinkRepo; |
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.
Please don't rename a protected property as this breaks backwards compatibility (source). I know you get a warning regarding the variable name length, but backwards compatibility is more important than code style.
* @param $linksByType | ||
* @return array | ||
*/ | ||
private function isPositionsSet($linksByType) |
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.
Please rename this to isPositionSet()
for consistency.
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.
@mzeis Changes made
the idea of the plural positions naming instead of singular was that the function returns an array of multi product link type, on the same time wanted to keep the bool naming convention using 'is' or 'has'.
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 have the feeling this code could be a little bit more simplified. I'm wondering whether we really need so many iterations to find the links that need their position set.
Please can you consider this change? Also see my comments regarding variable naming.
Thanks!
* @param $linksByType | ||
* @return array | ||
*/ | ||
private function isPositionSet($linksByType) |
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.
Thinking some more about it, it's confusing that a method name starting with is
returns an array. It suggests the result should be boolean.
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.
Naming convention will be improved
} | ||
|
||
// Do check | ||
$hasPositionLinkType = $this->isPositionSet($linksByType); |
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.
It's confusing that a variable name starting with has
contains an array. The name suggests that it contains a boolean value.
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.
it contains an array of boolean values per link type, I had the case that the positions were set for the products in "Related" but not set for products under "Cross sell" and so on,
the same that under on Link type got the case of some products had position attribute and others don't, therefor I had to check if at least one product doesn't have a position under a product Link type all positions under this type will be ignored and user array index instead.
regarding the question whether we need that amount of iterations,
This came from too many use cases and variants of sending data, as I said about some link types aren't used at all others have positions but not for all products, another type with full defined products positions.
I'll continue working in January because of the holidays, |
Hi @mohammedsalem, please can you provide a way to re-produce the initial problem by posting the calls starting from a fresh 2.2 install? Sorry I only come up with this now. |
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.
Thanks for your refactoring! I have two more recommendations, please have a look at the review.
|
||
// Build links per type | ||
$linksByType = []; | ||
foreach ($productLinks as $link) { |
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.
After your last refactoring, we don't need the variable $productLinks
in line 72 anymore. Call $entity->getProductLinks()
directly in line 72. By doing this, we adhere to the best practice that a variable shouldn't be changed anymore after initialisation.
* @param $links | ||
* @return bool | ||
*/ | ||
public function hasPosition($links) |
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.
Please change the scope - methods should be private
whenever possible. Also, we now that $links
will be an array so we can use type hints.
PR-usecase.json.txt |
@mohammedsalem thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository. |
…ot set in request #12650
FYI In this PR introduced new dependency to constructor, that wasn't used. I removed it inside #13436 |
if position index was not set in API request Original PR: magento#12650
@mohammedsalem Thanks again for your contribution! Your PR has been merged into @ihor-sviziev Thanks for catching this! The dependency was needed first, but not anymore after the refactoring. |
By setting product_links index on a product via API call, only the first two of each link type are shown in the backend or a in the response of a GET request, although they are correctly added to the database.
The problem was that if the position wasn't set there's no default value or a fallback set in the table "catalog_product_link_attribute_int" for the position attribute.
Description
in SaveHandler class from Magento\Catalog\Model\Product\Link
A check goes in all Products link by type to check if the position were set for all products of this type, else if any wasn't set then do a fallback by array position.
Manual testing scenarios
Before applying PR:
After applying PR:
If the position is set manually in the POST/PUT request for all products of a specific link type, this positions should be taken into account.