문제

I am trying it as:

Using the collection

\Magento\CustomerBalance\Model\Balance\HistoryFactory $historyFactory

As

$model = $this->_historyFactory->create();
    //$model->setId(2);
    $model->setData('balance_amount', 333);
    $model->save();

But it prompts this exception:

1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): A balance is needed to save a balance history.

도움이 되었습니까?

해결책

If you see in file magento/module-customer-balance/Model/Balance/History.php line no: 154. You need to set setBalanceModel and setHistoryAction like this

require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');


// for Update customer balance history set setHistoryAction(2)

$customerBalance = $obj->create(\Magento\CustomerBalance\Model\BalanceFactory::class)->create()->load(2)->setHistoryAction(2);

$customerBalanceHistory = $obj->create(\Magento\CustomerBalance\Model\Balance\History::class)->create()
        ->setBalanceModel($customerBalance)
        ->setId(2)
        ->setAdditionalInfo('test')
        ->save();    

// for new entry use set setHistoryAction(1)

$customerBalance = $obj->create(\Magento\CustomerBalance\Model\BalanceFactory::class)->create()->load(2)->setHistoryAction(1);

$customerBalanceHistory = $obj->create(\Magento\CustomerBalance\Model\Balance\HistoryFactory::class)->create()
        ->setBalanceModel($customerBalance)
        ->setAdditionalInfo('test')
        ->save();

Hope this will solve your problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top