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
Use product type id instead of product id #35299
Use product type id instead of product id #35299
Conversation
Hi @tobias-forkel. Thank you for your contribution
❗ Automated tests can be triggered manually with an appropriate comment:
You can find more information about the builds here ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review. For more details, review the Magento Contributor Guide documentation. 🕙 You can find the schedule on the Magento Community Calendar page. 📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket. ✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel |
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.
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Catalog\Model\Product\Option;
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface as OptionRepository;
use Magento\Catalog\Model\Product\Option;
use Magento\Catalog\Model\ResourceModel\Product\Relation;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
use Magento\Framework\Exception\CouldNotSaveException;
/**
* SaveHandler for product option
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class SaveHandler implements ExtensionInterface
{
/**
* @var string[]
*/
private array $compositeProductTypes = ['grouped', 'configurable', 'bundle'];
/**
* @var OptionRepository
*/
protected OptionRepository $optionRepository;
/**
* @var Relation
*/
private $relation;
/**
* @param OptionRepository $optionRepository
* @param Relation|null $relation
*/
public function __construct(
OptionRepository $optionRepository,
?Relation $relation = null
) {
$this->optionRepository = $optionRepository;
$this->relation = $relation ?: ObjectManager::getInstance()->get(Relation::class);
}
/**
* Perform action on relation/extension attribute
*
* @param object $entity
* @param array $arguments
* @return ProductInterface|object
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws CouldNotSaveException
*/
public function execute($entity, $arguments = [])
{
if ($entity->getOptionsSaved()) {
return $entity;
}
$options = $entity->getOptions();
$optionIds = [];
if ($options) {
$optionIds = array_map(function (Option $option) {
return $option->getOptionId();
}, $options);
}
/** @var ProductInterface $entity */
foreach ($this->optionRepository->getProductOptions($entity) as $option) {
if (!in_array($option->getOptionId(), $optionIds)) {
$this->optionRepository->delete($option);
}
}
if ($options) {
$this->processOptionsSaving($options, (bool)$entity->dataHasChangedFor('sku'), $entity);
}
return $entity;
}
/**
* Save custom options
*
* @param array $options
* @param bool $hasChangedSku
* @param ProductInterface $product
* @return void
* @throws CouldNotSaveException
*/
private function processOptionsSaving(array $options, bool $hasChangedSku, ProductInterface $product): void
{
$isProductHasRelations = $this->isProductHasRelations($product);
/** @var ProductCustomOptionInterface $option */
foreach ($options as $option) {
if (!$isProductHasRelations && $option->getIsRequire()) {
$message = 'Required custom options can not be added to a simple product'
. ' that is a part of a composite product.';
throw new CouldNotSaveException(__($message));
}
if ($hasChangedSku && $option->hasData('product_sku')) {
$option->setProductSku($product->getSku());
}
$this->optionRepository->save($option);
}
}
/**
* Check if product doesn't belong to composite product
*
* @param ProductInterface $product
* @return bool
*/
private function isProductHasRelations(ProductInterface $product): bool
{
$result = true;
if (!in_array($product->getTypeId(), $this->compositeProductTypes)
&& $this->relation->getRelationsByChildren([$product->getId()])
) {
$result = false;
}
return $result;
}
}
Can you please Update your PR according to the suggested changes? @tobias-forkel
Thanks for your suggestion! I want to keep my first PR as simple as possible without adding further changes, especially wording. Create your own PR and explain why you want to change the spelling. |
Hi @tobias-forkel , Except for this change(cannot to can not), I have suggested changes according to Coding Standards. If you review it once. |
@tobias-forkel Can you please Sign the CLA so this PR can proceed further? |
@magento run all tests |
The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time. |
…f github.com:tobias-forkel/magento2 into fix/magento-catalog-model-product-option-savehandler
Thanks @manavluhar I just signed the CLA and committed your suggestions. |
@magento run all tests |
The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time. |
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 @tobias-forkel
Please check failed static tests
@magento run all tests |
The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time. |
Hi @tobias-forkel , @Den4ik & @manavluhar Thanks for your contribution and collaboration. Can you please help us answering in brief, how the file changes are not affected any functional scenario? Thanks in advance! |
Hi @tobias-forkel , |
@engcom-Alfa, the mistake can be seen here, where What I think is the purpose of this So steps to reproduce in the backoffice of Magento:
@tobias-forkel: correct me if I'm wrong 🙂 |
Hi @hostep Thank you so much for the detailed above explanation! I have followed to identify the issue as mentioned below that just follows as you explained above
Query: I took the file changes into the local and repeated the above step6. Didn't notice any change in the feature after pulling PR code changes. So, can you please help me to understand in which scenario the file changes are going to be utilised and what fix does it do? @hostep Thanks for this additional info about this PR #31645 too! |
@engcom-Alfa, you should repeat from step 4 with the code changes from this PR, in step 4 you should no longer see To repeat myself, the point of this change, is that a certain SQL statement is no longer executed when saving configurable, bundled and grouped products when they have a required custom option. Which is a micro optimisation. But it's a good one because the code as it is currently makes no sense (even though it doesn't really cause issues). |
Thank you for the reply! |
@magento create issue |
✔️ QA Passed Manual testing scenario:
Before: ✖️ Updating configurable products functionality was calling a method After: ✔️ Updating configurable products functionality not calling a method It is a minor code fix and has no impact on functional features explicitly, required no additional regression test cases. |
Well explained @hostep! Thank you! Yes, depending on how product options are setup, the backend will falsely return In this particular case I was able to fix it by manually changing the condition in |
Description (*)
The condition supposed to match
$product->getTypeId()
against['grouped', 'configurable', 'bundle']
- not the product id.Related Pull Requests
Fixed Issues (if relevant)
Manual testing scenarios (*)
No testing required.
Questions or comments
Contribution checklist (*)
Resolved issues: