如何以编程方式取消 magento 2 中的订单
How to Programatically cancel order in magento 2
如果客户下了 3 种不同数量的不同商品的订单,例如
- 衬衫 2 数量
- 观看 1 次
- 裤子 3 条
并在不同时间取消订单项目我必须将状态设置为已取消订单
这是我的代码,谁能告诉我哪里错了
if (isset($_POST['order'])) {
$_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$collection = $_orderCollectionFactory->create()
->addFieldToSelect('*')
->addFieldToFilter('status', ['neq' => 'cancelled']);
foreach ($collectionFactory as $order)
{
$items = $order->getAllVisibleItems();
foreach ($items as $items) {
$totalitem = $totalitem + ($item['qty_canceled']);
$itemcount = $order->getQtyOrdered();
if ($itemcount == ($totalitem))
{
$order->setState("canceled");
$order->save();
}
}
}
}
最后,我已经解决了上面代码的问题
$_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$collection = $_orderCollectionFactory->create()
->addFieldToSelect('*')
->addFieldToFilter('status', ['neq' => 'cancelled']);
foreach ($collection as $order)
{
$items = $order->getAllVisibleItems();
$totalitem=0;
foreach ($items as $item) {
// echo '<pre>';print_r(get_class_methods($item));
// die();
$totalitem = $totalitem + ($item['qty_canceled']);
$itemcount = $order->getQtyOrdered();
if ($itemcount == ($totalitem))
{
echo $order->getIncrementId();
$order->setState("canceled");
$order->setStatus("canceled");
$order->save();
}
}
}
如果客户下了 3 种不同数量的不同商品的订单,例如
- 衬衫 2 数量
- 观看 1 次
- 裤子 3 条
并在不同时间取消订单项目我必须将状态设置为已取消订单
这是我的代码,谁能告诉我哪里错了
if (isset($_POST['order'])) {
$_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$collection = $_orderCollectionFactory->create()
->addFieldToSelect('*')
->addFieldToFilter('status', ['neq' => 'cancelled']);
foreach ($collectionFactory as $order)
{
$items = $order->getAllVisibleItems();
foreach ($items as $items) {
$totalitem = $totalitem + ($item['qty_canceled']);
$itemcount = $order->getQtyOrdered();
if ($itemcount == ($totalitem))
{
$order->setState("canceled");
$order->save();
}
}
}
}
最后,我已经解决了上面代码的问题
$_orderCollectionFactory = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');
$collection = $_orderCollectionFactory->create()
->addFieldToSelect('*')
->addFieldToFilter('status', ['neq' => 'cancelled']);
foreach ($collection as $order)
{
$items = $order->getAllVisibleItems();
$totalitem=0;
foreach ($items as $item) {
// echo '<pre>';print_r(get_class_methods($item));
// die();
$totalitem = $totalitem + ($item['qty_canceled']);
$itemcount = $order->getQtyOrdered();
if ($itemcount == ($totalitem))
{
echo $order->getIncrementId();
$order->setState("canceled");
$order->setStatus("canceled");
$order->save();
}
}
}