Answer the question
In order to leave comments, you need to log in
How to assign an object id to its collection elements in magento 2?
It is required to create a payment and its items (paid orders). It is not difficult to create separately, but I don’t know how to create it in a transaction.
Code example:
$payment = $this->paymentFactory->create();
$payment->setCustomerId($order->getCustomerId());
$payment->setAmount($amount);
$payment->save();
$paymentOrder = $this->orderConvertor->toPaymentOrder($order);
$paymentOrder->setAmount($amount);
$paymentOrder->setPaymentId($payment->getId()); // <- проблема в этом
$paymentOrder->save();
$transaction = $this->transactionFactory->create();
$transaction->addObject($payment);
$transaction->save();
Answer the question
In order to leave comments, you need to log in
Seems to have found it.
namespace Magento\Sales\Model\ResourceModel\Order;
class Payment extends SalesResource
{
// ...
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
/**@var $object \Magento\Sales\Model\Order\Payment */
parent::_beforeSave($object);
if (!$object->getParentId() && $object->getOrder()) {
$object->setParentId($object->getOrder()->getId());
}
return $this;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question