Magento 2.4.2-p1
Goal is to create a bundle option with a selection in it based on array of skus.
The array has 4 skus of simples existing in the catalog, lets say:
[50702640,50702240,90102870,90102871]
The strange thing, is that it will never add the first one.
Logs i have put in:
module-bundle/Model/Option/SaveAction
module-bundle/Model/OptionRepository
module-bundle/Model/OptionManagement
shows its being saved and giving back an optionId:
saveAction save SAVED resource save option title: 1665148566 optionId: 908641
saveAction save SAVED resource save option title: 1665148566 optionId: 908642
saveAction save SAVED resource save option title: 1665148566 optionId: 908643
saveAction save SAVED resource save option title: 1665148566 optionId: 908644
BUT, db shows differently (908641 is missing):
mysql> SELECT * FROM catalog_product_bundle_option_value WHERE title REGEXP ‘^[0-9]+$’;
| value_id | option_id | parent_product_id | store_id | title | placeholder |
| 21012029 | 908642 | 5831 | 0 | 1665148566 | NULL |
| 21012055 | 908643 | 5831 | 0 | 1665148566 | NULL |
| 21012083 | 908644 | 5831 | 0 | 1665148566 | NULL |
mysql> select * from catalog_product_bundle_option order by option_id DESC limit 20;
| option_id | parent_id | required | position | type | category_id |
| 908644 | 5831 | 0 | 0 | radio | NULL |
| 908643 | 5831 | 0 | 0 | radio | NULL |
| 908642 | 5831 | 0 | 0 | radio | NULL |
I have tried changing order of skus with no result.
(as alternative, if i create only one option, and put the array of links in it and save that only option, it wont save)
All of this, without errors that i can find in logs (debug, syslog, system, exception, mysql)
And here the code in question:
foreach ($newProductLinksSkus as $newLinkSku)
{
/** @var OptionInterface $option */
$option = $this->optionInterfaceFactory->create();
$option->setRequired(0)
->setType('radio')
->setTitle(strval($this->date->getTimestamp()))
->setOptionId(null)
->setSku($this->currentProduct->getSku());
/** @var LinkInterface $link */
$link = $this->linkFactory->create();
$link->setSku($newLinkSku)
->setQty($incomingItemLinksSkusAndQty[$newLinkSku])
->setCanChangeQuantity(1);
try {
$option->setProductLinks([$link]);
$optionid = $this->optionManagement->save($option);
#$optionid = $this->productOptionRepository->save($this->currentProduct, $option);
} catch (CouldNotSaveException|InputException $e) {
$this->logger->critical($this->prefix . $fn . "save fail {$e->getMessage()}");
}
}