我正在创建一个退款模块,但使用该方法时出错
<?php
namespace CrediYa\CrediYa\Controller\CrediYa;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Sales\Model\Order\Creditmemo\CreationArgumentsFactory;
class Refund extends Action
{
/** @var \Magento\Framework\App\Request\Http */
protected $request;
public function __construct(
Context $context,
Http $request,
\Magento\Sales\Api\RefundInvoiceInterface $invoiceRefunder,
CreationArgumentsFactory $creationArguments
)
{
$this->request = $request;
$this->invoiceRefunder = $invoiceRefunder;
$this->creationArguments = $creationArguments;
parent::__construct($context);
}
public function execute()
{
$items = [];
$order_id = $this->request->getParam('order_id');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($order_id);
foreach ($order->getAllVisibleItems() as $item)
{
$items[$item->getId()] = array(
'qty' => $item->getQtyOrdered()
);
// $object['order_item_id'] = $item->getId();
// $object['qty'] = $item->getQtyOrdered();
}
$invoiceId = null;
foreach ($order->getInvoiceCollection() as $invoice)
{
// this will always point to the latest invoice id
$invoiceId = $invoice->getId();
}
$this->invoiceRefunder->execute(
$invoiceId, // Invoice ID (NOTE: not increment ID)
$items, // Items array (default all)
false // false = offline / true = online
);
}
}
这是我班的代码
在其他问题中,他们说 $ items 参数,如果它是空的,该方法会将其作为订单的总回报,但是这样做时它会给我这个错误Exception #0 (Magento\Sales\Exception\DocumentValidationException): Creditmemo Document Validation Error(s): We can't create creditmemo for the invoice.
另一方面,如果我在 $ items 中发送某些内容,则不再出现此错误,但它会给出另一个错误,该错误源自 magento 供应商,特别是在此代码区域中。
foreach ($items as $item) {
$data['qtys'][$item->getOrderItemId()] = $item->getQty();
}
这是在文件CredimemoDocumentFactory.php
中,我想我发送数组中的文章是错误的,因为它试图在每个项目上调用一个函数getOrderItemId()
并且getQty()
我应该如何发送我的 $ items 数组的数据结构来解决这个错误,并且 magento 可以在每个元素上执行必要的方法吗?