4

I am doing a webapi for Magento 2.1 and i need to create configurable product and simple products and then linking it all up.

A psuedo-code normal transaction will look something like this.

//Initialize database transactionFactory
$this->transactionFactory:  \Magento\Framework\DB\TransactionFactory

$addTransaction = $this->transactionFactory->create();

try{ 

 //Point 1: Add simple products
 $simpleProduct = [....]
 $simpleProduct2 = [....] 

 //Point 2: Add configurable product
 $configurableProduct = [....]

 //Point 3: Add to transaction
 $addTransaction->addObject($simpleProduct);
 $addTransaction->addObject($simpleProduct2);
 $addTransaction->addObject($configurableProduct );

 //Point 4: Form the relation
 .....

 //Point 5: Save transaction
 $result = $addTransaction->save();

}

*note:

i am aware the above wouldn't work as the products have not been added to database yet at point 4, so i am not able to retrieve the IDs to form the relation.

So my requirement is to do a "transaction-save" BEFORE point 4, as i need to have the IDs of the simple products and configurable product before i can form the relation. But at the same time, have an option to rollback everything if any error comes up from point 4 onwards.

Any advice would be great. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.